def _init(self): dth = DatetimeHelper() path = o_getattr(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 = o_getattr(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 _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)
def _xml_repr(self): dth = DatetimeHelper() root = etree.Element('properties') data = o_getattr(self, '_data') for key, value in data.items(): sub = etree.SubElement(root, key) if type(value) in ITER_TYPES: for item in value: item_elem = etree.SubElement(sub, 'item') item_elem.text = dth.w_value(item) elif isinstance(value, dict) 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)
def test_DatetimeHelper(self): # Helper object for read/write operations with datetime values helper = DatetimeHelper() dt = datetime(2010, 1, 1, 10, 15) self.assertEqual(dt.isoformat(), '2010-01-01T10:15:00') self.assertEqual(helper.dt_to_iso(dt), '2010-01-01T10:15:00') dt = datetime(2010, 1, 1, 10, 15, 10, 5) self.assertEqual(dt.isoformat(), '2010-01-01T10:15:10.000005') self.assertEqual(helper.dt_to_iso(dt), '2010-01-01T10:15:10') self.assertEqual( helper.dt_from_iso('2010-01-01T10:15:00'), datetime(2010, 1, 1, 10, 15) ) self.assertEqual( helper.dt_from_iso(u'2010-01-01T10:15:00'), datetime(2010, 1, 1, 10, 15) ) self.assertEqual(helper.r_value(u'äöü'), u'äöü') self.assertEqual(helper.r_value(b'\xc3\xa4\xc3\xb6\xc3\xbc'), u'äöü') self.assertEqual( helper.r_value('2010-01-01T10:15:00'), datetime(2010, 1, 1, 10, 15) ) self.assertEqual(helper.w_value(b'abc'), u'abc') self.assertEqual(helper.w_value(u'abc'), u'abc') self.assertEqual(helper.w_value(u'äöü'), u'äöü') self.assertEqual(helper.w_value(dt), u'2010-01-01T10:15:10') self.assertEqual(helper.w_value(0), u'0') self.assertEqual(helper.w_value(0.0), u'0.0') self.assertEqual(helper.w_value(None), u'None') self.assertEqual(helper.w_value(True), u'True') self.assertEqual(helper.w_value(False), u'False')
def test_DatetimeHelper(self): # Helper object for read/write operations with datetime values helper = DatetimeHelper() dt = datetime(2010, 1, 1, 10, 15) self.assertEqual(dt.isoformat(), '2010-01-01T10:15:00') self.assertEqual(helper.dt_to_iso(dt), '2010-01-01T10:15:00') dt = datetime(2010, 1, 1, 10, 15, 10, 5) self.assertEqual(dt.isoformat(), '2010-01-01T10:15:10.000005') self.assertEqual(helper.dt_to_iso(dt), '2010-01-01T10:15:10') self.assertEqual(helper.dt_from_iso('2010-01-01T10:15:00'), datetime(2010, 1, 1, 10, 15)) self.assertEqual(helper.dt_from_iso(u'2010-01-01T10:15:00'), datetime(2010, 1, 1, 10, 15)) self.assertEqual(helper.r_value(u'äöü'), u'äöü') self.assertEqual(helper.r_value(b'\xc3\xa4\xc3\xb6\xc3\xbc'), u'äöü') self.assertEqual(helper.r_value('2010-01-01T10:15:00'), datetime(2010, 1, 1, 10, 15)) self.assertEqual(helper.w_value(b'abc'), u'abc') self.assertEqual(helper.w_value(u'abc'), u'abc') self.assertEqual(helper.w_value(u'äöü'), u'äöü') self.assertEqual(helper.w_value(dt), u'2010-01-01T10:15:10') self.assertEqual(helper.w_value(0), u'0') self.assertEqual(helper.w_value(0.0), u'0.0') self.assertEqual(helper.w_value(None), u'None') self.assertEqual(helper.w_value(True), u'True') self.assertEqual(helper.w_value(False), u'False')