def construct(self): root = xmlutil.TemplateElement('servers') elem = xmlutil.SubTemplateElement(root, 'server', selector='servers') make_server(elem) return xmlutil.SlaveTemplate(root, 1)
def construct(self): root = xmlutil.TemplateElement('cells') elem = xmlutil.SubTemplateElement(root, 'cell', selector='cells') make_cell(elem) return xmlutil.MasterTemplate(root, 1, nsmap=cell_nsmap)
def construct(self): root = xmlutil.TemplateElement('servers') elem = xmlutil.SubTemplateElement(root, 'server', selector='servers') make_server(elem) return xmlutil.SubordinateTemplate( root, 1, nsmap={ServerUsage.alias: ServerUsage.namespace})
def construct(self): root = xmlutil.TemplateElement('floating_ips') elem = xmlutil.SubTemplateElement(root, 'floating_ip', selector='floating_ips') make_float_ip(elem) return xmlutil.MasterTemplate(root, 1)
def construct(self): root = xmlutil.TemplateElement('volumes') elem = xmlutil.SubTemplateElement(root, 'volume', selector='volumes') make_volume(elem) return xmlutil.MasterTemplate(root, 1)
def construct(self): root = xmlutil.TemplateElement('choices') elem = xmlutil.SubTemplateElement(root, 'version', selector='choices') make_version(elem) return xmlutil.MasterTemplate(root, 1, nsmap=version_nsmap)
def construct(self): root = xmlutil.TemplateElement('users') elem = xmlutil.SubTemplateElement(root, 'user', selector='users') make_user(elem) return xmlutil.MasterTemplate(root, 1)
def test__serialize(self): # Our test object to serialize obj = { 'test': { 'name': 'foobar', 'values': [1, 2, 3, 4], 'attrs': { 'a': 1, 'b': 2, 'c': 3, 'd': 4, }, 'image': { 'name': 'image_foobar', 'id': 42, }, }, } # Set up our main template root = xmlutil.TemplateElement('test', selector='test', name='name') value = xmlutil.SubTemplateElement(root, 'value', selector='values') value.text = xmlutil.Selector() attrs = xmlutil.SubTemplateElement(root, 'attrs', selector='attrs') xmlutil.SubTemplateElement(attrs, 'attr', selector=xmlutil.get_items, key=0, value=1) main = xmlutil.MainTemplate(root, 1, nsmap=dict(f='foo')) # Set up our subordinate template root_subordinate = xmlutil.TemplateElement('test', selector='test') image = xmlutil.SubTemplateElement(root_subordinate, 'image', selector='image', id='id') image.text = xmlutil.Selector('name') subordinate = xmlutil.SubordinateTemplate(root_subordinate, 1, nsmap=dict(b='bar')) # Attach the subordinate to the main... main.attach(subordinate) # Try serializing our object siblings = main._siblings() nsmap = main._nsmap() result = main._serialize(None, obj, siblings, nsmap) # Now we get to manually walk the element tree... self.assertEqual(result.tag, 'test') self.assertEqual(len(result.nsmap), 2) self.assertEqual(result.nsmap['f'], 'foo') self.assertEqual(result.nsmap['b'], 'bar') self.assertEqual(result.get('name'), obj['test']['name']) for idx, val in enumerate(obj['test']['values']): self.assertEqual(result[idx].tag, 'value') self.assertEqual(result[idx].text, str(val)) idx += 1 self.assertEqual(result[idx].tag, 'attrs') for attr in result[idx]: self.assertEqual(attr.tag, 'attr') self.assertEqual(attr.get('value'), str(obj['test']['attrs'][attr.get('key')])) idx += 1 self.assertEqual(result[idx].tag, 'image') self.assertEqual(result[idx].get('id'), str(obj['test']['image']['id'])) self.assertEqual(result[idx].text, obj['test']['image']['name']) templ = xmlutil.Template(None) self.assertEqual(templ.serialize(None), '')