def _init(self): dth = DatetimeHelper() path = object.__getattribute__(self, '_path') if not path or not os.path.exists(path): return file = open(path, 'r') tree = etree.parse(file) file.close() root = tree.getroot() data = object.__getattribute__(self, '_data') for elem in root.getchildren(): children = elem.getchildren() if children: subchildren = children[0].getchildren() # case dict like if subchildren: val = odict() for subelem in children: entry_elems = subelem.getchildren() val[entry_elems[0].text] = entry_elems[1].text # case list like else: val = list() for subelem in children: value = subelem.text if not value: value = '' val.append(dth.r_value(value.strip())) data[elem.tag] = val else: value = elem.text if not value: value = '' data[elem.tag] = dth.r_value(value.strip()) file.close()
def _xml_repr(self): dth = DatetimeHelper() root = etree.Element('properties') data = object.__getattribute__(self, '_data') for key, value in data.items(): sub = etree.SubElement(root, key) if type(value) in [types.ListType, types.TupleType]: for item in value: item_elem = etree.SubElement(sub, 'item') item_elem.text = dth.w_value(item) elif type(value) is types.DictType or isinstance(value, odict): for key, val in value.items(): dict_entry_elem = etree.SubElement(sub, 'elem') key_elem = etree.SubElement(dict_entry_elem, 'key') value_elem = etree.SubElement(dict_entry_elem, 'value') key_elem.text = dth.w_value(key) value_elem.text = dth.w_value(val) else: sub.text = dth.w_value(value) return etree.tostring(root, pretty_print=True)