def update_object(self, obj, options): """Create a new binding type of type `typ', and set its attributes with values from `options'.""" attrs = [ opt for opt in options if not opt.endswith('id') ] attrs.sort() for attr in attrs: baseobj = obj basetype = type(obj) walked = [] path = attr[2:].split('-') for pa in path[:-1]: walked.append(pa) try: subobj = getattr(baseobj, pa) subtype = getattr(basetype, pa) except AttributeError: self.error('no such attribute: %s' % '.'.join(walked)) if subobj is None: subtype = schema.subtype(subtype) if issubclass(subtype, schema.ComplexType): setattr(baseobj, pa, schema.new(subtype)) subobj = getattr(baseobj, pa) baseobj = subobj basetype = subtype if not hasattr(basetype, path[-1]): self.error('no such attribute: %s' % attr) setattr(baseobj, path[-1], options[attr]) return obj
def get_attributes(self, typ, prefix=''): """Return a list of valid attributes for a type.""" attrs = [] for elem in typ._ElementMap: name = elem.localName() if name in ('actions', 'link', 'fault'): continue prop = getattr(typ, name) if not isinstance(prop, property): continue subtype = schema.subtype(prop) if issubclass(subtype, schema.ComplexType): info = schema.type_info(subtype) if info is None: attrs += self.get_attributes(subtype, prefix + name + '.') elif info[3]: attrs.append('%s%s.id' % (prefix, name)) attrs.append('%s%s.name' % (prefix, name)) elif issubclass(subtype, schema.SimpleType): attrs.append('%s%s' % (prefix, name)) for attr in typ._AttributeMap: if not prefix and attr in ('id', 'href'): continue name = attr.localName() attrs.append('%s%s' % (prefix, name)) return attrs
def set(self, obj, value, context): obj, attr = self._resolve_parent(obj, self.attribute) subtyp = schema.subtype(getattr(type(obj), attr)) refobj = context.command.get_object(subtyp, value, None) if not refobj: context.command.error('%s not found: %s' % (self.name, value)) setattr(obj, attr, schema.ref(refobj))
def _resolve_parent(self, obj, attr): """INTERNAL: Resolve a dotted attribute name `attr' inside `obj' until its parent is reached, creating classes on the fly where required.""" baseobj = obj basetype = type(obj) walked = [] path = attr.split('.') for pa in path[:-1]: walked.append(pa) try: subobj = getattr(baseobj, pa) except AttributeError: m = 'no such attribute: %s' % '.'.join(walked) raise ValueError, m if subobj is None: prop = getattr(type(baseobj), pa) subtype = schema.subtype(prop) if issubclass(subtype, schema.ComplexType): setattr(baseobj, pa, schema.new(subtype)) subobj = getattr(baseobj, pa) baseobj = subobj return baseobj, path[-1]