Mercurial > mozilla > hg > ProfileManager
annotate profilemanager/command.py @ 69:86b137d49467
use only the path element, not the whole tuple
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Fri, 07 May 2010 15:57:14 -0700 |
parents | 18f16bd1ba6b |
children | 145e111903d2 |
rev | line source |
---|---|
0
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
1 """ |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
2 a command-line interface to the command line, a la pythonpaste |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
3 """ |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
4 |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
5 import inspect |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
6 import sys |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
7 from optparse import OptionParser |
4
35dc297efa25
adding listing function and other cleanup
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
8 from pprint import pprint |
0
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
9 |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
10 if 'commands' not in globals(): |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
11 commands = {} |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
12 |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
13 def command(function): |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
14 # XXX should get bound/unbound state from function (how?) |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
15 global commands |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
16 name = function.func_name |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
17 doc = inspect.cleandoc(function.__doc__) |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
18 argspec = inspect.getargspec(function) |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
19 defaults = argspec.defaults |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
20 if defaults: |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
21 args = argspec.args[1:-len(defaults)] |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
22 optional = dict(zip(argspec.args[-len(defaults):], defaults)) |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
23 else: |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
24 args = argspec.args[1:] |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
25 optional = None |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
26 commands[name] = { 'doc': doc, |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
27 'args': args, |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
28 'optional': optional, |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
29 'varargs': argspec.varargs |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
30 } |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
31 return function |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
32 |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
33 def commandargs2str(command): |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
34 if isinstance(command, basestring): |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
35 command = commands[command] |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
36 retval = [] |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
37 retval.extend(['<%s>' % arg for arg in command['args']]) |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
38 varargs = command['varargs'] |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
39 if varargs: |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
40 retval.append('<%s> [%s] [...]' % (varargs, varargs)) |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
41 if command['optional']: |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
42 retval.append('[options]') |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
43 return ' '.join(retval) |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
44 |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
45 |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
46 def doc2arghelp(docstring, decoration='-', delimeter=':'): |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
47 """ |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
48 Parse a docstring and get at the section describing arguments |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
49 - decoration: decoration character |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
50 - delimeter: delimter character |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
51 |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
52 Yields a tuple of the stripped docstring and the arguments help |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
53 dictionary |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
54 """ |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
55 lines = [ i.strip() for i in docstring.split('\n') ] |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
56 argdict = {} |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
57 doc = [] |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
58 option = None |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
59 for line in lines: |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
60 if not line and option: # blank lines terminate |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
61 break |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
62 if line.startswith(decoration) and delimeter in line: |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
63 name, description = line.split(delimeter, 1) |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
64 name = name.lstrip(decoration).strip() |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
65 description = description.strip() |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
66 argdict[name] = [ description ] |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
67 option = name |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
68 else: |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
69 if option: |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
70 argdict[name].append(line) |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
71 else: |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
72 doc.append(line) |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
73 argdict = dict([(key, ' '.join(value)) |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
74 for key, value in argdict.items()]) |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
75 return ('\n'.join(doc), argdict) |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
76 |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
77 def command2parser(command): |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
78 doc, argdict = doc2arghelp(commands[command]['doc']) |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
79 parser = OptionParser('%%prog %s %s' % (command, commandargs2str(command)), |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
80 description=doc, add_help_option=False) |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
81 if commands[command]['optional']: |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
82 for key, value in commands[command]['optional'].items(): |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
83 help = argdict.get(key) |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
84 if value is True: |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
85 parser.add_option('--no-%s' % key, dest=key, |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
86 action='store_false', default=True, |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
87 help=help) |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
88 elif value is False: |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
89 parser.add_option('--%s' % key, action='store_true', |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
90 default=False, help=help) |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
91 else: |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
92 parser.add_option('--%s' % key, help=help) |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
93 |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
94 return parser |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
95 |
1
979315ed0816
mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
96 class CommandParser(OptionParser): |
979315ed0816
mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
97 def __init__(self, commands, description=None, setup=None): |
979315ed0816
mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
98 usage = '%prog [options] command [command-options]' |
4
35dc297efa25
adding listing function and other cleanup
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
99 OptionParser.__init__(self, usage=usage, description=description) |
1
979315ed0816
mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
100 for _command in commands: |
979315ed0816
mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
101 command(_command) |
979315ed0816
mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
102 self.disable_interspersed_args() |
979315ed0816
mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
103 self.setup = setup |
979315ed0816
mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
104 |
979315ed0816
mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
105 def print_help(self): |
979315ed0816
mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
106 OptionParser.print_help(self) |
979315ed0816
mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
107 # short descriptions for commands |
979315ed0816
mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
108 command_descriptions = [dict(name=i, |
979315ed0816
mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
109 description=commands[i]['doc'].strip().split('\n',1)[0]) |
979315ed0816
mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
110 for i in sorted(commands.keys())] |
979315ed0816
mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
111 max_len = max([len(i['name']) for i in command_descriptions]) |
979315ed0816
mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
112 description = "Commands: \n%s" % ('\n'.join([' %s%s %s' % (description['name'], ' ' * (max_len - len(description['name'])), description['description']) |
979315ed0816
mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
113 for description in command_descriptions])) |
979315ed0816
mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
114 |
979315ed0816
mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
115 print |
979315ed0816
mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
116 print description |
979315ed0816
mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
117 |
979315ed0816
mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
118 def parse(self, args=sys.argv[1:]): |
979315ed0816
mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
119 """global parse step""" |
979315ed0816
mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
120 |
979315ed0816
mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
121 self.options, args = self.parse_args(args) |
979315ed0816
mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
122 |
979315ed0816
mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
123 # help/sanity check -- should probably be separated |
979315ed0816
mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
124 if not len(args): |
979315ed0816
mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
125 self.print_help() |
979315ed0816
mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
126 sys.exit(0) |
979315ed0816
mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
127 if args[0] == 'help': |
979315ed0816
mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
128 if len(args) == 2: |
979315ed0816
mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
129 if args[1] in commands: |
979315ed0816
mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
130 name = args[1] |
979315ed0816
mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
131 commandparser = command2parser(name) |
979315ed0816
mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
132 commandparser.print_help() |
979315ed0816
mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
133 else: |
979315ed0816
mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
134 self.error("No command '%s'" % args[1]) |
979315ed0816
mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
135 else: |
979315ed0816
mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
136 self.print_help() |
979315ed0816
mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
137 sys.exit(0) |
979315ed0816
mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
138 command = args[0] |
979315ed0816
mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
139 if command not in commands: |
979315ed0816
mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
140 self.error("No command '%s'" % command) |
979315ed0816
mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
141 return command, args[1:] |
979315ed0816
mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
142 |
979315ed0816
mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
143 def invoke(self, args=sys.argv[1:]): |
979315ed0816
mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
144 """ |
979315ed0816
mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
145 invoke |
979315ed0816
mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
146 """ |
979315ed0816
mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
147 |
979315ed0816
mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
148 # parse |
979315ed0816
mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
149 name, args = self.parse(args) |
979315ed0816
mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
150 |
979315ed0816
mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
151 # setup |
979315ed0816
mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
152 _object = self.setup(self, self.options) |
979315ed0816
mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
153 |
979315ed0816
mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
154 # command specific args |
979315ed0816
mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
155 command = commands[name] |
979315ed0816
mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
156 commandparser = command2parser(name) |
979315ed0816
mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
157 command_options, command_args = commandparser.parse_args(args) |
979315ed0816
mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
158 if len(command_args) < len(command['args']): |
979315ed0816
mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
159 commandparser.error("Not enough arguments given") |
979315ed0816
mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
160 if len(command_args) != len(command['args']) and not command['varargs']: |
979315ed0816
mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
161 commandparser.error("Too many arguments given") |
979315ed0816
mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
162 |
979315ed0816
mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
163 # invoke the command |
5
ca57920aa223
adding better formatting for list
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
164 retval = getattr(_object, name)(*command_args, **command_options.__dict__) |
38 | 165 |
166 # print the output | |
55 | 167 if retval is None: |
168 pass | |
169 elif isinstance(retval, basestring): | |
5
ca57920aa223
adding better formatting for list
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
170 print retval |
38 | 171 elif isinstance(retval, dict): |
172 for key in sorted(retval.keys()): | |
173 print '%s: %s' % (key, retval[key]) | |
174 elif hasattr(retval, '__iter__'): | |
64
18f16bd1ba6b
finish backups listing function and formatting for it
Jeff Hammel <jhammel@mozilla.com>
parents:
55
diff
changeset
|
175 |
18f16bd1ba6b
finish backups listing function and formatting for it
Jeff Hammel <jhammel@mozilla.com>
parents:
55
diff
changeset
|
176 # hack since python doesn't have ordered dicts |
18f16bd1ba6b
finish backups listing function and formatting for it
Jeff Hammel <jhammel@mozilla.com>
parents:
55
diff
changeset
|
177 if not [ val for val in retval |
18f16bd1ba6b
finish backups listing function and formatting for it
Jeff Hammel <jhammel@mozilla.com>
parents:
55
diff
changeset
|
178 if not(isinstance(val, tuple) and len(val) == 2) ]: |
18f16bd1ba6b
finish backups listing function and formatting for it
Jeff Hammel <jhammel@mozilla.com>
parents:
55
diff
changeset
|
179 for val in retval: |
18f16bd1ba6b
finish backups listing function and formatting for it
Jeff Hammel <jhammel@mozilla.com>
parents:
55
diff
changeset
|
180 print '%s: %s' % (val[0], val[1]) |
18f16bd1ba6b
finish backups listing function and formatting for it
Jeff Hammel <jhammel@mozilla.com>
parents:
55
diff
changeset
|
181 |
18f16bd1ba6b
finish backups listing function and formatting for it
Jeff Hammel <jhammel@mozilla.com>
parents:
55
diff
changeset
|
182 else: |
18f16bd1ba6b
finish backups listing function and formatting for it
Jeff Hammel <jhammel@mozilla.com>
parents:
55
diff
changeset
|
183 for val in retval: |
18f16bd1ba6b
finish backups listing function and formatting for it
Jeff Hammel <jhammel@mozilla.com>
parents:
55
diff
changeset
|
184 print val |
5
ca57920aa223
adding better formatting for list
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
185 else: |
ca57920aa223
adding better formatting for list
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
186 pprint(retval) |
38 | 187 |
188 # return the value | |
5
ca57920aa223
adding better formatting for list
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
189 return retval |
1
979315ed0816
mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
190 |