def print_subcommands_help(cmd): printout = {} for subcmd in cmd.subcommands.values(): spec, sep, print_path = subcmd.path.partition('_') printout[print_path.replace('_', ' ')] = subcmd.help if printout: print('\nOptions:\n - - - -') print_dict(printout)
def _run(self): from kamaki.cli.utils import print_dict, print_list, print_items self.writeln('Test simple dict:\n- - -') print_dict(self.d1) self.writeln('- - -\n') self.writeln('\nTest simple list:\n- - -') print_list(self.l1) self.writeln('- - -\n') self.writeln('\nTest 2-level dict:\n- - -') print_dict(self.d2) self.writeln('- - -\n') self.writeln('\nTest non-trivial list:\n- - -') print_list(self.l2) self.writeln('- - -') self.writeln('\nTest extreme dict:\n- - -') print_dict(self.d3) self.writeln('- - -\n') self.writeln('Test simple enumerated dict:\n- - -') print_dict(self.d1, with_enumeration=True) self.writeln('- - -\n') self.writeln('\nTest simple enumerated list:\n- - -') print_list(self.l1, with_enumeration=True) self.writeln('- - -\n') self.writeln('Test non-trivial deep-enumerated dict:\n- - -') print_dict(self.d2, with_enumeration=True, recursive_enumeration=True) self.writeln('- - -\n') self.writeln('\nTest non-trivial enumerated list:\n- - -') print_list(self.l2, with_enumeration=True) self.writeln('- - -\n') self.writeln('\nTest print_items with id:\n- - -') print_items([ {'id': '42', 'title': 'lalakis 1', 'content': self.d1}, {'id': '142', 'title': 'lalakis 2', 'content': self.d2}]) self.writeln('- - -') self.writeln('\nTest print_items with id and enumeration:\n- - -') print_items( [ {'id': '42', 'title': 'lalakis 1', 'content': self.d1}, {'id': '142', 'title': 'lalakis 2', 'content': self.d2}], with_enumeration=True) self.writeln('- - -') self.writeln('\nTest print_items with id, title, redundancy:\n- - -') print_items( [ {'id': '42', 'title': 'lalakis 1', 'content': self.d1}, {'id': '142', 'title': 'lalakis 2', 'content': self.d2}], title=('id', 'title'), with_redundancy=True) self.writeln('- - -') self.writeln('\nTest print_items with lists- - -') print_items([['i00', 'i01', 'i02'], [self.l2, 'i11', self.d1], 3]) self.writeln('- - -')
def complete_method(self, text, line, begidx, endidx): subcmd, cmd_args = cmd.parse_out(split_input(line)[1:]) if subcmd.is_command: cls = subcmd.cmd_class instance = cls(dict(arguments)) empty, sep, subname = subcmd.path.partition(cmd.path) cmd_name = '%s %s' % (cmd.name, subname.replace('_', ' ')) print('\n%s\nSyntax:\t%s %s' % ( cls.help, cmd_name, cls.syntax)) cmd_args = {} for arg in instance.arguments.values(): cmd_args[','.join(arg.parsed_name)] = arg.help print_dict(cmd_args, indent=2) stdout.write('%s %s' % (self.prompt, line)) return subcmd.subnames()
def complete_method(self, text, line, begidx, endidx): subcmd, cmd_args = cmd.parse_out(split_input(line)[1:]) if subcmd.is_command: cls = subcmd.cmd_class instance = cls(dict(arguments)) empty, sep, subname = subcmd.path.partition(cmd.path) cmd_name = '%s %s' % (cmd.name, subname.replace('_', ' ')) print('\n%s\nSyntax:\t%s %s' % (cls.help, cmd_name, cls.syntax)) cmd_args = {} for arg in instance.arguments.values(): cmd_args[','.join(arg.parsed_name)] = arg.help print_dict(cmd_args, indent=2) stdout.write('%s %s' % (self.prompt, line)) return subcmd.subnames()
def _groups_help(arguments): global _debug global kloger descriptions = {} acceptable_groups = arguments['config'].groups for cmd_group, spec in arguments['config'].cli_specs: pkg = _load_spec_module(spec, arguments, 'namespaces') if pkg: namespaces = getattr(pkg, 'namespaces') try: for cmd_tree in namespaces: if cmd_tree.name in acceptable_groups: descriptions[cmd_tree.name] = cmd_tree.description except TypeError: if _debug: kloger.warning('No cmd description (help) for module %s' % cmd_group) elif _debug: kloger.warning('Loading of %s cmd spec failed' % cmd_group) print('\nOptions:\n - - - -') print_dict(descriptions)
def _groups_help(arguments): global _debug global kloger descriptions = {} acceptable_groups = arguments['config'].groups for cmd_group, spec in arguments['config'].cli_specs: pkg = _load_spec_module(spec, arguments, 'namespaces') if pkg: namespaces = getattr(pkg, 'namespaces') try: for cmd_tree in namespaces: if cmd_tree.name in acceptable_groups: descriptions[cmd_tree.name] = cmd_tree.description except TypeError: if _debug: kloger.warning( 'No cmd description (help) for module %s' % cmd_group) elif _debug: kloger.warning('Loading of %s cmd spec failed' % cmd_group) print('\nOptions:\n - - - -') print_dict(descriptions)
def test_print_dict(self): from kamaki.cli.utils import print_dict, INDENT_TAB out = StringIO() self.assertRaises(AssertionError, print_dict, 'non-dict think') self.assertRaises(AssertionError, print_dict, {}, indent=-10) for args in product( ( {'k1': 'v1'}, {'k1': 'v1', 'k2': 'v2'}, {'k1': 'v1', 'k2': 'v2', 'k3': 'v3'}, {'k1': 'v1', 'k2': {'k1': 'v1', 'k2': 'v2'}, 'k3': 'v3'}, { 'k1': {'k1': 'v1', 'k2': 'v2'}, 'k2': [1, 2, 3], 'k3': 'v3'}, { 'k1': {'k1': 'v1', 'k2': 'v2'}, 'k2': 42, 'k3': {'k1': 1, 'k2': [1, 2, 3]}}, { 'k1': { 'k1': 'v1', 'k2': [1, 2, 3], 'k3': {'k1': [(1, 2)]}}, 'k2': (3, 4, 5), 'k3': {'k1': 1, 'k2': [1, 2, 3]}}), (tuple(), ('k1', ), ('k1', 'k2')), (0, 1, 2, 9), (False, True), (False, True)): d, exclude, indent, with_enumeration, recursive_enumeration = args with patch('kamaki.cli.utils.print_dict') as PD: with patch('kamaki.cli.utils.print_list') as PL: pd_calls, pl_calls = 0, 0 print_dict(*args, out=out) exp_calls = u'' for i, (k, v) in enumerate(d.items()): if k in exclude: continue str_k = u' ' * indent str_k += u'%s.' % (i + 1) if with_enumeration else u'' str_k += u'%s:' % k if isinstance(v, dict): self.assertEqual( PD.mock_calls[pd_calls], call( v, exclude, indent + INDENT_TAB, recursive_enumeration, recursive_enumeration, out)) pd_calls += 1 exp_calls += str_k + '\n' elif isinstance(v, list) or isinstance(v, tuple): self.assertEqual( PL.mock_calls[pl_calls], call( v, exclude, indent + INDENT_TAB, recursive_enumeration, recursive_enumeration, out)) pl_calls += 1 exp_calls += str_k + '\n' else: exp_calls += u'%s %s\n' % (str_k, v) self.assertEqual(exp_calls, out.getvalue()) out = StringIO()
def print_dict(self, *args, **kwargs): kwargs.setdefault('out', self) return print_dict(*args, **kwargs)