git-svn-id: https://s3tools.svn.sourceforge.net/svnroot/s3tools/s3cmd/trunk@330 830e0280-6d2a-0410-9c65-932aecc39d9d
Michal Ludvig authored on 2009/01/07 12:06:35... | ... |
@@ -1,5 +1,9 @@ |
1 | 1 |
2009-01-07 Michal Ludvig <michal@logix.cz> |
2 | 2 |
|
3 |
+ * S3/ACL.py: Generate XML from a current list of Grantees |
|
4 |
+ |
|
5 |
+2009-01-07 Michal Ludvig <michal@logix.cz> |
|
6 |
+ |
|
3 | 7 |
* S3/ACL.py: Keep ACL internally as a list of of 'Grantee' objects. |
4 | 8 |
* S3/Utils.py: Fix crash in stripNameSpace() when the XML has no NS. |
5 | 9 |
|
... | ... |
@@ -19,14 +19,30 @@ class Grantee(object): |
19 | 19 |
display_name = None |
20 | 20 |
permission = None |
21 | 21 |
|
22 |
- def __str__(self): |
|
23 |
- return '%(name)s : %(permission)s' % { "name" : self.name, "permission" : self.permission } |
|
22 |
+ def __repr__(self): |
|
23 |
+ return 'Grantee("%(tag)s", "%(name)s", "%(permission)s")' % { |
|
24 |
+ "tag" : self.tag, |
|
25 |
+ "name" : self.name, |
|
26 |
+ "permission" : self.permission |
|
27 |
+ } |
|
24 | 28 |
|
25 | 29 |
def isAllUsers(self): |
26 | 30 |
return self.tag == "URI" and self.name == Grantee.ALL_USERS_URI |
27 | 31 |
|
28 | 32 |
def isAnonRead(self): |
29 | 33 |
return self.isAllUsers and self.permission == "READ" |
34 |
+ |
|
35 |
+ def getElement(self): |
|
36 |
+ el = ET.Element("Grant") |
|
37 |
+ grantee = ET.SubElement(el, "Grantee", { |
|
38 |
+ 'xmlns:xsi' : 'http://www.w3.org/2001/XMLSchema-instance', |
|
39 |
+ 'xsi:type' : self.xsi_type |
|
40 |
+ }) |
|
41 |
+ name = ET.SubElement(grantee, self.tag) |
|
42 |
+ name.text = self.name |
|
43 |
+ permission = ET.SubElement(el, "Permission") |
|
44 |
+ permission.text = self.permission |
|
45 |
+ return el |
|
30 | 46 |
|
31 | 47 |
class GranteeAnonRead(Grantee): |
32 | 48 |
xsi_type = "Group" |
... | ... |
@@ -35,20 +51,15 @@ class GranteeAnonRead(Grantee): |
35 | 35 |
permission = "READ" |
36 | 36 |
|
37 | 37 |
class ACL(object): |
38 |
- EMPTY_ACL = """ |
|
39 |
- <AccessControlPolicy> |
|
40 |
- <AccessControlList> |
|
41 |
- </AccessControlList> |
|
42 |
- </AccessControlPolicy> |
|
43 |
- """ |
|
38 |
+ EMPTY_ACL = "<AccessControlPolicy><AccessControlList></AccessControlList></AccessControlPolicy>" |
|
44 | 39 |
|
45 |
- grants = [] |
|
40 |
+ grantees = [] |
|
46 | 41 |
|
47 | 42 |
def __init__(self, xml = None): |
48 | 43 |
if not xml: |
49 | 44 |
xml = ACL.EMPTY_ACL |
50 | 45 |
self.tree = getTreeFromXml(xml) |
51 |
- self.grants = self.parseGrants() |
|
46 |
+ self.parseGrants() |
|
52 | 47 |
|
53 | 48 |
def parseGrants(self): |
54 | 49 |
for grant in self.tree.findall(".//Grant"): |
... | ... |
@@ -62,12 +73,11 @@ class ACL(object): |
62 | 62 |
else: |
63 | 63 |
grantee.tag = el.tag |
64 | 64 |
grantee.name = el.text |
65 |
- self.grants.append(grantee) |
|
66 |
- return self.grants |
|
65 |
+ self.grantees.append(grantee) |
|
67 | 66 |
|
68 | 67 |
def getGrantList(self): |
69 | 68 |
acl = {} |
70 |
- for grantee in self.grants: |
|
69 |
+ for grantee in self.grantees: |
|
71 | 70 |
if grantee.display_name: |
72 | 71 |
user = grantee.display_name |
73 | 72 |
elif grantee.isAllUsers(): |
... | ... |
@@ -78,20 +88,25 @@ class ACL(object): |
78 | 78 |
return acl |
79 | 79 |
|
80 | 80 |
def isAnonRead(self): |
81 |
- for grantee in self.grants: |
|
81 |
+ for grantee in self.grantees: |
|
82 | 82 |
if grantee.isAnonRead(): |
83 | 83 |
return True |
84 | 84 |
return False |
85 | 85 |
|
86 | 86 |
def grantAnonRead(self): |
87 | 87 |
if not self.isAnonRead(): |
88 |
- self.grants.append(GranteeAnonRead()) |
|
88 |
+ self.grantees.append(GranteeAnonRead()) |
|
89 | 89 |
|
90 | 90 |
def revokeAnonRead(self): |
91 |
- self.grants = [g for g in self.grants if not g.isAnonRead()] |
|
91 |
+ self.grantees = [g for g in self.grantees if not g.isAnonRead()] |
|
92 | 92 |
|
93 | 93 |
def __str__(self): |
94 |
- return ET.tostring(self.tree) |
|
94 |
+ tree = getTreeFromXml(ACL.EMPTY_ACL) |
|
95 |
+ tree.attrib['xmlns'] = "http://s3.amazonaws.com/doc/2006-03-01/" |
|
96 |
+ acl = tree.find(".//AccessControlList") |
|
97 |
+ for grantee in self.grantees: |
|
98 |
+ acl.append(grantee.getElement()) |
|
99 |
+ return ET.tostring(tree) |
|
95 | 100 |
|
96 | 101 |
if __name__ == "__main__": |
97 | 102 |
xml = """<?xml version="1.0" encoding="UTF-8"?> |
... | ... |
@@ -123,4 +138,4 @@ if __name__ == "__main__": |
123 | 123 |
print "Grants:", acl.getGrantList() |
124 | 124 |
acl.grantAnonRead() |
125 | 125 |
print "Grants:", acl.getGrantList() |
126 |
- #print acl |
|
126 |
+ print acl |