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