def listof(value_type): """List of values.""" def result_type(value): return list(map(value_type, value.split(','))) result_type.__name__ = "listof({type})".format(type=getname(value_type)) return result_type
def __init__(self, option, value): pattern = "Invalid value for option '{name}' of type '{type}': '{value}'" error_message = pattern.format(name=option.name, type=getname(option.type), value=value) super(InvalidOptionValue, self).__init__(error_message) self.option = option self.value = value
def __init__(self, argument, value): pattern = "Invalid value for argument '{name}' of type '{type}': '{value}'" error_message = pattern.format(name=argument.name, type=getname(argument.type), value=value) super(InvalidArgumentValue, self).__init__(error_message) self.argument = argument self.value = value
def __getattr__(self, item): """Delegate attribute access to items contained by the underlying dict.""" if hasattr(self._target, item): return getattr(self._target, item) if item in self._target: return self._target[item] error_pattern = "'{type}' object has no attribute '{name}'" error_message = error_pattern.format(type=getname(type(self._target)), name=item) raise AttributeError(error_message)
def _unsupport(self, *names): for name in names: pattern = "'{type}' does not support method '{name}'" message = pattern.format(type=getname(type(self)), name=name) def stub(*_args, **_kwargs): raise TypeError(message) stub.__name__ = name self._set(name, stub)
def __init__(self, name=None): """Initialize instance. Among other things this constructor will walk through all existing methods and collect those marked as cli-commands. """ self._name = name or getname(type(self)).lower() self._declared_commands = {} self._declared_options = {} self._short_options = set() self._brief, self._descr = self._describe() self._discover_commands()
def summarize_option(self, option): """Get option summary.""" paragraphs = [] descr_paragraphs = self.paragraphs(self.dedent(option.descr)) pattern = self.option_pattern(option) synopsis = pattern.format(long=option.name, short=option.short, type=getname(option.type)) first_paragraph_text = "{synopsis}\n{text}".format( synopsis=synopsis, text=next(iter(descr_paragraphs)).text) first_paragraph = Paragraph(first_paragraph_text, initial_indent='', subsequent_indent=self._indent_unit) paragraphs.append(first_paragraph) for paragraph in descr_paragraphs: paragraph.indent(self._indent_unit) paragraphs.append(paragraph) return paragraphs
def test_listof_name(self): list_of_int = cli.listof(int) self.assertEqual(getname(list_of_int), 'listof(int)')
def test_choice_name(self): valid = ('first', 'second') choice = cli.choice(*valid) self.assertEqual(getname(choice), '|'.join(valid))
def test_getname_value(self): def func(): pass self.assertEqual(getname(func), 'func')