Fix _command_list() function for empty command sets
[stgit] / stgit / commands / __init__.py
CommitLineData
33ff9cdd
KH
1# -*- coding: utf-8 -*-
2
fcee87cf
CM
3__copyright__ = """
4Copyright (C) 2005, Catalin Marinas <catalin.marinas@gmail.com>
33ff9cdd 5Copyright (C) 2008, Karl Hasselström <kha@treskal.com>
fcee87cf
CM
6
7This program is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License version 2 as
9published by the Free Software Foundation.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with this program; if not, write to the Free Software
18Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19"""
33ff9cdd
KH
20
21import os
22from stgit import utils
23
24def get_command(mod):
25 """Import and return the given command module."""
26 return __import__(__name__ + '.' + mod, globals(), locals(), ['*'])
27
28_kinds = [('repo', 'Repository commands'),
29 ('stack', 'Stack (branch) commands'),
30 ('patch', 'Patch commands'),
3ecc9c01
CM
31 ('wc', 'Index/worktree commands'),
32 ('alias', 'Alias commands')]
33ff9cdd
KH
33_kind_order = [kind for kind, desc in _kinds]
34_kinds = dict(_kinds)
35
36def _find_commands():
37 for p in __path__:
38 for fn in os.listdir(p):
39 if not fn.endswith('.py'):
40 continue
41 mod = utils.strip_suffix('.py', fn)
42 m = get_command(mod)
43 if not hasattr(m, 'usage'):
44 continue
45 yield mod, m
46
47def get_commands(allow_cached = True):
48 """Return a map from command name to a tuple of module name, command
49 type, and one-line command help."""
50 if allow_cached:
51 try:
52 from stgit.commands.cmdlist import command_list
53 return command_list
54 except ImportError:
55 # cmdlist.py doesn't exist, so do it the expensive way.
56 pass
57 return dict((getattr(m, 'name', mod), (mod, _kinds[m.kind], m.help))
58 for mod, m in _find_commands())
59
60def py_commands(commands, f):
61 f.write('command_list = {\n')
62 for key, val in sorted(commands.iteritems()):
63 f.write(' %r: %r,\n' % (key, val))
64 f.write(' }\n')
65
66def _command_list(commands):
67 kinds = {}
68 for cmd, (mod, kind, help) in commands.iteritems():
69 kinds.setdefault(kind, {})[cmd] = help
70 for kind in _kind_order:
71 kind = _kinds[kind]
c391c4c1
CM
72 try:
73 yield kind, sorted(kinds[kind].iteritems())
74 except KeyError:
75 pass
33ff9cdd
KH
76
77def pretty_command_list(commands, f):
78 cmd_len = max(len(cmd) for cmd in commands.iterkeys())
79 sep = ''
80 for kind, cmds in _command_list(commands):
81 f.write(sep)
82 sep = '\n'
83 f.write('%s:\n' % kind)
84 for cmd, help in cmds:
85 f.write(' %*s %s\n' % (-cmd_len, cmd, help))
86
87def _write_underlined(s, u, f):
88 f.write(s + '\n')
89 f.write(u*len(s) + '\n')
90
91def asciidoc_command_list(commands, f):
92 for kind, cmds in _command_list(commands):
93 _write_underlined(kind, '~', f)
94 f.write('\n')
95 for cmd, help in cmds:
760a7cfc 96 f.write('linkstgsub:%s[]::\n' % cmd)
33ff9cdd
KH
97 f.write(' %s\n' % help)
98 f.write('\n')