1
from txaws.util import XML
4
PERMISSIONS = ("FULL_CONTROL",
11
class XMLMixin(object):
14
return "".join(self._to_xml())
17
class AccessControlPolicy(XMLMixin):
19
def __init__(self, owner=None, access_control_list=()):
21
self.access_control_list = access_control_list
23
def _to_xml(self, buffer=None):
26
buffer.append("<AccessControlPolicy>\n")
28
self.owner._to_xml(buffer=buffer, indent=1)
29
buffer.append(" <AccessControlList>\n")
30
for grant in self.access_control_list:
31
grant._to_xml(buffer=buffer, indent=2)
32
buffer.append(" </AccessControlList>\n"
33
"</AccessControlPolicy>")
37
def from_xml(cls, xml_bytes):
39
owner_node = root.find("Owner")
40
owner = Owner(owner_node.findtext("ID"),
41
owner_node.findtext("DisplayName"))
42
acl_node = root.find("AccessControlList")
44
for grant_node in acl_node.findall("Grant"):
45
grantee_node = grant_node.find("Grantee")
46
grantee = Grantee(grantee_node.findtext("ID"),
47
grantee_node.findtext("DisplayName"))
48
permission = grant_node.findtext("Permission")
49
acl.append(Grant(grantee, permission))
50
return cls(owner, acl)
53
class Grant(XMLMixin):
55
def __init__(self, grantee, permission=None):
56
self.grantee = grantee
57
self.permission = permission
59
def _set_permission(self, perm):
60
if perm not in PERMISSIONS:
61
raise ValueError("Invalid permission '%s'. Must be one of %s" %
62
(perm, ",".join(PERMISSIONS)))
63
self._permission = perm
65
def _get_permission(self):
66
return self._permission
68
permission = property(_get_permission, _set_permission)
70
def _to_xml(self, buffer=None, indent=0):
73
ws = " " * (indent * 2)
74
buffer.append(ws + "<Grant>\n")
76
self.grantee._to_xml(buffer, indent + 1)
78
buffer.append("%s <Permission>%s</Permission>\n" % (
80
buffer.append(ws + "</Grant>\n")
84
class Owner(XMLMixin):
86
def __init__(self, id, display_name):
88
self.display_name = display_name
90
def _to_xml(self, buffer=None, indent=0):
93
ws = " " * (indent * 2)
94
buffer.append("%s<Owner>\n"
96
"%s <DisplayName>%s</DisplayName>\n"
97
"%s</Owner>\n" % (ws, ws, self.id, ws, self.display_name,
102
class Grantee(XMLMixin):
104
def __init__(self, id="", display_name="", email_address="", uri=""):
105
if id or display_name:
106
msg = "Both 'id' and 'display_name' must be provided."
107
if not (id and display_name):
108
raise ValueError(msg)
110
self.display_name = display_name
111
self.email_address = email_address
114
def _to_xml(self, buffer=None, indent=0):
117
ws = " " * (indent * 2)
118
if self.id and self.display_name:
119
xsi_type = "CanonicalUser"
120
value = ("%s <ID>%s</ID>\n"
121
"%s <DisplayName>%s</DisplayName>\n" % (
122
ws, self.id, ws, self.display_name))
123
elif self.email_address:
124
xsi_type = "AmazonCustomerByEmail"
125
value = "%s <EmailAddress>%s</EmailAddress>\n" % (
126
ws, self.email_address)
129
value = "%s <URI>%s</URI>\n" % (ws, self.uri)
130
buffer.append("%s<Grantee "
131
'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"'
133
"%s%s</Grantee>\n" % (ws, xsi_type, value, ws))