Ejemplo n.º 1
0
def _mkGeneric(oldfunc,argname):
    funcname = oldfunc.__name__
    args, varargs, kwargs, defaults = inspect.getargspec(oldfunc)
    if defaults:
        tmpd = ["=__gfDefaults[%s]" % i for i in range(len(defaults))]
    else:
        tmpd = None

    argspec = inspect.formatargspec(
        args, varargs, kwargs, tmpd, formatvalue=lambda x:x)
    outargs = inspect.formatargspec(args,varargs,kwargs)

    protocol = protocols.Protocol()
    d={}
    s= """
def setup(__gfProtocol, __gfDefaults):

    def %(funcname)s%(argspec)s:
         __gfWhat = __gfProtocol(%(argname)s,None)
         if __gfWhat is None:
             raise NoApplicableMethods(%(argname)s)
         else:
             %(argname)s = __gfWhat[0]
             return __gfWhat[1]%(outargs)s


    return %(funcname)s
""" % locals()
    exec(s, globals(),d); func = d['setup'](protocol,defaults)

    def when(cond):
        """Add following function to this GF, using 'cond' as a guard"""
        def callback(frm,name,value,old_locals):
            declarePredicate(cond, protocol, lambda ob: (ob,value))
            if old_locals.get(name) is func:
                return func
            return value
        return decorate_assignment(callback)



    def addMethod(cond,func):
        """Use 'func' when dispatch argument matches 'cond'"""
        declarePredicate(cond, protocol, lambda ob: (ob,func))

    def clone():
        """Return a simple generic function that "inherits" from this one"""
        f = _mkGeneric(oldfunc,argname)
        protocols.declareAdapter(
            protocols.NO_ADAPTER_NEEDED,[f.protocol],forProtocols=[protocol]
        )
        return f

    func.addMethod = addMethod
    func.when      = when
    func.clone     = clone
    func.protocol  = protocol
    func.__doc__   = oldfunc.__doc__
    protocols.adviseObject(func,provides=[IExtensibleFunction])
    return func
Ejemplo n.º 2
0
 def __init__(self, func):
     self.delegate, args = _mkNormalizer(func, self)
     self.delegate.__dict__ = dict([(k, getattr(self, k))
                                    for k in dir(self.__class__)
                                    if not k.startswith('_')])
     self.delegate.__doc__ = self.__doc__ = func.__doc__
     protocols.adviseObject(self.delegate, [IGenericFunction])
     self.__name__ = func.__name__
     self.__call__ = self.delegate
     Dispatcher.__init__(self, args)
Ejemplo n.º 3
0
 def __init__(self,func):
     self.delegate, args = _mkNormalizer(func, self)
     self.delegate.__dict__ = dict(
         [(k,getattr(self,k))
             for k in dir(self.__class__) if not k.startswith('_')]
     )
     self.delegate.__doc__ = self.__doc__ = func.__doc__
     protocols.adviseObject(self.delegate,[IGenericFunction])
     self.__name__ = func.__name__; self.__call__ = self.delegate
     Dispatcher.__init__(self,args)
Ejemplo n.º 4
0
def _mkGeneric(oldfunc, argname):
    funcname = oldfunc.__name__
    args, varargs, kwargs, defaults = inspect.getargspec(oldfunc)
    if defaults:
        tmpd = ["=__gfDefaults[%s]" % i for i in range(len(defaults))]
    else:
        tmpd = None

    argspec = inspect.formatargspec(args,
                                    varargs,
                                    kwargs,
                                    tmpd,
                                    formatvalue=lambda x: x)
    outargs = inspect.formatargspec(args, varargs, kwargs)

    protocol = protocols.Protocol()
    d = {}
    s = """
def setup(__gfProtocol, __gfDefaults):

    def %(funcname)s%(argspec)s:
         __gfWhat = __gfProtocol(%(argname)s,None)
         if __gfWhat is None:
             raise NoApplicableMethods(%(argname)s)
         else:
             %(argname)s = __gfWhat[0]
             return __gfWhat[1]%(outargs)s


    return %(funcname)s
""" % locals()
    exec(s, globals(), d)
    func = d['setup'](protocol, defaults)

    def when(cond):
        """Add following function to this GF, using 'cond' as a guard"""
        def callback(frm, name, value, old_locals):
            declarePredicate(cond, protocol, lambda ob: (ob, value))
            if old_locals.get(name) is func:
                return func
            return value

        return decorate_assignment(callback)

    def addMethod(cond, func):
        """Use 'func' when dispatch argument matches 'cond'"""
        declarePredicate(cond, protocol, lambda ob: (ob, func))

    def clone():
        """Return a simple generic function that "inherits" from this one"""
        f = _mkGeneric(oldfunc, argname)
        protocols.declareAdapter(protocols.NO_ADAPTER_NEEDED, [f.protocol],
                                 forProtocols=[protocol])
        return f

    func.addMethod = addMethod
    func.when = when
    func.clone = clone
    func.protocol = protocol
    func.__doc__ = oldfunc.__doc__
    protocols.adviseObject(func, provides=[IExtensibleFunction])
    return func
Ejemplo n.º 5
0
    is, you can do this::

        myInitCtx = binding.Make(naming.InitialContext)

    in a class to create a 'myInitCtx' attribute.  This can be useful if
    you will be using naming functions a lot and would like to hang onto
    an initial context.
    """

    return spi.getInitialContext(parent, componentName, **options)


from peak.binding.interfaces import IComponentFactory
from protocols import adviseObject

adviseObject(InitialContext, provides=[IComponentFactory])
del IComponentFactory, adviseObject

from peak.api import NOT_GIVEN


def lookup(parent, name, default=NOT_GIVEN, **options):
    """Look up 'name' in the default initial context for 'parent', w/'options'

    This is just a shortcut for calling::

        naming.InitialContext(parent,**options)[name]
    """

    return InitialContext(parent, **options).lookup(name, default)