def autoRegister(self, obj): from twisted.python import reflect d = {} reflect.accumulateMethods(obj, d, self.prefix) for k, v in d.items(): self.registerHandler(k, v)
def test_prefix(self): """ If a prefix is given, L{accumulateMethods} limits its results to methods beginning with that prefix. Keys in the resulting dictionary also have the prefix removed from them. """ x = Separate() output = {} accumulateMethods(x, output, 'good_') self.assertEqual({'method': x.good_method}, output)
def test_ownClass(self): """ If x is and instance of Base and Base defines a method named method, L{accumulateMethods} adds an item to the given dictionary with C{"method"} as the key and a bound method object for Base.method value. """ x = Base() output = {} accumulateMethods(x, output) self.assertEqual({"method": x.method}, output)
def addAdditionalOptions(self): """ Add additional options to the optFlags and optParams lists. These will be defined by 'opt_foo' methods of the Options subclass @return: C{None} """ methodsDict = {} reflect.accumulateMethods(self.options, methodsDict, 'opt_') methodToShort = {} for name in methodsDict.copy(): if len(name) == 1: methodToShort[methodsDict[name]] = name del methodsDict[name] for methodName, methodObj in methodsDict.items(): longname = methodName.replace('_', '-') # t.p.usage does this # if this option is already defined by the optFlags or # optParameters then we don't want to override that data if longname in self.allOptionsNameToDefinition: continue descr = self.getDescription(longname) short = None if methodObj in methodToShort: short = methodToShort[methodObj] reqArgs = methodObj.im_func.func_code.co_argcount if reqArgs == 2: self.optParams.append([longname, short, None, descr]) self.paramNameToDefinition[longname] = [short, None, descr] self.allOptionsNameToDefinition[longname] = [ short, None, descr ] else: # reqArgs must equal 1. self.options would have failed # to instantiate if it had opt_ methods with bad signatures. self.optFlags.append([longname, short, descr]) self.flagNameToDefinition[longname] = [short, descr] self.allOptionsNameToDefinition[longname] = [ short, None, descr ]
def addAdditionalOptions(self): """ Add additional options to the optFlags and optParams lists. These will be defined by 'opt_foo' methods of the Options subclass @return: C{None} """ methodsDict = {} reflect.accumulateMethods(self.options, methodsDict, 'opt_') methodToShort = {} for name in methodsDict.copy(): if len(name) == 1: methodToShort[methodsDict[name]] = name del methodsDict[name] for methodName, methodObj in methodsDict.items(): long = methodName.replace('_', '-') # t.p.usage does this # if this option is already defined by the optFlags or # optParameters then we don't want to override that data if long in self.optAll_d: continue descr = self.getDescription(long) short = None if methodObj in methodToShort: short = methodToShort[methodObj] reqArgs = methodObj.im_func.func_code.co_argcount if reqArgs == 2: self.optParams.append([long, short, None, descr]) self.optParams_d[long] = [short, None, descr] self.optAll_d[long] = [short, None, descr] elif reqArgs == 1: self.optFlags.append([long, short, descr]) self.optFlags_d[long] = [short, descr] self.optAll_d[long] = [short, None, descr] else: raise TypeError, '%r has wrong number ' \ 'of arguments' % (methodObj,)
def addAdditionalOptions(self): """ Add additional options to the optFlags and optParams lists. These will be defined by 'opt_foo' methods of the Options subclass @return: L{None} """ methodsDict = {} reflect.accumulateMethods(self.options, methodsDict, 'opt_') methodToShort = {} for name in methodsDict.copy(): if len(name) == 1: methodToShort[methodsDict[name]] = name del methodsDict[name] for methodName, methodObj in methodsDict.items(): longname = methodName.replace('_', '-') # t.p.usage does this # if this option is already defined by the optFlags or # optParameters then we don't want to override that data if longname in self.allOptionsNameToDefinition: continue descr = self.getDescription(longname) short = None if methodObj in methodToShort: short = methodToShort[methodObj] reqArgs = methodObj.__func__.__code__.co_argcount if reqArgs == 2: self.optParams.append([longname, short, None, descr]) self.paramNameToDefinition[longname] = [short, None, descr] self.allOptionsNameToDefinition[longname] = [short, None, descr] else: # reqArgs must equal 1. self.options would have failed # to instantiate if it had opt_ methods with bad signatures. self.optFlags.append([longname, short, descr]) self.flagNameToDefinition[longname] = [short, descr] self.allOptionsNameToDefinition[longname] = [short, None, descr]
def addAdditionalOptions(self): """Add additional options to the optFlags and optParams lists. These will be defined by 'opt_foo' methods of the Options subclass""" methodsDict = {} reflect.accumulateMethods(self.optionsClass, methodsDict, 'opt_', curClass=self.optionsClass) # we need to go through methodsDict and figure out is there are such things as # opt_debug and opt_b that point to the same method (long and short option names methodToShort = {} for name in methodsDict.copy(): if len(name) == 1: methodToShort[methodsDict[name]] = name del methodsDict[name] for methodName, methodObj in methodsDict.items(): long = methodName.replace('_', '-') # t.p.usage does this # if this option is already defined by the optFlags or # optParameters then we don't want to override that data if long in self.optAll_d: continue descr = self.getDescription(long) short = None if methodObj in methodToShort: short = methodToShort[methodObj] reqArgs = methodObj.im_func.func_code.co_argcount if reqArgs == 2: self.optParams.append([long, short, None, descr]) self.optParams_d[long] = [short, None, descr] self.optAll_d[long] = [short, None, descr] elif reqArgs == 1: self.optFlags.append([long, short, descr]) self.optFlags_d[long] = [short, descr] self.optAll_d[long] = [short, None, descr] else: raise SystemExit, 'opt_ method has wrong number of arguments'
def addAdditionalOptions(self): """Add additional options to the optFlags and optParams lists. These will be defined by 'opt_foo' methods of the Options subclass""" methodsDict = {} reflect.accumulateMethods(self.options, methodsDict, 'opt_') # we need to go through methodsDict and figure out is there are such things as # opt_debug and opt_b that point to the same method (long and short option names methodToShort = {} for name in methodsDict.copy(): if len(name) == 1: methodToShort[methodsDict[name]] = name del methodsDict[name] for methodName, methodObj in methodsDict.items(): long = methodName.replace('_', '-') # t.p.usage does this # if this option is already defined by the optFlags or # optParameters then we don't want to override that data if long in self.optAll_d: continue descr = self.getDescription(long) short = None if methodObj in methodToShort: short = methodToShort[methodObj] reqArgs = methodObj.im_func.func_code.co_argcount if reqArgs == 2: self.optParams.append([long, short, None, descr]) self.optParams_d[long] = [short, None, descr] self.optAll_d[long] = [short, None, descr] elif reqArgs == 1: self.optFlags.append([long, short, descr]) self.optFlags_d[long] = [short, descr] self.optAll_d[long] = [short, None, descr] else: raise SystemExit, 'opt_ method has wrong number of arguments'
def _getAsserts(self): dct = {} accumulateMethods(self, dct, 'assert') return [ dct[k] for k in dct if not k.startswith('Not') and k != '_' ]
def _getAsserts(self): dct = {} accumulateMethods(self, dct, "assert") return [dct[k] for k in dct if not k.startswith("Not") and k != "_"]
def register(self, obj, check_exists=False): d = {} reflect.accumulateMethods(obj, d, self.prefix) for k, v in d.items(): self.sub(k, v, check_exists=check_exists)
def register(self, obj): d = {} reflect.accumulateMethods(obj, d, self.prefix) for k, v in d.items(): self.sub(k, v)
def register(self, obj): d = {} reflect.accumulateMethods(obj, d, self.prefix) for k,v in d.items(): self.sub(k, v)