Beispiel #1
0
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
Beispiel #2
0
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
Beispiel #3
0
def account_request(request, end):
    ticket = ITicket(request)
    id = ticket.id
    info = str(IInfo(request))
    _lock.acquire()
    try:
        if end: del _state[id]
        else: _state[id] = Request(id, info, request, ticket.time, get_ident())
    finally:
        _lock.release()
Beispiel #4
0
def account_request(request, status=0):
    ticket = ITicket(request)
    id = ticket.id
    info = str(IInfo(request))
    request_time = 0
    type = status and '-' or '+'
    ct = time()
    _lock.acquire()
    try: 
        if status:
            request_time = ct - _state[id]
            del _state[id]
        else: _state[id] = ct
    finally:
        _lock.release()
    _log(type=type, status=status,
         request_id=id, request_time=request_time, info = info
         )
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)
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)