def test_makeCopy(self): """ L{util.padTo} doesn't modify the input list but makes a copy. """ items = [] util.padTo(4, items) self.assertEqual([], items)
def _gather_flags(self): """ Gather up boolean (flag) options. """ longOpt, shortOpt = [], '' docs, settings, synonyms, dispatch = {}, {}, {}, {} flags = [] reflect.accumulateClassList(self.__class__, 'optFlags', flags) for flag in flags: long, short, doc = util.padTo(3, flag) if not long: raise ValueError("A flag cannot be without a name.") docs[long] = doc settings[long] = 0 if short: shortOpt = shortOpt + short synonyms[short] = long longOpt.append(long) synonyms[long] = long dispatch[long] = self._generic_flag return longOpt, shortOpt, docs, settings, synonyms, dispatch
def test_padNonEmptyList(self): """ A list which already has some items has the padding value added after those items. """ padded = util.padTo(3, [1, 2], "z") self.assertEqual([1, 2, "z"], padded)
def test_default(self): """ L{None} values can be added to a list to cause it to have a certain length. """ padded = util.padTo(3, []) self.assertEqual([None] * 3, padded)
def test_specificDefaultValue(self): """ A specific value can be added to a list to cause it to have a certain length. """ padded = util.padTo(4, [], "x") self.assertEqual(["x"] * 4, padded)
def _gather_parameters(self): """ Gather options which take a value. """ longOpt, shortOpt = [], '' docs, settings, synonyms, dispatch = {}, {}, {}, {} parameters = [] reflect.accumulateClassList(self.__class__, 'optParameters', parameters) synonyms = {} for parameter in parameters: long, short, default, doc, paramType = util.padTo(5, parameter) if not long: raise ValueError("A parameter cannot be without a name.") docs[long] = doc settings[long] = default if short: shortOpt = shortOpt + short + ':' synonyms[short] = long longOpt.append(long + '=') synonyms[long] = long if paramType is not None: dispatch[long] = CoerceParameter(self, paramType) else: dispatch[long] = CoerceParameter(self, str) return longOpt, shortOpt, docs, settings, synonyms, dispatch
def test_default(self): """ C{None} values can be added to a list to cause it to have a certain length. """ padded = util.padTo(3, []) self.assertEqual([None] * 3, padded)
def test_alreadyPaddedCopies(self): """ If the list is already the length indicated by the padding argument then the return value is a copy of the input. """ items = [1, 2] padded = util.padTo(len(items), items) self.assertIsNot(padded, items)
def test_alreadyPadded(self): """ If the list is already the length indicated by the padding argument then a list with the same value is returned. """ items = [1, 2] padded = util.padTo(len(items), items) self.assertEqual(items, padded)
def htmlFor_menu(write, name, value, allowMultiple=False): "Value of the format [(optionName, displayName[, selected]), ...]" write(' <select NAME="%s"%s>\n' % (name, (allowMultiple and " multiple") or '')) for v in value: optionName, displayName, selected = util.padTo(3, v) selected = (selected and " selected") or '' write(' <option VALUE="%s"%s>%s</option>\n' % (optionName, selected, displayName)) if not value: write(' <option VALUE=""></option>\n') write(" </select>\n")
def updateDestinguishedName(self, subject): DN = {} parameters = [] reflect.accumulateClassList(self.__class__, 'optParameters', parameters) for parameter in parameters: key, short, val, doc, _type = util.padTo(5, parameter) if self.opts[key]: val = _type and _type(self.opts[key]) or self.opts[key] elif self.defaults[key]: val = _type and _type(self.defaults[key]) or self.defaults[key] if key == 'years': val = 60 * 60 * 24 * 365 * val if val and key in self.x509names: try: setattr(subject, self.x509names.get(key), val.strip()) except crypto.Error, err: raise SysExit("Setting value of '%s' failed: %s", key, err[0][0][2]) DN[self.x509names.get(key)] = val.strip()
def _set_defaults(self, parser, subCommands): parser_defaults = parser.defaults() for name, sname, options, doc in subCommands: if hasattr(options, 'optParameters'): parameters = [] instance = options().__class__ reflect.accumulateClassList(instance, 'optParameters', parameters) for idx, parameter in enumerate(parameters): long, short, default, doc, type = util.padTo(5, parameter) _def = parser_defaults.get(long, default) if parser.has_option(name, long): _def = parser.get(name, long, _def) if _def != default: option = [long, short, type and type(_def) or _def, doc] if type: option.append(type) parameters[idx] = option # Override class defaults with config-file defaults options.optParameters = parameters if hasattr(options, "subCommands"): self._set_defaults(parser, options.subCommands)
def __init__(self, cmd_name, options, file): """ @type cmd_name: C{str} @param cmd_name: The name of the command @type options: C{twisted.usage.Options} @param options: The C{twisted.usage.Options} instance defined for this command @type file: C{file} @param file: The C{file} to write the completion function to """ self.cmd_name = cmd_name self.options = options self.file = file self.altArgDescr = {} self.actionDescr = {} self.multiUse = [] self.mutuallyExclusive = [] self.actions = {} self.extras = [] aCL = reflect.accumulateClassList aCD = reflect.accumulateClassDict aCD(options.__class__, 'zsh_altArgDescr', self.altArgDescr) aCD(options.__class__, 'zsh_actionDescr', self.actionDescr) aCL(options.__class__, 'zsh_multiUse', self.multiUse) aCL(options.__class__, 'zsh_mutuallyExclusive', self.mutuallyExclusive) aCD(options.__class__, 'zsh_actions', self.actions) aCL(options.__class__, 'zsh_extras', self.extras) optFlags = [] optParams = [] aCL(options.__class__, 'optFlags', optFlags) aCL(options.__class__, 'optParameters', optParams) for i, optList in enumerate(optFlags): if len(optList) != 3: optFlags[i] = util.padTo(3, optList) for i, optList in enumerate(optParams): if len(optList) != 4: optParams[i] = util.padTo(4, optList) self.optFlags = optFlags self.optParams = optParams optParams_d = {} for optList in optParams: optParams_d[optList[0]] = optList[1:] self.optParams_d = optParams_d optFlags_d = {} for optList in optFlags: optFlags_d[optList[0]] = optList[1:] self.optFlags_d = optFlags_d optAll_d = {} optAll_d.update(optParams_d) optAll_d.update(optFlags_d) self.optAll_d = optAll_d self.addAdditionalOptions() # makes sure none of the zsh_ data structures reference option # names that don't exist. (great for catching typos) self.verifyZshNames() self.excludes = self.makeExcludesDict()
def __init__(self, options, cmdName, file): self.options = options self.cmdName = cmdName self.file = file self.descriptions = {} self.multiUse = set() self.mutuallyExclusive = [] self.optActions = {} self.extraActions = [] for cls in reversed(inspect.getmro(options.__class__)): data = getattr(cls, 'compData', None) if data: self.descriptions.update(data.descriptions) self.optActions.update(data.optActions) self.multiUse.update(data.multiUse) self.mutuallyExclusive.extend(data.mutuallyExclusive) # I don't see any sane way to aggregate extraActions, so just # take the one at the top of the MRO (nearest the `options' # instance). if data.extraActions: self.extraActions = data.extraActions aCL = reflect.accumulateClassList optFlags = [] optParams = [] aCL(options.__class__, 'optFlags', optFlags) aCL(options.__class__, 'optParameters', optParams) for i, optList in enumerate(optFlags): if len(optList) != 3: optFlags[i] = util.padTo(3, optList) for i, optList in enumerate(optParams): if len(optList) != 5: optParams[i] = util.padTo(5, optList) self.optFlags = optFlags self.optParams = optParams paramNameToDefinition = {} for optList in optParams: paramNameToDefinition[optList[0]] = optList[1:] self.paramNameToDefinition = paramNameToDefinition flagNameToDefinition = {} for optList in optFlags: flagNameToDefinition[optList[0]] = optList[1:] self.flagNameToDefinition = flagNameToDefinition allOptionsNameToDefinition = {} allOptionsNameToDefinition.update(paramNameToDefinition) allOptionsNameToDefinition.update(flagNameToDefinition) self.allOptionsNameToDefinition = allOptionsNameToDefinition self.addAdditionalOptions() # makes sure none of the Completions metadata references # option names that don't exist. (great for catching typos) self.verifyZshNames() self.excludes = self.makeExcludesDict()
def __init__(self, options, cmdName, file): self.options = options self.cmdName = cmdName self.file = file self.descriptions = {} self.multiUse = set() self.mutuallyExclusive = [] self.optActions = {} self.extraActions = [] for cls in reversed(inspect.getmro(options.__class__)): data = getattr(cls, 'compData', None) if data: self.descriptions.update(data.descriptions) self.optActions.update(data.optActions) self.multiUse.update(data.multiUse) self.mutuallyExclusive.extend(data.mutuallyExclusive) # I don't see any sane way to aggregate extraActions, so just # take the one at the top of the MRO (nearest the `options' # instance). if data.extraActions: self.extraActions = data.extraActions aCL = reflect.accumulateClassList aCD = reflect.accumulateClassDict optFlags = [] optParams = [] aCL(options.__class__, 'optFlags', optFlags) aCL(options.__class__, 'optParameters', optParams) for i, optList in enumerate(optFlags): if len(optList) != 3: optFlags[i] = util.padTo(3, optList) for i, optList in enumerate(optParams): if len(optList) != 5: optParams[i] = util.padTo(5, optList) self.optFlags = optFlags self.optParams = optParams paramNameToDefinition = {} for optList in optParams: paramNameToDefinition[optList[0]] = optList[1:] self.paramNameToDefinition = paramNameToDefinition flagNameToDefinition = {} for optList in optFlags: flagNameToDefinition[optList[0]] = optList[1:] self.flagNameToDefinition = flagNameToDefinition allOptionsNameToDefinition = {} allOptionsNameToDefinition.update(paramNameToDefinition) allOptionsNameToDefinition.update(flagNameToDefinition) self.allOptionsNameToDefinition = allOptionsNameToDefinition self.addAdditionalOptions() # makes sure none of the Completions metadata references # option names that don't exist. (great for catching typos) self.verifyZshNames() self.excludes = self.makeExcludesDict()
def __init__(self, cmd_name, options, file): """write the zsh completion code to the given file""" self.cmd_name = cmd_name self.options = options self.file = file self.altArgDescr = {} self.actionDescr = {} self.multiUse = [] self.mutuallyExclusive = [] self.actions = {} self.extras = [] aCL = reflect.accumulateClassList aCD = reflect.accumulateClassDict aCD(options.__class__, 'zsh_altArgDescr', self.altArgDescr) aCD(options.__class__, 'zsh_actionDescr', self.actionDescr) aCL(options.__class__, 'zsh_multiUse', self.multiUse) aCL(options.__class__, 'zsh_mutuallyExclusive', self.mutuallyExclusive) aCD(options.__class__, 'zsh_actions', self.actions) aCL(options.__class__, 'zsh_extras', self.extras) optFlags = [] optParams = [] aCL(options.__class__, 'optFlags', optFlags) # optFlags = getattr(options, 'optFlags', []) aCL(options.__class__, 'optParameters', optParams) # optParams = getattr(options, 'optParameters', []) # for l in optFlags: # print l # for l in optParams: # print l for i, optList in enumerate(optFlags): if len(optList) != 3: optFlags[i] = util.padTo(3, optList) for i, optList in enumerate(optParams): if len(optList) != 4: optParams[i] = util.padTo(4, optList) self.optFlags = optFlags self.optParams = optParams optParams_d = {} for optList in optParams: optParams_d[optList[0]] = optList[1:] self.optParams_d = optParams_d optFlags_d = {} for optList in optFlags: optFlags_d[optList[0]] = optList[1:] self.optFlags_d = optFlags_d optAll_d = {} optAll_d.update(optParams_d) optAll_d.update(optFlags_d) self.optAll_d = optAll_d self.addAdditionalOptions() # makes sure none of the zsh_ data structures reference option names that # don't exist. (great for catching typos) self.verifyZshNames() self.excludes = self.makeExcludesDict()
def __init__(self, cmd_name, optionsClass, file): """write the zsh completion code to the given file""" self.cmd_name = cmd_name self.optionsClass = optionsClass self.file = file self.altArgDescr = {} self.actionDescr = {} self.multiUse = [] self.mutuallyExclusive = [] self.actions = {} self.extras = [] aCL = reflect.accumulateClassList aCD = reflect.accumulateClassDict aCD(optionsClass, 'zsh_altArgDescr', self.altArgDescr) aCD(optionsClass, 'zsh_actionDescr', self.actionDescr) aCL(optionsClass, 'zsh_multiUse', self.multiUse) aCL(optionsClass, 'zsh_mutuallyExclusive', self.mutuallyExclusive) aCD(optionsClass, 'zsh_actions', self.actions) aCL(optionsClass, 'zsh_extras', self.extras) optFlags = [] optParams = [] aCL(optionsClass, 'optFlags', optFlags) # optFlags = getattr(optionsClass, 'optFlags', []) aCL(optionsClass, 'optParameters', optParams) # optParams = getattr(optionsClass, 'optParameters', []) # for l in optFlags: # print l # for l in optParams: # print l for i, optList in enumerate(optFlags): if len(optList) != 3: optFlags[i] = util.padTo(3, optList) for i, optList in enumerate(optParams): if len(optList) != 4: optParams[i] = util.padTo(4, optList) self.optFlags = optFlags self.optParams = optParams optParams_d = {} for optList in optParams: optParams_d[optList[0]] = optList[1:] self.optParams_d = optParams_d optFlags_d = {} for optList in optFlags: optFlags_d[optList[0]] = optList[1:] self.optFlags_d = optFlags_d optAll_d = {} optAll_d.update(optParams_d) optAll_d.update(optFlags_d) self.optAll_d = optAll_d self.addAdditionalOptions() # makes sure none of the zsh_ data structures reference option names that # don't exist. (great for catching typos) self.verifyZshNames() self.excludes = self.makeExcludesDict()