def iswrapped(obj): #{{{ if _ism(obj) or _isf(obj): return False elif hasattr(obj, '__call__') and _ism(obj.__call__): return False else: return True
def callableobj(obj): #{{{ if not iscallable(obj): return None if _ism(obj) or _isf(obj): return obj elif hasattr(obj, '__call__') and _ism(obj.__call__): return obj.__call__ else: obj = callable_wrapper(obj) return obj
def subscribe(iobj, target, msgobj=Message, argsobj=Arguments, ftype=None, choosefunc=None): #{{{ _validate_subscribekw(iobj, target, msgobj, argsobj, ftype, choosefunc) i = issue(iobj, msgobj, argsobj) if i is None: if ftype: raise TypeError('Please subscribe an issue first.') i = Signal(target) cobj = callableobj(target) numargs = i.func.numargs if _isf(cobj) and numargs != 1: raise ValueError("'target' argument is a function: it must accept exactly 1 argument; instead got %i" %numargs) elif _ism(cobj) and numargs != 2: raise ValueError("'target' argument is a method: it must accept exactly 2 arguments; instead got %i" %numargs) smsg = _siglist.get(iobj, {}) sargs = smsg.get(msgobj, {}) sargs.update({argsobj: i}) smsg.update({msgobj: sargs}) _siglist.update({iobj: smsg}) elif not ftype or ftype == 'after': i.connect(target) elif ftype == 'before': i.connect(before=[target]) elif ftype == 'around': i.connect(around=[target]) elif ftype == 'onreturn': i.connect(onreturn=[target]) elif ftype == 'choose': i.connect(choose=[(choosefunc, target)]) return i
def methodtype(obj): #{{{ if not _ism(obj): return METHODTYPE_NOTMETHOD elif obj.im_self is None: if obj.im_class is None: return METHODTYPE_UNBOUND return METHODTYPE_CLASS else: return METHODTYPE_INSTANCE
def wrap(self, func): #{{{ if not iscallable(func): raise TypeError('Argument must be a valid callable object') newfunc = func(self._newcall) if not iscallable(newfunc): raise TypeError('Return value of wrapping callable must be a valid callable object') if not _ism(newfunc): newfunc = method(newfunc, self, self.__class__) self._newcall = newfunc
def iscallable(obj): #{{{ if not callable(obj): return False elif _ism(obj) or _isf(obj): return True elif isinstance(obj, type(str.__call__)): return False elif hasattr(obj, '__call__'): return iscallable(obj.__call__) return False
def num_static_args(obj): #{{{ if not iscallable(obj): return -1, None if _ism(obj): obj = obj.im_func if isinstance(obj, _CallableWrapper): return obj._numargs, obj._maxargs isc = isclass(obj) if not _isf(obj) and not isc: obj = obj.__call__ if not _ism(obj): return -1, None argspec = cgetargspec(obj) l, d = len(argspec[0]), argspec[3] max = l if argspec[1]: max = None if d: l -= len(d) if isc: l -= 1 return l, max
def num_static_args(obj): #{{{ if not iscallable(obj): return -1 if _ism(obj): obj = obj.im_func if isinstance(obj, CallableWrapper): return obj._numargs if not _isf(obj): obj = obj.__call__ argspec = getargspec(obj) l, d = len(argspec[0]), argspec[3] if d: l -= len(d) return l