Mercurial > hg > CommandParser
annotate commandparser/command.py @ 13:6ffd095cf5dd
add TODO
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Fri, 17 May 2013 02:06:30 -0700 |
parents | e0a3148e67a8 |
children |
rev | line source |
---|---|
1 | 1 """ |
2 a command-line interface to the command line, a la pythonpaste | |
3 """ | |
4 | |
5 import inspect | |
6 import os | |
7 import sys | |
8 from optparse import OptionParser | |
9 from pprint import pprint | |
10 | |
9 | 11 try: |
12 import json | |
13 except ImportError: | |
14 import simplejson as json | |
15 | |
10 | 16 # BBB python 2.4 |
17 cleandoc = getattr(inspect, 'cleandoc', lambda x: x.strip()) | |
18 | |
2
d36032625794
add __all__ and whitespace cleanup
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
19 __all__ = ['Undefined', 'CommandParser'] |
d36032625794
add __all__ and whitespace cleanup
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
20 |
1 | 21 class Undefined(object): |
9 | 22 def __init__(self, default): |
23 self.default=default | |
1 | 24 |
25 class CommandParser(OptionParser): | |
26 | |
27 def __init__(self, _class, description=None): | |
28 self._class = _class | |
29 self.commands = {} | |
8
109627b7db9f
add ability to have mandatory class arguments
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
30 init = self.command(_class.__init__) |
109627b7db9f
add ability to have mandatory class arguments
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
31 self.init_args = init['args'] |
109627b7db9f
add ability to have mandatory class arguments
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
32 command_str = ' '.join(self.init_args + ['command']) |
109627b7db9f
add ability to have mandatory class arguments
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
33 usage = '%prog [options]' + ' %s [command-options]' % (command_str) |
1 | 34 description = description or _class.__doc__ |
35 OptionParser.__init__(self, usage=usage, description=description) | |
36 commands = [ getattr(_class, i) for i in dir(_class) | |
37 if not i.startswith('_') ] | |
38 commands = [ method for method in commands | |
39 if hasattr(method, '__call__') ] | |
40 for _command in commands: | |
41 c = self.command(_command) | |
42 self.commands[c['name']] = c | |
43 | |
44 # get class options | |
45 self.command2parser(init, self) | |
46 self.disable_interspersed_args() | |
47 | |
48 def add_option(self, *args, **kwargs): | |
49 kwargs['default'] = Undefined(kwargs.get('default')) | |
50 OptionParser.add_option(self, *args, **kwargs) | |
51 | |
52 def print_help(self): | |
3 | 53 |
1 | 54 OptionParser.print_help(self) |
3 | 55 |
1 | 56 # short descriptions for commands |
57 command_descriptions = [dict(name=i, | |
58 description=self.commands[i]['doc'].strip().split('\n',1)[0]) | |
59 for i in self.commands.keys()] | |
60 command_descriptions.append(dict(name='help', description='print help for a given command')) | |
61 command_descriptions.sort(key=lambda x: x['name']) | |
62 max_len = max([len(i['name']) for i in command_descriptions]) | |
63 description = "Commands: \n%s" % ('\n'.join([' %s%s %s' % (description['name'], ' ' * (max_len - len(description['name'])), description['description']) | |
64 for description in command_descriptions])) | |
65 | |
66 print | |
67 print description | |
68 | |
69 def parse(self, args=sys.argv[1:]): | |
70 """global parse step""" | |
3 | 71 |
1 | 72 self.options, args = self.parse_args(args) |
73 | |
74 # help/sanity check -- should probably be separated | |
75 if not len(args): | |
76 self.print_help() | |
77 sys.exit(0) | |
78 if args[0] == 'help': | |
79 if len(args) == 2: | |
80 if args[1] == 'help': | |
81 self.print_help() | |
82 elif args[1] in self.commands: | |
83 name = args[1] | |
84 commandparser = self.command2parser(name) | |
85 commandparser.print_help() | |
86 else: | |
87 self.error("No command '%s'" % args[1]) | |
88 else: | |
89 self.print_help() | |
90 sys.exit(0) | |
8
109627b7db9f
add ability to have mandatory class arguments
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
91 required = len(self.init_args) + 1 # command |
109627b7db9f
add ability to have mandatory class arguments
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
92 if len(args) < required: |
109627b7db9f
add ability to have mandatory class arguments
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
93 self.print_usage() |
109627b7db9f
add ability to have mandatory class arguments
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
94 sys.exit(1) |
109627b7db9f
add ability to have mandatory class arguments
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
95 self.command_args = args[:len(self.init_args)] |
109627b7db9f
add ability to have mandatory class arguments
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
96 args = args[len(self.init_args):] |
1 | 97 command = args[0] |
98 if command not in self.commands: | |
99 self.error("No command '%s'" % command) | |
100 return command, args[1:] | |
101 | |
102 def invoke(self, args=sys.argv[1:]): | |
103 """ | |
104 invoke | |
105 """ | |
106 | |
107 # parse | |
8
109627b7db9f
add ability to have mandatory class arguments
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
108 name, args = self.parse(args) |
1 | 109 |
110 # setup | |
111 options = {} | |
112 dotfile = os.path.join(os.environ['HOME'], '.' + self.get_prog_name()) | |
113 if os.path.exists(dotfile): | |
114 f = file(dotfile) | |
115 for line in f.readlines(): | |
116 line = line.strip() | |
117 if not line: | |
118 continue | |
119 if ':' in line: | |
120 key, value = [i.strip() | |
121 for i in line.split(':', 1)] | |
122 options[key] = value | |
123 else: | |
124 print >> sys.stderr, "Bad option line: " + line | |
125 for key, value in self.options.__dict__.items(): | |
126 if isinstance(value, Undefined): | |
127 if key in options: | |
128 continue | |
129 options[key] = value.default | |
130 else: | |
131 options[key] = value | |
8
109627b7db9f
add ability to have mandatory class arguments
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
132 _object = self._class(*self.command_args, **options) |
1 | 133 |
134 # command specific args | |
135 command = self.commands[name] | |
136 commandparser = self.command2parser(name) | |
137 command_options, command_args = commandparser.parse_args(args) | |
138 if len(command_args) < len(command['args']): | |
139 commandparser.error("Not enough arguments given") | |
140 if len(command_args) != len(command['args']) and not command['varargs']: | |
141 commandparser.error("Too many arguments given") | |
142 | |
143 # invoke the command | |
144 retval = getattr(_object, name)(*command_args, **command_options.__dict__) | |
145 if isinstance(retval, basestring): | |
146 print retval | |
147 elif retval is None: | |
148 pass | |
149 elif isinstance(retval, list): | |
150 for i in retval: | |
151 print i | |
152 elif isinstance(retval, dict): | |
153 try: | |
154 print json.dumps(retval, indent=2, sort_keys=True) | |
155 except: | |
156 pprint(retval) | |
157 else: | |
158 pprint(retval) | |
159 return retval | |
160 | |
161 def command(self, function): | |
162 name = function.func_name | |
163 if function.__doc__: | |
10 | 164 doc = cleandoc(function.__doc__) |
1 | 165 else: |
166 doc = '' | |
12
e0a3148e67a8
bug fix and a short overhaul of documentation
Jeff Hammel <jhammel@mozilla.com>
parents:
10
diff
changeset
|
167 _args, varargs, varkw, defaults = inspect.getargspec(function) |
1 | 168 if defaults: |
12
e0a3148e67a8
bug fix and a short overhaul of documentation
Jeff Hammel <jhammel@mozilla.com>
parents:
10
diff
changeset
|
169 args = _args[1:-len(defaults)] |
e0a3148e67a8
bug fix and a short overhaul of documentation
Jeff Hammel <jhammel@mozilla.com>
parents:
10
diff
changeset
|
170 optional = dict(zip(_args[-len(defaults):], defaults)) |
1 | 171 else: |
12
e0a3148e67a8
bug fix and a short overhaul of documentation
Jeff Hammel <jhammel@mozilla.com>
parents:
10
diff
changeset
|
172 args = _args[1:] |
1 | 173 optional = None |
5 | 174 command = {'doc': doc, |
175 'name': name, | |
12
e0a3148e67a8
bug fix and a short overhaul of documentation
Jeff Hammel <jhammel@mozilla.com>
parents:
10
diff
changeset
|
176 'args': args, # mandatory arguments |
5 | 177 'optional': optional, |
10 | 178 'varargs': varargs |
5 | 179 } |
1 | 180 return command |
181 | |
182 def commandargs2str(self, command): | |
183 if isinstance(command, basestring): | |
184 command = self.commands[command] | |
185 retval = [] | |
186 retval.extend(['<%s>' % arg for arg in command['args']]) | |
187 varargs = command['varargs'] | |
188 if varargs: | |
189 retval.append('<%s> [%s] [...]' % (varargs, varargs)) | |
190 if command['optional']: | |
191 retval.append('[options]') | |
192 return ' '.join(retval) | |
193 | |
194 def doc2arghelp(self, docstring, decoration='-', delimeter=':'): | |
195 """ | |
196 Parse a docstring and get at the section describing arguments | |
197 - decoration: decoration character | |
198 - delimeter: delimter character | |
3 | 199 |
1 | 200 Yields a tuple of the stripped docstring and the arguments help |
201 dictionary | |
202 """ | |
203 lines = [ i.strip() for i in docstring.split('\n') ] | |
204 argdict = {} | |
205 doc = [] | |
206 option = None | |
207 for line in lines: | |
208 if not line and option: # blank lines terminate [?] | |
209 break | |
210 if line.startswith(decoration) and delimeter in line: | |
211 name, description = line.split(delimeter, 1) | |
212 name = name.lstrip(decoration).strip() | |
213 description = description.strip() | |
214 argdict[name] = [ description ] | |
215 option = name | |
216 else: | |
217 if option: | |
218 argdict[name].append(line) | |
219 else: | |
220 doc.append(line) | |
221 argdict = dict([(key, ' '.join(value)) | |
222 for key, value in argdict.items()]) | |
223 return ('\n'.join(doc), argdict) | |
224 | |
225 def command2parser(self, command, parser=None): | |
226 if isinstance(command, basestring): | |
227 command = self.commands[command] | |
228 doc, argdict = self.doc2arghelp(command['doc']) | |
229 if parser is None: | |
230 parser = OptionParser('%%prog %s %s' % (command['name'], self.commandargs2str(command)), | |
231 description=doc, add_help_option=False) | |
232 if command['optional']: | |
233 for key, value in command['optional'].items(): | |
234 help = argdict.get(key, '') | |
235 if value is True: | |
236 parser.add_option('--no-%s' % key, dest=key, | |
237 action='store_false', default=True, | |
238 help=help) | |
239 elif value is False: | |
240 parser.add_option('--%s' % key, action='store_true', | |
241 default=False, help=help) | |
4 | 242 elif isinstance(value, int): |
243 help += ' [DEFAULT: %s]' % value | |
244 parser.add_option('--%s' % key, help=help, | |
245 type='int', default=value) | |
1 | 246 elif type(value) in set([type(()), type([])]): |
247 if value: | |
248 help += ' [DEFAULT: %s]' % value | |
249 parser.add_option('--%s' % key, action='append', | |
250 default=list(value), | |
251 help=help) | |
252 else: | |
253 if value is not None: | |
254 help += ' [DEFAULT: %s]' % value | |
255 parser.add_option('--%s' % key, help=help, default=value) | |
3 | 256 |
1 | 257 return parser |