def marshal(obj, keys=None, marshallerName='', objs=None): """ Convert an object to a dictionary. keys is an optional list of keys to include in the returned dictionary. if keys is None then all public attributes are returned. marshallerName is an optional marshalling adapter name. if it is an empty string then the default marshaller will be used. """ #to prevent recursing back over something twice, keep track of seen objs if objs is None: objs = OOSet() # obj is itself marshallable, so make a marshaller and marshal away if IMarshallable.providedBy(obj): marshaller = component.getAdapter(obj, IMarshaller, marshallerName) verify.verifyObject(IMarshaller, marshaller) if IInfo.providedBy(obj): key = (obj._object._p_oid, obj.__class__) if key in objs: raise AlreadySeenException() else: objs.insert(key) try: return marshal(marshaller.marshal(keys), keys, marshallerName, objs) except AlreadySeenException: pass finally: objs.remove(key) else: return marshal(marshaller.marshal(keys), keys, marshallerName, objs) # obj is a dict, so marshal its values recursively # Zuul.marshal({'foo':1, 'bar':2}) if isinstance(obj, dict): marshalled_dict = {} for k in obj: try: marshalled_dict[k] = marshal(obj[k], keys, marshallerName, objs) except AlreadySeenException: pass return marshalled_dict # obj is a non-string iterable, so marshal its members recursively # Zuul.marshal(set([o1, o2])) elif hasattr(obj, '__iter__'): marshalled_list = [] for o in obj: try: marshalled_list.append(marshal(o, keys, marshallerName, objs)) except AlreadySeenException: pass return marshalled_list elif isinstance(obj, DateTime ): return str(obj) # Nothing matched, so it's a string or number or other unmarshallable. else: return obj
def marshal(obj, keys=None, marshallerName='', objs=None): """ Convert an object to a dictionary. keys is an optional list of keys to include in the returned dictionary. if keys is None then all public attributes are returned. marshallerName is an optional marshalling adapter name. if it is an empty string then the default marshaller will be used. """ #to prevent recursing back over something twice, keep track of seen objs if objs is None: objs = OOSet() # obj is itself marshallable, so make a marshaller and marshal away if IMarshallable.providedBy(obj): marshaller = component.getAdapter(obj, IMarshaller, marshallerName) verify.verifyObject(IMarshaller, marshaller) if IInfo.providedBy(obj): key = (getattr(obj._object, '_p_oid', id(obj)), obj.__class__) if key in objs: raise AlreadySeenException() else: objs.insert(key) try: return marshal(marshaller.marshal(keys), keys, marshallerName, objs) except AlreadySeenException: pass finally: objs.remove(key) else: return marshal(marshaller.marshal(keys), keys, marshallerName, objs) # obj is a dict, so marshal its values recursively # Zuul.marshal({'foo':1, 'bar':2}) if isinstance(obj, dict): marshalled_dict = {} for k in obj: try: marshalled_dict[k] = marshal(obj[k], keys, marshallerName, objs) except AlreadySeenException: pass return marshalled_dict # obj is a non-string iterable, so marshal its members recursively # Zuul.marshal(set([o1, o2])) elif hasattr(obj, '__iter__'): marshalled_list = [] for o in obj: try: marshalled_list.append(marshal(o, keys, marshallerName, objs)) except AlreadySeenException: pass return marshalled_list elif isinstance(obj, DateTime ): return str(obj) # Nothing matched, so it's a string or number or other unmarshallable. else: return obj
def info(obj, adapterName=''): """ Recursively adapt obj or members of obj to IInfo. """ def infoize(o): return info(o, adapterName) if IInfo.providedBy(obj): return obj # obj is a dict, so apply to its values recursively elif isinstance(obj, dict): return dict((k, infoize(obj[k])) for k in obj) # obj is a non-string iterable, so apply to its members recursively elif hasattr(obj, '__iter__') and not isinstance(obj, ObjectManager): return map(infoize, obj) # attempt to adapt; if no adapter, return obj itself else: return component.queryAdapter(obj, IInfo, adapterName, obj)