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