Пример #1
0
    def __init__(self, *anon, **mval): #{{{
        prop, values = self.p, self.v
        if not isinstance(prop, MultiValueProperties):
            raise TypeError("%s object is not a valid properties object" %prop.__class__.__name__)
        elif not isinstance(values, MultiValueValues):
            raise TypeError("%s object is not a valid values object" %values.__class__.__name__)

        if not anon and not mval:
            raise ValueError("Cannot create empty mval")
        block = ['__get__', '__getitem__']
        reserved = [n for n in mval if n in block + dir(object)]
        if reserved:
            raise AttributeError("The following attribute names are reserved: %s" %', '.join(reserved))
        tfunc, eqfunc = self._mktfunc(), self._mkeqfunc()
        if [a for a in (tfunc, eqfunc) if not iscallable(a)]:
            raise TypeError("Attempt to set transform or equals function to a non-callable")
        anon = ((k, v) for k, v in enumerate(anon))
        mval = mdict(anon, mval)
        mval.clearmerged()
#        object.__setattr__(self, '_mval', mval)
#        object.__setattr__(self, '_tfunc', tfunc)
#        object.__setattr__(self, '_eqfunc', eqfunc)
        osetattr(self, '_mval', mval)
        osetattr(self, '_tfunc', tfunc)
        osetattr(self, '_eqfunc', eqfunc)
Пример #2
0
 def __init__(self, *anon, **mval): #{{{
     super(EvalMixin, self).__init__(*anon, **mval)
     mval = self._mval
     for v in mval.itervalues():
         if not iscallable(v):
             mval.clear()
             raise TypeError("'%s' object is not callable" %v.__class__.__name__)
     object.__setattr__(self, '_eqfunc', lambda s, o: bool(s(o)))
Пример #3
0
    def _getset_eqfunc(self, *args): #{{{
        mval = self._mval()
        if not args:
            func = mval._eqfunc
            return func
        func = args[0]
        if not iscallable(func):
            raise TypeError("Equals function is not callable")
#        object.__setattr__(mval, '_eqfunc', func)
        osetattr(mval, '_eqfunc', func)
Пример #4
0
    def _getset_transformer(self, *args): #{{{
        mval = self._mval()
        if not args:
            func = mval._tfunc
            return func
        func = args[0]
        if not iscallable(func):
            raise TypeError("Transform function is not callable")
#        object.__setattr__(mval, '_tfunc', func)
        osetattr(mval, '_tfunc', func)
Пример #5
0
 def _anyall_func(self, args): #{{{
     f = None
     if not args:
         f = lambda v: bool(v)
     else:
         if len(args) > 1:
             raise TypeError('any() takes at most 1 argument (%i given)'  %len(args))
         f = func = args[0]
         if iscallable(func):
             f = lambda v: bool(func(v))
         else:
             raise TypeError("Compare function must be callable")
     return f
Пример #6
0
    def __new__(cls, classname, bases, clsdict): #{{{
        if not isbasemetaclass(bases, cls):
            return super(MetaProxy, cls).__new__(cls, classname, bases, clsdict)
        proxyattr = clsdict.pop('proxyattr')
        curobj = proxyattr.proxyobj
        retproxy = proxyattr.return_proxy
        if not isinstance(retproxy, tuple):
            retproxy = tuple(retproxy)
        can_weakref = proxyattr.weakref
        clsproxy = isclass(curobj)
        proxyinst = None
        if clsproxy:
            proxyinst = {}

        # Set setattr
        temp_attr = {}
        def __setattr__(self, name, val): #{{{
            try:
                obj = _ga(self, '__proxyinst__')
            except KeyError:
                obj = curobj
            if clsproxy and obj is curobj:
                sid = id(self)
                vars = temp_attr.setdefault(sid, {})
                vars[name] = val
            else:
                try:
                    setattr(obj, name, val)
                except AttributeError:
                    _sa(self, name, val)
        # End def #}}}
        def __delattr__(self, name): #{{{
            try:
                obj = _ga(self, '__proxyinst__')
            except KeyError:
                obj = curobj
            if clsproxy and obj is curobj:
                sid = id(self)
                vars = temp_attr.setdefault(sid, {})
                try:
                    vars.pop(name)
                except KeyError:
                    raise AttributeError()
            else:
                try:
                    delattr(obj, name)
                except AttributeError:
                    if name in dir(self):
                        _da(self, name)
                    else:
                        raise
        # End def #}}}
        clsdict['__setattr__'] = __setattr__
        clsdict['__delattr__'] = __delattr__

        instblock = ('__new__', '__init__', '__getattribute__', '__setattr__', 
                        '__delattr__')
        instblock_retproxy = instblock + retproxy
        block = ('__class__', '__slots__', '__proxyinst__') + instblock

        # Set getattribute
        def __getattribute__(self, name): #{{{
            sid = id(self)
            if proxyinst is None:
                obj = curobj
            else:
                obj = proxyinst.get(sid, curobj)
#            obj = _ga(self, '__proxyinst__')
            if clsproxy:
                if obj is curobj:
                    vars = temp_attr.setdefault(sid, {})
                    if name in vars:
                        return vars[name]
            if name == '__class__' and not clsproxy:
                return _ga(obj, name)
            try:
                ret = _ga(self, name)
            except AttributeError:
                ret = _ga(obj, name)
            return ret
        # End def #}}}
        clsdict['__getattribute__'] = __getattribute__


        clsdict['__proxyinst__'] = property(lambda s: curobj)
        if clsproxy:
#            proxyinst = {}
            def __init__(self, *args, **kwargs): #{{{
                sid = id(self)
                proxyinst[sid] = inst = curobj(*args, **kwargs)
                vars = temp_attr.setdefault(sid, {})
                pi = vars.popitem
                while vars:
                    setattr(self, *pi())
                temp_attr.pop(sid)
            # End def #}}}
            clsdict['__init__'] = __init__
            clsdict['__proxyinst__'] = property(lambda s: proxyinst[id(s)])
        if can_weakref:
            objslots = getattr(curobj, '__slots__', None)
            curslots = clsdict['__slots__']
            if not isbuiltin(curobj) and (objslots is None or '__weakref__' in objslots):
                clsdict['__slots__'] += curslots + ('__weakref__',)
        attrblock = block if clsproxy else instblock
        for n in dir(curobj):
            if n in attrblock:
                continue
            attr = getattr(curobj, n)
            if iscallable(attr) and not isclass(attr):
                res = None
                if isclassmethod(attr):
                    res = _mkproxycallable(attr, n, clsproxy, retproxy, classmethod)
                elif isfunction(attr):
                    res = _mkproxycallable(attr,n, clsproxy, retproxy, staticmethod)
                else:
                    res = _mkproxycallable(attr, n, clsproxy, retproxy, usewraps=False)
                clsdict[n] = res
            else:
                clsdict[n] = attr
        if clsproxy:
            classname = curobj.__name__
        return super(MetaProxy, cls).__new__(cls, classname, bases, clsdict)
Пример #7
0
 def init_query(self, f): #{{{
     if not iscallable(f):
         return (lambda i: i== f)
     else:
         return f