def _update(attrs, *sources): ''' Updates attrs from sources: - Every source with a __name__ that is not a class is updated into attrs. - For every source that is a class, it's public attributes and all methods are updated into attrs. ''' from six import class_types, iteritems as iteritems_ attrs.update({str(f.__name__): f for f in sources if (not isinstance(f, class_types) and getattr(f, '__name__', False))}) attrs.update({str(name): member for cls in sources if isinstance(cls, class_types) # XXX: [manu] Use cls.__dict__ instead of xdir(cls) since # getattr(cls, attr) would not yield the classmethod and # staticmethod wrappers. for name, member in iteritems_(cls.__dict__) if not name.startswith('_') or isinstance(member, FunctionType)}) return attrs
def inner(cls): from collections import (Mapping, MutableMapping, MutableSequence as List, Set) from xoutil import Unset from six import iteritems as iteritems_ for attr, value in iteritems_(attrs): attr = str(attr) assigned = attr in cls.__dict__ if assigned: ok = isinstance # XXX: [manu] In order to be Python 2 and 3 compatible is best # to get things from the class' dictionary, then current would # be a function for methods, a classmethod for classmethods and # a staticmethod for staticmethods. current = cls.__dict__.get(attr, None) if ok(value, Mapping) and ok(current, MutableMapping): current.update(value) value = Unset elif ok(value, List) and ok(current, List): current.extend(value) value = Unset elif ok(value, tuple) and ok(current, tuple): value = current + value elif ok(value, Set) and ok(current, Set): value = current | value elif ok(value, (FunctionType, classmethod, staticmethod)) and current: setattr(cls, str('_super_%s') % attr, current) else: current = None if value is not Unset: if current and not getattr(value, '__doc__', False): from functools import update_wrapper update_wrapper(value, current) setattr(cls, attr, value) return cls
from six import PY3 as _py3k, iteritems as iteritems_ from xoutil.string import safe_decode if _py3k: from html import entities from html import parser else: import htmlentitydefs as entities import HTMLParser as parser entities.entitydefs_unicode = {} entities.entitydefs_utf8 = {} for name, entity in iteritems_(entities.entitydefs): text = entities.entitydefs_unicode[name] = safe_decode(entity, 'latin-1') entities.entitydefs_utf8[name] = text.encode('utf-8') del name, entity, safe_decode, iteritems_ def _further_escape(s): import re from xoutil.string import safe_encode ASCII = getattr(re, 'ASCII', 0) # Py3k what = re.compile(br'[\x00-\x1F\x80-\xFF]', ASCII) res, pos = b'', 0 for match in what.finditer(s): char, start, end = match.group(), match.start(), match.end() assert start + 1 == end res += s[pos:start]