simple.py 8.02 KB
Newer Older
Stelios Karozis's avatar
Stelios Karozis committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
import operator

from .compat import PY2
from .compat import PY3
from .compat import with_metaclass
from .utils import cached_property
from .utils import identity


def make_proxy_method(code):
    def proxy_wrapper(self, *args):
        return code(self.__wrapped__, *args)

    return proxy_wrapper


class _ProxyMethods(object):
    # We use properties to override the values of __module__ and
    # __doc__. If we add these in ObjectProxy, the derived class
    # __dict__ will still be setup to have string variants of these
    # attributes and the rules of descriptors means that they appear to
    # take precedence over the properties in the base class. To avoid
    # that, we copy the properties into the derived class type itself
    # via a meta class. In that way the properties will always take
    # precedence.

    @property
    def __module__(self):
        return self.__wrapped__.__module__

    @__module__.setter
    def __module__(self, value):
        self.__wrapped__.__module__ = value

    @property
    def __doc__(self):
        return self.__wrapped__.__doc__

    @__doc__.setter
    def __doc__(self, value):
        self.__wrapped__.__doc__ = value

    # Need to also propagate the special __weakref__ attribute for case
    # where decorating classes which will define this. If do not define
    # it and use a function like inspect.getmembers() on a decorator
    # class it will fail. This can't be in the derived classes.

    @property
    def __weakref__(self):
        return self.__wrapped__.__weakref__


class _ProxyMetaType(type):
    def __new__(cls, name, bases, dictionary):
        # Copy our special properties into the class so that they
        # always take precedence over attributes of the same name added
        # during construction of a derived class. This is to save
        # duplicating the implementation for them in all derived classes.

        dictionary.update(vars(_ProxyMethods))
        dictionary.pop('__dict__')

        return type.__new__(cls, name, bases, dictionary)


