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 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 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 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 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
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