[Subversion] / PEAK / src / peak / util / signature.py  

View of /PEAK/src/peak/util/signature.py

Parent Directory | Revision Log
Revision: 1769 - (download) (as text)
Mon Jul 5 23:41:51 2004 UTC (19 years, 9 months ago) by pje
File size: 1270 byte(s)
Change all adapter factories to use only a single argument.
"""Crude introspection of call signatures"""

import protocols; from protocols import adapt, Interface
from inspect import getargspec
from types import FunctionType, MethodType

__all__ = 'ISignature', 'getPositionalArgs'


class ISignature(Interface):
    # XXX There should be a lot more here than this...

    def getPositionalArgs():
        """Return a sequence of positional argument names"""

    def getCallable():
        """Return the callable object"""


class FunctionAsSignature(protocols.Adapter):

    protocols.advise(
        instancesProvide=[ISignature],
        asAdapterForTypes=[FunctionType]
    )

    def getPositionalArgs(self):
        return getargspec(self.subject)[0]

    def getCallable(self):
        return self.subject










class MethodAsSignature(FunctionAsSignature):

    protocols.advise(
        instancesProvide=[ISignature],
        asAdapterForTypes=[MethodType]
    )

    def __init__(self, ob):
        self.funcSig = adapt(ob.im_func, ISignature)
        self.offset = ob.im_self is not None
        self.subject = ob

    def getPositionalArgs(self):
        return self.funcSig.getPositionalArgs()[self.offset:]


def getPositionalArgs(ob):
    return adapt(ob,ISignature).getPositionalArgs()
























cvs-admin@eby-sarna.com

Powered by ViewCVS 1.0-dev

ViewCVS and CVS Help