def watchObject(self, object, identifier, callback): """Watch the given object. Whenever I think the object might have changed, I'll send an ObjectLink of it to the callback. The identifier argument is used to generate identifiers for objects which are members of this one. """ if type(object) is not types.InstanceType: raise TypeError, "Sorry, can only place a watch on Instances." dct = {} reflect.addMethodNamesToDict(object.__class__, dct, '') for k in object.__dict__.keys(): dct[k] = 1 members = dct.keys() clazzNS = {} clazz = new.classobj('Watching%s%X' % (object.__class__.__name__, id(object)), (_MonkeysSetattrMixin, object.__class__,), clazzNS) clazzNS['_watchEmitChanged'] = new.instancemethod( lambda slf, i=identifier, b=self, cb=callback: cb(b.browseObject(slf, i)), None, clazz) object.__class__ = clazz for name in members: m = getattr(object, name) if ((type(m) is types.MethodType) and (m.im_self is not None)): monkey = _WatchMonkey(object) monkey.install(name)
def ability_commands(self, sentence): """Usage: commands Commands will print a list of all the various commands and inherent abilities your character has.""" dict = {} reflect.addMethodNamesToDict(self.__class__, dict, "ability_") self.hears("You have the following abilities:") for x in dict.keys(): self.hears(x)
def watchObject(self, object, identifier, callback): """Watch the given object. Whenever I think the object might have changed, I'll send an ObjectLink of it to the callback. The identifier argument is used to generate identifiers for objects which are members of this one. """ if type(object) is not types.InstanceType: raise TypeError, "Sorry, can only place a watch on Instances." # uninstallers = [] dct = {} reflect.addMethodNamesToDict(object.__class__, dct, '') for k in object.__dict__.keys(): dct[k] = 1 members = dct.keys() clazzNS = {} clazz = types.ClassType( 'Watching%s%X' % (object.__class__.__name__, id(object)), ( _MonkeysSetattrMixin, object.__class__, ), clazzNS) clazzNS['_watchEmitChanged'] = types.MethodType( lambda slf, i=identifier, b=self, cb=callback: cb( b.browseObject(slf, i)), None, clazz) # orig_class = object.__class__ object.__class__ = clazz for name in members: m = getattr(object, name) # Only hook bound methods. if ((type(m) is types.MethodType) and (m.im_self is not None)): # What's the use of putting watch monkeys on methods # in addition to __setattr__? Well, um, uh, if the # methods modify their attributes (i.e. add a key to # a dictionary) instead of [re]setting them, then # we wouldn't know about it unless we did this. # (Is that convincing?) monkey = _WatchMonkey(object) monkey.install(name)
def watchObject(self, object, identifier, callback): """Watch the given object. Whenever I think the object might have changed, I'll send an ObjectLink of it to the callback. The identifier argument is used to generate identifiers for objects which are members of this one. """ if type(object) is not types.InstanceType: raise TypeError, "Sorry, can only place a watch on Instances." # uninstallers = [] dct = {} reflect.addMethodNamesToDict(object.__class__, dct, '') for k in object.__dict__.keys(): dct[k] = 1 members = dct.keys() clazzNS = {} clazz = new.classobj('Watching%s%X' % (object.__class__.__name__, id(object)), (_MonkeysSetattrMixin, object.__class__,), clazzNS) clazzNS['_watchEmitChanged'] = new.instancemethod( lambda slf, i=identifier, b=self, cb=callback: cb(b.browseObject(slf, i)), None, clazz) # orig_class = object.__class__ object.__class__ = clazz for name in members: m = getattr(object, name) # Only hook bound methods. if ((type(m) is types.MethodType) and (m.im_self is not None)): # What's the use of putting watch monkeys on methods # in addition to __setattr__? Well, um, uh, if the # methods modify their attributes (i.e. add a key to # a dictionary) instead of [re]setting them, then # we wouldn't know about it unless we did this. # (Is that convincing?) monkey = _WatchMonkey(object) monkey.install(name)
def test_baseClass(self): """ If C{baseClass} is passed to L{addMethodNamesToDict}, only methods which are a subclass of C{baseClass} are added to the result dictionary. """ class Alternate(object): pass class Child(Separate, Alternate): def good_alternate(self): pass result = {} addMethodNamesToDict(Child, result, 'good_', Alternate) self.assertEqual({'alternate': 1}, result)
def test_baseClass(self): """ If C{baseClass} is passed to L{addMethodNamesToDict}, only methods which are a subclass of C{baseClass} are added to the result dictionary. """ class Alternate(object): pass class Child(Separate, Alternate): def good_alternate(self): pass result = {} addMethodNamesToDict(Child, result, 'good_', Alternate) self.assertEqual({'alternate': 1}, result)
def _absorbHandlers(self): twistd_handlers = {} reflect.addMethodNamesToDict(self.__class__, twistd_handlers, "opt_") # NOTE(termie): Much of the following is derived/copied from # twisted.python.usage with the express purpose of # providing compatibility for name in twistd_handlers.keys(): method = getattr(self, 'opt_'+name) takesArg = not usage.flagFunction(method, name) doc = getattr(method, '__doc__', None) if not doc: doc = 'undocumented' if not takesArg: if name not in FLAGS: flags.DEFINE_boolean(name, None, doc) self._flagHandlers[name] = method else: if name not in FLAGS: flags.DEFINE_string(name, None, doc) self._paramHandlers[name] = method
def _gather_handlers(self): """ Gather up options with their own handler methods. """ longOpt, shortOpt = [], '' docs, settings, synonyms, dispatch = {}, {}, {}, {} dct = {} reflect.addMethodNamesToDict(self.__class__, dct, "opt_") for name in dct.keys(): method = getattr(self, 'opt_'+name) takesArg = not flagFunction(method, name) prettyName = name.replace('_', '-') doc = getattr(method, '__doc__', None) if doc: ## Only use the first line. #docs[name] = doc.split('\n')[0] docs[prettyName] = doc else: docs[prettyName] = self.docs.get(prettyName) synonyms[prettyName] = prettyName # A little slight-of-hand here makes dispatching much easier # in parseOptions, as it makes all option-methods have the # same signature. if takesArg: fn = lambda name, value, m=method: m(value) else: # XXX: This won't raise a TypeError if it's called # with a value when it shouldn't be. fn = lambda name, value=None, m=method: m() dispatch[prettyName] = fn if len(name) == 1: shortOpt = shortOpt + name if takesArg: shortOpt = shortOpt + ':' else: if takesArg: prettyName = prettyName + '=' longOpt.append(prettyName) reverse_dct = {} # Map synonyms for name in dct.keys(): method = getattr(self, 'opt_' + name) if method not in reverse_dct: reverse_dct[method] = [] reverse_dct[method].append(name) cmpLength = lambda a, b: cmp(len(a), len(b)) for method, names in reverse_dct.items(): if len(names) < 2: continue names_ = names[:] names_.sort(cmpLength) longest = names_.pop() for name in names_: synonyms[name] = longest return longOpt, shortOpt, docs, settings, synonyms, dispatch
def postOptions(self): postOpt = {} reflect.addMethodNamesToDict(self.__class__, postOpt, "postOptions_") for name in postOpt.keys(): method = getattr(self, 'postOptions_' + name) method()
def _gather_handlers(self): """ Gather up options with their own handler methods. This returns a tuple of many values. Amongst those values is a synonyms dictionary, mapping all of the possible aliases (C{str}) for an option to the longest spelling of that option's name C({str}). Another element is a dispatch dictionary, mapping each user-facing option name (with - substituted for _) to a callable to handle that option. """ longOpt, shortOpt = [], '' docs, settings, synonyms, dispatch = {}, {}, {}, {} dct = {} reflect.addMethodNamesToDict(self.__class__, dct, "opt_") for name in dct.keys(): method = getattr(self, 'opt_' + name) takesArg = not flagFunction(method, name) prettyName = name.replace('_', '-') doc = getattr(method, '__doc__', None) if doc: ## Only use the first line. #docs[name] = doc.split('\n')[0] docs[prettyName] = doc else: docs[prettyName] = self.docs.get(prettyName) synonyms[prettyName] = prettyName # A little slight-of-hand here makes dispatching much easier # in parseOptions, as it makes all option-methods have the # same signature. if takesArg: fn = lambda name, value, m=method: m(value) else: # XXX: This won't raise a TypeError if it's called # with a value when it shouldn't be. fn = lambda name, value=None, m=method: m() dispatch[prettyName] = fn if len(name) == 1: shortOpt = shortOpt + name if takesArg: shortOpt = shortOpt + ':' else: if takesArg: prettyName = prettyName + '=' longOpt.append(prettyName) reverse_dct = {} # Map synonyms for name in dct.keys(): method = getattr(self, 'opt_' + name) if method not in reverse_dct: reverse_dct[method] = [] reverse_dct[method].append(name.replace('_', '-')) for method, names in reverse_dct.items(): if len(names) < 2: continue longest = max(names, key=len) for name in names: synonyms[name] = longest return longOpt, shortOpt, docs, settings, synonyms, dispatch
def _gather_handlers(self): """ Gather up options with their own handler methods. This returns a tuple of many values. Amongst those values is a synonyms dictionary, mapping all of the possible aliases (C{str}) for an option to the longest spelling of that option's name C({str}). Another element is a dispatch dictionary, mapping each user-facing option name (with - substituted for _) to a callable to handle that option. """ longOpt, shortOpt = [], '' docs, settings, synonyms, dispatch = {}, {}, {}, {} dct = {} reflect.addMethodNamesToDict(self.__class__, dct, "opt_") for name in dct.keys(): method = getattr(self, 'opt_'+name) takesArg = not flagFunction(method, name) prettyName = name.replace('_', '-') doc = getattr(method, '__doc__', None) if doc: ## Only use the first line. #docs[name] = doc.split('\n')[0] docs[prettyName] = doc else: docs[prettyName] = self.docs.get(prettyName) synonyms[prettyName] = prettyName # A little slight-of-hand here makes dispatching much easier # in parseOptions, as it makes all option-methods have the # same signature. if takesArg: fn = lambda name, value, m=method: m(value) else: # XXX: This won't raise a TypeError if it's called # with a value when it shouldn't be. fn = lambda name, value=None, m=method: m() dispatch[prettyName] = fn if len(name) == 1: shortOpt = shortOpt + name if takesArg: shortOpt = shortOpt + ':' else: if takesArg: prettyName = prettyName + '=' longOpt.append(prettyName) reverse_dct = {} # Map synonyms for name in dct.keys(): method = getattr(self, 'opt_' + name) if method not in reverse_dct: reverse_dct[method] = [] reverse_dct[method].append(name.replace('_', '-')) for method, names in reverse_dct.items(): if len(names) < 2: continue longest = max(names, key=len) for name in names: synonyms[name] = longest return longOpt, shortOpt, docs, settings, synonyms, dispatch
def postOptions(self): postOpt = {} reflect.addMethodNamesToDict(self.__class__, postOpt, "postOptions_") for name in postOpt.keys(): method = getattr(self, 'postOptions_'+name) method()
def registerAll(self, perspective): dct = {} reflect.addMethodNamesToDict(self.__class__, dct, "event_") for name in dct.keys(): perspective.subscribe(name, self)
def _gather_handlers(self): """ Gather up options with their own handler methods. """ longOpt, shortOpt = [], '' docs, settings, synonyms, dispatch = {}, {}, {}, {} dct = {} reflect.addMethodNamesToDict(self.__class__, dct, "opt_") for name in dct.keys(): method = getattr(self, 'opt_' + name) takesArg = not flagFunction(method, name) prettyName = name.replace('_', '-') doc = getattr(method, '__doc__', None) if doc: ## Only use the first line. #docs[name] = doc.split('\n')[0] docs[prettyName] = doc else: docs[prettyName] = self.docs.get(prettyName) synonyms[prettyName] = prettyName # A little slight-of-hand here makes dispatching much easier # in parseOptions, as it makes all option-methods have the # same signature. if takesArg: fn = lambda name, value, m=method: m(value) else: # XXX: This won't raise a TypeError if it's called # with a value when it shouldn't be. fn = lambda name, value=None, m=method: m() dispatch[prettyName] = fn if len(name) == 1: shortOpt = shortOpt + name if takesArg: shortOpt = shortOpt + ':' else: if takesArg: prettyName = prettyName + '=' longOpt.append(prettyName) reverse_dct = {} # Map synonyms for name in dct.keys(): method = getattr(self, 'opt_' + name) if method not in reverse_dct: reverse_dct[method] = [] reverse_dct[method].append(name) cmpLength = lambda a, b: cmp(len(a), len(b)) for method, names in reverse_dct.items(): if len(names) < 2: continue names_ = names[:] names_.sort(cmpLength) longest = names_.pop() for name in names_: synonyms[name] = longest return longOpt, shortOpt, docs, settings, synonyms, dispatch