def __get_field_value(self, field, klass, child, id, i): if isinstance(field.type, (tuple, list, set, frozenset)): value = self.__convert_list(klass, i, child, id, field) elif issubclass(field.type, Entity): try: value = self.__convert_entity(klass, child, id, field) except AttributeError, e: self.logger.exception("Unexpected error %s while unserializing %s" % (repr(e), tostring(child))) raise
def __unserialize_entity(self, klass, child, expect, i): #self.logger.debug("__unserialize_entity %s %s %s %s" % (klass, child, expect, i)) if expect != child.tag: raise Surprised("expected %s but list contained %s (%s)" % (expect, child.tag, tostring(child))) try: id = int(child.get("id")) except (TypeError, ValueError): raise Surprised("Illegal id at element %s: %s" % (i, child.get("id"))) values = {} for field in klass.get_fields(): values[field.name] = self.__get_field_value(field, klass, child, id, i) #self.logger.debug("__unserialize_entity out %s %s %s" % (klass, id, values)) return id, values
def serialize(self, e, pretty_print = True): assert isinstance(e, Entity) name = self.__get_tagname(e) + "Instance" parent = Element(name) for k, v in e._get_fields().iteritems(): if not v and v not in (0, False): SubElement(parent, k) elif isinstance(v, (tuple, set, list, frozenset)): for v2 in v: assert isinstance(v2, Entity) self.__append_entity(parent, k, v2, e.__class__) else: if isinstance(v, Entity): self.__append_entity(parent, k, v, e.__class__) else: child = SubElement(parent, k) child.text = unicode(v) return tostring(parent, pretty_print = pretty_print)
def __convert_entity(self, klass, child, id, field): element = self.__get_element_by_tagname(klass, child, id, field) if element is None: if field.name == "configParamAtomic": element = child.find("configurationParametersAtomic") if element is not None: return self.__convert_entity(klass, element, id, field) return None expect = field.type value = element.get("id") if not value and len(element): if len(element) > 1: self.logger.warn("Found more than one of child of %s for %s-%d in %s" % (field.name, klass, id, tostring(element))) value = element[0].get("id") self.__get_entity_klass(element[0].tag) if value: try: cid = int(value) except (TypeError, ValueError): raise Surprised("illegal value for id on attribute %s: %s (%s)" % (field.name, value, tostring(element))) try: #print ("get", expect, value) try: if klass is expect: value = self.__repo._get_entity_unlocked(expect, cid) else: #raise Exception(expect, cid) value = self.__repo._get_entity(expect, cid) except NoEntityFound, e: self.logger.exception("Could not find referenced element %s for %s-%d in %s" % (field.name, klass, id, tostring(element))) raise Surprised(e) except Exception, e: if expect is ConfigParamComposite: #self.logger.info("Was looking for ConfigParamComposite now trying ConfigParamAtomic") value = self.__repo._get_entity(ConfigParamAtomic, cid) #self.logger.warn("ConfigParamAtomic found where ConfigParamComposite was expected. This is an error in the repo's structure. I'll try to fix things for you.") comp = ConfigParamComposite( fields = { "commonName": u"autogenerated-%d" % (value.id, ), "description": u"" }) comp.configParams = [ value ] return comp self.logger.exception("error while getting %s-%s (id=%s) for %s-%s.%s: '%s' (%s)" % (field.type, value, value, klass, id, field.name, repr(e), tostring(element))) raise
def __convert_list(self, klass, i, child, parentid, field): assert child is not None, "None passed as child for __convert_list" #self.logger.debug("__convert_list %s %s", klass, field) node = self.__get_element_by_tagname(klass, child, parentid, field) #self.logger.debug("found child: %s" % (node, )) if node is None: raise Surprised("list attribute not found for %s-%s: %s (%s)" % (klass.__name__, parentid, field.name, tostring(child))) #print("__convert_list: %s %s %s %s %s" % (klass, i, child, parentid, field)) infos = [(element.tag, element.get("id")) for element in node] #self.logger.debug("have nfos %s " % (infos, )) try: infos = [(name, int(id)) for name, id in infos] except (ValueError, TypeError): raise Surprised("Illegal id '%s' in %s on element %d of %s-%d (%s - %s)" % (id, field.name, i, klass.__name__, parentid, infos, tostring(child))) #self.logger.debug("__convert_list call %s %s", klass, infos) list = [] for name, id in infos: memklass=self.__get_entity_klass(name) try: list.append(self.__repo._get_entity(klass = memklass, id=id)) except NoEntityFound: self.logger.exception("referenced entity not found when unserializing attribute %s of %s-%s: %s-%s" % (field.name, klass.__name__, parentid, memklass.__name__, id)) raise Surprised("referenced entity not found when unserializing attribute %s of %s-%s: %s-%s" % (field.name, klass.__name__, parentid, memklass.__name__, id)) #self.logger.debug("__convert_list %s %s", klass, infos) return list
elif issubclass(field.type, Entity): try: value = self.__convert_entity(klass, child, id, field) except AttributeError, e: self.logger.exception("Unexpected error %s while unserializing %s" % (repr(e), tostring(child))) raise else: value = self.__get_text(child, field.name) if value is not None: if issubclass(field.type, bool): value = value.lower() not in ( "", "false", "f", "no", "0" ) else: try: value = field.type(value) except (ValueError, TypeError) as e: raise Surprised("illegal value for attribute %s (%s): %s (%s, %s, %s)" % (field.name, field.type, value, repr(e), type(value), tostring(child))) if value is None: if not field.nullable: if ((field.default is None and not isinstance(field.type, type)) or not field.coerce_none): raise Surprised(("attribute %s not found on element %d (id=%d - %s)" % (field.name, i, id, tostring(child)))) #self.logger.warn("attribute %s not found on element %d (id=%d) %s %s" % (field.name, i, id, field.default, field.coerce_none)) value = field.get_default_value() #if value is not None: #self.logger.debug("Coercing to %s" % (value, )) elif field.enum is not None and value not in field.enum: if field.name == "configParamType": if value == "int": value = "integer" elif value == "BOOLEAN":