def encode(self, arg): """Return an encoded copy of the argument - strs are decoded and reencode to make sure they conform to the encoding. XXX: makes no sence, especially because a UnicodeDecodeError ends up in a recursion error due to re-trying to encode. See below. Added condition to return if str is still str after decoding. This behavior should be removed completely. - unicodes are encoded as str according to encoding - lists/tuples/dicts are recursively worked on - everything else is left untouched """ if isinstance(arg, (list, tuple)): arg = arg.__class__(map(self.encode, arg)) elif isinstance(arg, dict): arg = dict([self.encode(t) for t in iteritems(arg)]) elif isinstance(arg, bytes): arg = self.decode(arg) # If UnicodeDecodeError, binary data is expected. Return value # as is. if not isinstance(arg, bytes): arg = self.encode(arg) elif isinstance(arg, UNICODE_TYPE): arg = arg.encode(self._encoding) elif INode.providedBy(arg): arg = dict([self.encode(t) for t in iteritems(arg)]) return arg
def default(self, ob): # serialize UNSET if ob is UNSET: return '<UNSET>' # serialize UUID if isinstance(ob, uuid.UUID): return '<UUID>:{}'.format(ob) # serialize Node if INode.providedBy(ob): ret = dict() if self.simple_mode: data = ret else: data = ret.setdefault('__node__', dict()) if not self.simple_mode or self.include_class: data['class'] = self.dotted_name(ob) data['name'] = ob.name for cls, callback in serializer.registry.items(): if issubclass(cls, Interface) and cls.providedBy(ob): callback(self, ob, data) elif isinstance(ob, cls): callback(self, ob, data) return ret # Serialize class, method or function if isclass(ob) or ismethod(ob) or isfunction(ob): if self.simple_mode: return self.dotted_name(ob) return {'__ob__': self.dotted_name(ob)} # no custom serialization required return ob
def printtree(self, indent=0): print "{0}{1}".format(indent * ' ', self.noderepr) for key, value in self.items(): if INode.providedBy(value): value.printtree(indent + 2) else: print "{0}{1}: {2}".format((indent + 2) * ' ', key, repr(value))
def printtree(self, indent=0): print "{0}{1}".format(indent * ' ', self.noderepr) for key, value in self.items(): if INode.providedBy(value): value.printtree(indent + 2) else: print "{0}{1}: {2}".format( (indent + 2) * ' ', key, repr(value) )
def __setitem__(_next, self, key, val): if INode.providedBy(val): try: next(val.iterkeys()) keys = set(self._index.keys()) if keys.intersection(val._index.keys()): raise ValueError('Node with uuid already exists') except StopIteration: pass self._index.update(val._index) val._index = self._index _next(self, key, val)
def treerepr(self, indent=0, prefix=' '): res = '{}{}\n'.format(indent * prefix, self.noderepr) items = self.items() \ if IOrdered.providedBy(self) \ else sorted(self.items(), key=lambda x: safe_decode(x[0])) for key, value in items: if INode.providedBy(value): res += value.treerepr(indent=indent + 2, prefix=prefix) else: res += '{}{}: {}\n'.format((indent + 2) * prefix, key, repr(value)) return res
def treerepr(self, indent=0): res = '{}{}\n'.format(indent * ' ', self.noderepr) items = self.items() \ if IOrdered.providedBy(self) \ else sorted(self.items(), key=lambda x: UNICODE_TYPE(x[0])) for key, value in items: if INode.providedBy(value): res += value.treerepr(indent + 2) else: res += '{}{}: {}\n'.format((indent + 2) * ' ', key, repr(value)) return res
def __setitem__(_next, self, key, val): if INode.providedBy(val): has_children = False for valkey in val.iterkeys(): has_children = True break if has_children: keys = set(self._index.keys()) if keys.intersection(val._index.keys()): raise ValueError, u"Node with uuid already exists" self._index.update(val._index) val._index = self._index _next(self, key, val)
def treerepr(self, indent=0): res = '{}{}\n'.format(indent * ' ', self.noderepr) items = self.items() \ if IOrdered.providedBy(self) \ else sorted(self.items(), key=lambda x: UNICODE_TYPE(x[0])) for key, value in items: if INode.providedBy(value): res += value.treerepr(indent + 2) else: res += '{}{}: {}\n'.format( (indent + 2) * ' ', key, repr(value) ) return res
def decode(self, arg): if isinstance(arg, (list, tuple)): arg = arg.__class__(map(self.decode, arg)) elif isinstance(arg, dict): arg = dict([self.decode(t) for t in iteritems(arg)]) elif isinstance(arg, bytes): try: arg = arg.decode(self._encoding) except UnicodeDecodeError: # in soft mode we leave the string, otherwise we raise the # exception if not self._soft: raise elif INode.providedBy(arg): arg = dict([self.decode(t) for t in iteritems(arg)]) return arg
def __setitem__(_next, self, key, val): # only care about adopting if we have a node if not INode.providedBy(val): _next(self, key, val) return # save old __parent__ and __name__ to restore if something goes wrong old_name = val.__name__ old_parent = val.__parent__ val.__name__ = key val.__parent__ = self try: _next(self, key, val) except (AttributeError, KeyError, ValueError): # XXX: In what other cases do we want to revert adoption? val.__name__ = old_name val.__parent__ = old_parent raise
def __setitem__(_next, self, key, val): if not self.allow_non_node_childs and inspect.isclass(val): raise ValueError(u"It isn't allowed to use classes as values.") if not self.allow_non_node_childs and not INode.providedBy(val): raise ValueError("Non-node childs are not allowed.") _next(self, key, val)
def __setitem__(_next, self, key, val): if INode.providedBy(val): val = val.context _next(self, key, val)
def __getitem__(_next, self, key): v = _next(self, key) if INode.providedBy(v): v._v_parent = self return v
def __getitem__(_next, self, key): val = _next(self, key) if INode.providedBy(val): val = NodeNode(val) return val
def __getitem__(self, key): v = self.storage[key] if INode.providedBy(v): v._v_parent = self return v