f9de42bdf1159a77e3d4073148ca15757f0d4b67
1 # -*- coding: utf-8 -*-
4 Copyright (C) 2005, Catalin Marinas <catalin.marinas@gmail.com>
5 Copyright (C) 2008, Karl Hasselström <kha@treskal.com>
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.
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.
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
22 from stgit
import utils
25 """Import and return the given command module."""
26 return __import__(__name__
+ '.' + mod
, globals(), locals(), ['*'])
28 _kinds
= [('repo', 'Repository commands'),
29 ('stack', 'Stack (branch) commands'),
30 ('patch', 'Patch commands'),
31 ('wc', 'Index/worktree commands'),
32 ('alias', 'Alias commands')]
33 _kind_order
= [kind
for kind
, desc
in _kinds
]
38 for fn
in os
.listdir(p
):
39 if not fn
.endswith('.py'):
41 mod
= utils
.strip_suffix('.py', fn
)
43 if not hasattr(m
, 'usage'):
47 def 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."""
52 from stgit
.commands
.cmdlist
import command_list
55 # cmdlist.py doesn't exist, so do it the expensive way.
57 return dict((getattr(m
, 'name', mod
), (mod
, _kinds
[m
.kind
], m
.help))
58 for mod
, m
in _find_commands())
60 def 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
))
66 def _command_list(commands
):
68 for cmd
, (mod
, kind
, help) in commands
.iteritems():
69 kinds
.setdefault(kind
, {})[cmd
] = help
70 for kind
in _kind_order
:
72 yield kind
, sorted(kinds
[kind
].iteritems())
74 def pretty_command_list(commands
, f
):
75 cmd_len
= max(len(cmd
) for cmd
in commands
.iterkeys())
77 for kind
, cmds
in _command_list(commands
):
80 f
.write('%s:\n' % kind
)
81 for cmd
, help in cmds
:
82 f
.write(' %*s %s\n' %
(-cmd_len
, cmd
, help))
84 def _write_underlined(s
, u
, f
):
86 f
.write(u
*len(s
) + '\n')
88 def asciidoc_command_list(commands
, f
):
89 for kind
, cmds
in _command_list(commands
):
90 _write_underlined(kind
, '~', f
)
92 for cmd
, help in cmds
:
93 f
.write('linkstgsub:%s[]::\n' % cmd
)
94 f
.write(' %s\n' %
help)