class Proxy(with_metaclass(_ProxyMetaType)):
    __factory__ = None

    def __init__(self, factory):
        self.__dict__['__factory__'] = factory

    @cached_property
    def __wrapped__(self):
        self = self.__dict__
        if '__factory__' in self:
            factory = self['__factory__']
            return factory()
        else:
            raise ValueError("Proxy hasn't been initiated: __factory__ is missing.")

    __name__ = property(make_proxy_method(operator.attrgetter('__name__')))
    __class__ = property(make_proxy_method(operator.attrgetter('__class__')))
    __annotations__ = property(make_proxy_method(operator.attrgetter('__anotations__')))
    __dir__ = make_proxy_method(dir)
    __str__ = make_proxy_method(str)

    if PY3:
        __bytes__ = make_proxy_method(bytes)

    def __repr__(self, __getattr__=object.__getattribute__):
        if '__wrapped__' in self.__dict__:
            return '<{} at 0x{:x} wrapping {!r} at 0x{:x} with factory {!r}>'.format(
                type(self).__name__, id(self),
                self.__wrapped__, id(self.__wrapped__),
                self.__factory__
            )
        else:
            return '<{} at 0x{:x} with factory {!r}>'.format(
                type(self).__name__, id(self),
                self.__factory__
            )

    __reversed__ = make_proxy_method(reversed)

    if PY3:
        __round__ = make_proxy_method(round)

    __lt__ = make_proxy_method(operator.lt)
    __le__ = make_proxy_method(operator.le)
    __eq__ = make_proxy_method(operator.eq)
    __ne__ = make_proxy_method(operator.ne)
    __gt__ = make_proxy_method(operator.gt)
    __ge__ = make_proxy_method(operator.ge)
    __hash__ = make_proxy_method(hash)
    __nonzero__ = make_proxy_method(bool)
    __bool__ = make_proxy_method(bool)

    def __setattr__(self, name, value):
        if hasattr(type(self), name):
            self.__dict__[name] = value
        else:
            setattr(self.__wrapped__, name, value)

    def __getattr__(self, name):
        if name in ('__wrapped__', '__factory__'):
            raise AttributeError(name)
        else:
            return getattr(self.__wrapped__, name)

    def __delattr__(self, name):
        if hasattr(type(self), name):
            del self.__dict__[name]
        else:
            delattr(self.__wrapped__, name)

    __add__ = make_proxy_method(operator.add)
    __sub__ = make_proxy_method(operator.sub)
    __mul__ = make_proxy_method(operator.mul)
    __div__ = make_proxy_method(operator.div if PY2 else operator.truediv)
    __truediv__ = make_proxy_method(operator.truediv)
    __floordiv__ = make_proxy_method(operator.floordiv)
    __mod__ = make_proxy_method(operator.mod)
    __divmod__ = make_proxy_method(divmod)
    __pow__ = make_proxy_method(pow)
    __lshift__ = make_proxy_method(operator.lshift)
    __rshift__ = make_proxy_method(operator.rshift)
    __and__ = make_proxy_method(operator.and_)
    __xor__ = make_proxy_method(operator.xor)
    __or__ = make_proxy_method(operator.or_)

    def __radd__(self, other):
        return other + self.__wrapped__

    def __rsub__(self, other):
        return other - self.__wrapped__

    def __rmul__(self, other):
        return other * self.__wrapped__

    def __rdiv__(self, other):
        return operator.div(other, self.__wrapped__)

    def __rtruediv__(self, other):
        return operator.truediv(other, self.__wrapped__)

    def __rfloordiv__(self, other):
        return other // self.__wrapped__

    def __rmod__(self, other):
        return other % self.__wrapped__

    def __rdivmod__(self, other):
        return divmod(other, self.__wrapped__)

    def __rpow__(self, other, *args):
        return pow(other, self.__wrapped__, *args)

    def __rlshift__(self, other):
        return other << self.__wrapped__

    def __rrshift__(self, other):
        return other >> self.__wrapped__

    def __rand__(self, other):
        return other & self.__wrapped__

    def __rxor__(self, other):
        return other ^ self.__wrapped__

    def __ror__(self, other):
        return other | self.__wrapped__

    __iadd__ = make_proxy_method(operator.iadd)
    __isub__ = make_proxy_method(operator.isub)
    __imul__ = make_proxy_method(operator.imul)
    __idiv__ = make_proxy_method(operator.idiv if PY2 else operator.itruediv)
    __itruediv__ = make_proxy_method(operator.itruediv)
    __ifloordiv__ = make_proxy_method(operator.ifloordiv)
    __imod__ = make_proxy_method(operator.imod)
    __ipow__ = make_proxy_method(operator.ipow)
    __ilshift__ = make_proxy_method(operator.ilshift)
    __irshift__ = make_proxy_method(operator.irshift)
    __iand__ = make_proxy_method(operator.iand)
    __ixor__ = make_proxy_method(operator.ixor)
    __ior__ = make_proxy_method(operator.ior)
    __neg__ = make_proxy_method(operator.neg)
    __pos__ = make_proxy_method(operator.pos)
    __abs__ = make_proxy_method(operator.abs)
    __invert__ = make_proxy_method(operator.invert)

    __int__ = make_proxy_method(int)

    if PY2:
        __long__ = make_proxy_method(long)  # noqa

    __float__ = make_proxy_method(float)
    __oct__ = make_proxy_method(oct)
    __hex__ = make_proxy_method(hex)
    __index__ = make_proxy_method(operator.index)
    __len__ = make_proxy_method(len)
    __contains__ = make_proxy_method(operator.contains)
    __getitem__ = make_proxy_method(operator.getitem)
    __setitem__ = make_proxy_method(operator.setitem)
    __delitem__ = make_proxy_method(operator.delitem)

    if PY2:
        __getslice__ = make_proxy_method(operator.getslice)
        __setslice__ = make_proxy_method(operator.setslice)
        __delslice__ = make_proxy_method(operator.delslice)

    def __enter__(self):
        return self.__wrapped__.__enter__()

    def __exit__(self, *args, **kwargs):
        return self.__wrapped__.__exit__(*args, **kwargs)

    __iter__ = make_proxy_method(iter)

    def __call__(self, *args, **kwargs):
        return self.__wrapped__(*args, **kwargs)

    def __reduce__(self):
        return identity, (self.__wrapped__,)

    def __reduce_ex__(self, protocol):
        return identity, (self.__wrapped__,)