82
83
xmlns = wsgi.XMLNS_V11
85
NSMAP = {None: xmlutil.XMLNS_V11, 'atom': xmlutil.XMLNS_ATOM}
84
87
def __init__(self):
87
def _create_rates_node(self, xml_doc, rates):
88
rates_node = xml_doc.createElement('rates')
90
def _create_rates_node(self, rates):
91
rates_elem = etree.Element('rates', nsmap=self.NSMAP)
90
rate_node = xml_doc.createElement('rate')
91
rate_node.setAttribute('uri', rate['uri'])
92
rate_node.setAttribute('regex', rate['regex'])
93
rate_node = etree.SubElement(rates_elem, 'rate')
94
rate_node.set('uri', rate['uri'])
95
rate_node.set('regex', rate['regex'])
94
96
for limit in rate['limit']:
95
limit_node = xml_doc.createElement('limit')
96
limit_node.setAttribute('value', str(limit['value']))
97
limit_node.setAttribute('verb', limit['verb'])
98
limit_node.setAttribute('remaining', str(limit['remaining']))
99
limit_node.setAttribute('unit', limit['unit'])
100
limit_node.setAttribute('next-available',
101
str(limit['next-available']))
102
rate_node.appendChild(limit_node)
104
rates_node.appendChild(rate_node)
107
def _create_absolute_node(self, xml_doc, absolutes):
108
absolute_node = xml_doc.createElement('absolute')
109
for key, value in absolutes.iteritems():
110
limit_node = xml_doc.createElement('limit')
111
limit_node.setAttribute('name', key)
112
limit_node.setAttribute('value', str(value))
113
absolute_node.appendChild(limit_node)
116
def _limits_to_xml(self, xml_doc, limits):
117
limits_node = xml_doc.createElement('limits')
118
rates_node = self._create_rates_node(xml_doc, limits['rate'])
119
limits_node.appendChild(rates_node)
121
absolute_node = self._create_absolute_node(xml_doc, limits['absolute'])
122
limits_node.appendChild(absolute_node)
97
limit_elem = etree.SubElement(rate_node, 'limit')
98
limit_elem.set('value', str(limit['value']))
99
limit_elem.set('verb', str(limit['verb']))
100
limit_elem.set('remaining', str(limit['remaining']))
101
limit_elem.set('unit', str(limit['unit']))
102
limit_elem.set('next-available', str(limit['next-available']))
105
def _create_absolute_node(self, absolute_dict):
106
absolute_elem = etree.Element('absolute', nsmap=self.NSMAP)
107
for key, value in absolute_dict.items():
108
limit_elem = etree.SubElement(absolute_elem, 'limit')
109
limit_elem.set('name', str(key))
110
limit_elem.set('value', str(value))
113
def _populate_limits(self, limits_elem, limits_dict):
114
"""Populate a limits xml element from a dict."""
116
rates_elem = self._create_rates_node(
117
limits_dict.get('rate', []))
118
limits_elem.append(rates_elem)
120
absolutes_elem = self._create_absolute_node(
121
limits_dict.get('absolute', {}))
122
limits_elem.append(absolutes_elem)
126
124
def index(self, limits_dict):
127
xml_doc = minidom.Document()
128
node = self._limits_to_xml(xml_doc, limits_dict['limits'])
129
return self.to_xml_string(node, False)
125
limits = etree.Element('limits', nsmap=self.NSMAP)
126
self._populate_limits(limits, limits_dict['limits'])
127
return self._to_xml(limits)
132
130
def create_resource(version='1.0'):