annotate profilemanager/command.py @ 79:145e111903d2 default tip

add MPL license
author Jeff Hammel <jhammel@mozilla.com>
date Mon, 10 May 2010 13:11:38 -0700
parents 18f16bd1ba6b
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
79
145e111903d2 add MPL license
Jeff Hammel <jhammel@mozilla.com>
parents: 64
diff changeset
1 # ***** BEGIN LICENSE BLOCK *****
145e111903d2 add MPL license
Jeff Hammel <jhammel@mozilla.com>
parents: 64
diff changeset
2 # Version: MPL 1.1/GPL 2.0/LGPL 2.1
145e111903d2 add MPL license
Jeff Hammel <jhammel@mozilla.com>
parents: 64
diff changeset
3 #
145e111903d2 add MPL license
Jeff Hammel <jhammel@mozilla.com>
parents: 64
diff changeset
4 # The contents of this file are subject to the Mozilla Public License Version
145e111903d2 add MPL license
Jeff Hammel <jhammel@mozilla.com>
parents: 64
diff changeset
5 # 1.1 (the "License"); you may not use this file except in compliance with
145e111903d2 add MPL license
Jeff Hammel <jhammel@mozilla.com>
parents: 64
diff changeset
6 # the License. You may obtain a copy of the License at
145e111903d2 add MPL license
Jeff Hammel <jhammel@mozilla.com>
parents: 64
diff changeset
7 # http://www.mozilla.org/MPL/
145e111903d2 add MPL license
Jeff Hammel <jhammel@mozilla.com>
parents: 64
diff changeset
8 #
145e111903d2 add MPL license
Jeff Hammel <jhammel@mozilla.com>
parents: 64
diff changeset
9 # Software distributed under the License is distributed on an "AS IS" basis,
145e111903d2 add MPL license
Jeff Hammel <jhammel@mozilla.com>
parents: 64
diff changeset
10 # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
145e111903d2 add MPL license
Jeff Hammel <jhammel@mozilla.com>
parents: 64
diff changeset
11 # for the specific language governing rights and limitations under the
145e111903d2 add MPL license
Jeff Hammel <jhammel@mozilla.com>
parents: 64
diff changeset
12 # License.
145e111903d2 add MPL license
Jeff Hammel <jhammel@mozilla.com>
parents: 64
diff changeset
13 #
145e111903d2 add MPL license
Jeff Hammel <jhammel@mozilla.com>
parents: 64
diff changeset
14 # The Original Code is mozilla.org code.
145e111903d2 add MPL license
Jeff Hammel <jhammel@mozilla.com>
parents: 64
diff changeset
15 #
145e111903d2 add MPL license
Jeff Hammel <jhammel@mozilla.com>
parents: 64
diff changeset
16 # The Initial Developer of the Original Code is
145e111903d2 add MPL license
Jeff Hammel <jhammel@mozilla.com>
parents: 64
diff changeset
17 # Mozilla.org.
145e111903d2 add MPL license
Jeff Hammel <jhammel@mozilla.com>
parents: 64
diff changeset
18 # Portions created by the Initial Developer are Copyright (C) 2010
145e111903d2 add MPL license
Jeff Hammel <jhammel@mozilla.com>
parents: 64
diff changeset
19 # the Initial Developer. All Rights Reserved.
145e111903d2 add MPL license
Jeff Hammel <jhammel@mozilla.com>
parents: 64
diff changeset
20 #
145e111903d2 add MPL license
Jeff Hammel <jhammel@mozilla.com>
parents: 64
diff changeset
21 # Contributor(s):
145e111903d2 add MPL license
Jeff Hammel <jhammel@mozilla.com>
parents: 64
diff changeset
22 # Jeff Hammel <jhammel@mozilla.com> (Original author)
145e111903d2 add MPL license
Jeff Hammel <jhammel@mozilla.com>
parents: 64
diff changeset
23 #
145e111903d2 add MPL license
Jeff Hammel <jhammel@mozilla.com>
parents: 64
diff changeset
24 # Alternatively, the contents of this file may be used under the terms of
145e111903d2 add MPL license
Jeff Hammel <jhammel@mozilla.com>
parents: 64
diff changeset
25 # either of the GNU General Public License Version 2 or later (the "GPL"),
145e111903d2 add MPL license
Jeff Hammel <jhammel@mozilla.com>
parents: 64
diff changeset
26 # or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
145e111903d2 add MPL license
Jeff Hammel <jhammel@mozilla.com>
parents: 64
diff changeset
27 # in which case the provisions of the GPL or the LGPL are applicable instead
145e111903d2 add MPL license
Jeff Hammel <jhammel@mozilla.com>
parents: 64
diff changeset
28 # of those above. If you wish to allow use of your version of this file only
145e111903d2 add MPL license
Jeff Hammel <jhammel@mozilla.com>
parents: 64
diff changeset
29 # under the terms of either the GPL or the LGPL, and not to allow others to
145e111903d2 add MPL license
Jeff Hammel <jhammel@mozilla.com>
parents: 64
diff changeset
30 # use your version of this file under the terms of the MPL, indicate your
145e111903d2 add MPL license
Jeff Hammel <jhammel@mozilla.com>
parents: 64
diff changeset
31 # decision by deleting the provisions above and replace them with the notice
145e111903d2 add MPL license
Jeff Hammel <jhammel@mozilla.com>
parents: 64
diff changeset
32 # and other provisions required by the GPL or the LGPL. If you do not delete
145e111903d2 add MPL license
Jeff Hammel <jhammel@mozilla.com>
parents: 64
diff changeset
33 # the provisions above, a recipient may use your version of this file under
145e111903d2 add MPL license
Jeff Hammel <jhammel@mozilla.com>
parents: 64
diff changeset
34 # the terms of any one of the MPL, the GPL or the LGPL.
145e111903d2 add MPL license
Jeff Hammel <jhammel@mozilla.com>
parents: 64
diff changeset
35 #
145e111903d2 add MPL license
Jeff Hammel <jhammel@mozilla.com>
parents: 64
diff changeset
36 # ***** END LICENSE BLOCK *****
0
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
37 """
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
38 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
39 """
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
40
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
41 import inspect
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
42 import sys
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
43 from optparse import OptionParser
4
35dc297efa25 adding listing function and other cleanup
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
44 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
45
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
46 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
47 commands = {}
79
145e111903d2 add MPL license
Jeff Hammel <jhammel@mozilla.com>
parents: 64
diff changeset
48
0
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
49 def command(function):
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
50 # 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
51 global commands
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
52 name = function.func_name
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
53 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
54 argspec = inspect.getargspec(function)
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
55 defaults = argspec.defaults
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
56 if defaults:
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
57 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
58 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
59 else:
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
60 args = argspec.args[1:]
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
61 optional = None
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
62 commands[name] = { 'doc': doc,
79
145e111903d2 add MPL license
Jeff Hammel <jhammel@mozilla.com>
parents: 64
diff changeset
63 'args': args,
0
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
64 'optional': optional,
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
65 'varargs': argspec.varargs
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
66 }
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
67 return function
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
68
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
69 def commandargs2str(command):
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
70 if isinstance(command, basestring):
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
71 command = commands[command]
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
72 retval = []
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
73 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
74 varargs = command['varargs']
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
75 if varargs:
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
76 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
77 if command['optional']:
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
78 retval.append('[options]')
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
79 return ' '.join(retval)
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
80
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
81
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
82 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
83 """
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
84 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
85 - decoration: decoration character
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
86 - delimeter: delimter character
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
87
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
88 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
89 dictionary
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
90 """
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
91 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
92 argdict = {}
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
93 doc = []
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
94 option = None
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
95 for line in lines:
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
96 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
97 break
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
98 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
99 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
100 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
101 description = description.strip()
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
102 argdict[name] = [ description ]
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
103 option = name
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
104 else:
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
105 if option:
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
106 argdict[name].append(line)
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
107 else:
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
108 doc.append(line)
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
109 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
110 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
111 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
112
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
113 def command2parser(command):
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
114 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
115 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
116 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
117 if commands[command]['optional']:
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
118 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
119 help = argdict.get(key)
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
120 if value is True:
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
121 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
122 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
123 help=help)
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
124 elif value is False:
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
125 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
126 default=False, help=help)
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
127 else:
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
128 parser.add_option('--%s' % key, help=help)
79
145e111903d2 add MPL license
Jeff Hammel <jhammel@mozilla.com>
parents: 64
diff changeset
129
0
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
130 return parser
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
131
1
979315ed0816 mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
132 class CommandParser(OptionParser):
979315ed0816 mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
133 def __init__(self, commands, description=None, setup=None):
979315ed0816 mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
134 usage = '%prog [options] command [command-options]'
4
35dc297efa25 adding listing function and other cleanup
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
135 OptionParser.__init__(self, usage=usage, description=description)
1
979315ed0816 mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
136 for _command in commands:
979315ed0816 mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
137 command(_command)
979315ed0816 mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
138 self.disable_interspersed_args()
979315ed0816 mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
139 self.setup = setup
979315ed0816 mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
140
979315ed0816 mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
141 def print_help(self):
979315ed0816 mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
142 OptionParser.print_help(self)
979315ed0816 mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
143 # short descriptions for commands
979315ed0816 mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
144 command_descriptions = [dict(name=i,
979315ed0816 mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
145 description=commands[i]['doc'].strip().split('\n',1)[0])
979315ed0816 mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
146 for i in sorted(commands.keys())]
979315ed0816 mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
147 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
148 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
149 for description in command_descriptions]))
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 print
979315ed0816 mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
152 print description
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 def parse(self, args=sys.argv[1:]):
979315ed0816 mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
155 """global parse step"""
79
145e111903d2 add MPL license
Jeff Hammel <jhammel@mozilla.com>
parents: 64
diff changeset
156
1
979315ed0816 mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
157 self.options, args = self.parse_args(args)
979315ed0816 mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
158
979315ed0816 mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
159 # help/sanity check -- should probably be separated
979315ed0816 mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
160 if not len(args):
979315ed0816 mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
161 self.print_help()
979315ed0816 mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
162 sys.exit(0)
979315ed0816 mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
163 if args[0] == 'help':
979315ed0816 mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
164 if len(args) == 2:
979315ed0816 mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
165 if args[1] in commands:
979315ed0816 mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
166 name = args[1]
979315ed0816 mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
167 commandparser = command2parser(name)
979315ed0816 mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
168 commandparser.print_help()
979315ed0816 mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
169 else:
979315ed0816 mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
170 self.error("No command '%s'" % args[1])
979315ed0816 mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
171 else:
979315ed0816 mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
172 self.print_help()
979315ed0816 mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
173 sys.exit(0)
979315ed0816 mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
174 command = args[0]
979315ed0816 mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
175 if command not in commands:
979315ed0816 mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
176 self.error("No command '%s'" % command)
979315ed0816 mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
177 return command, args[1:]
979315ed0816 mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
178
979315ed0816 mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
179 def invoke(self, args=sys.argv[1:]):
979315ed0816 mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
180 """
979315ed0816 mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
181 invoke
979315ed0816 mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
182 """
979315ed0816 mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
183
979315ed0816 mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
184 # parse
979315ed0816 mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
185 name, args = self.parse(args)
979315ed0816 mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
186
979315ed0816 mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
187 # setup
979315ed0816 mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
188 _object = self.setup(self, self.options)
979315ed0816 mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
189
979315ed0816 mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
190 # command specific args
979315ed0816 mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
191 command = commands[name]
979315ed0816 mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
192 commandparser = command2parser(name)
979315ed0816 mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
193 command_options, command_args = commandparser.parse_args(args)
979315ed0816 mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
194 if len(command_args) < len(command['args']):
979315ed0816 mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
195 commandparser.error("Not enough arguments given")
979315ed0816 mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
196 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
197 commandparser.error("Too many arguments given")
979315ed0816 mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
198
979315ed0816 mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
199 # invoke the command
5
ca57920aa223 adding better formatting for list
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
200 retval = getattr(_object, name)(*command_args, **command_options.__dict__)
38
81500171fda8 change output format
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
201
81500171fda8 change output format
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
202 # print the output
55
3b53d584195f dont print None
Jeff Hammel <jhammel@mozilla.com>
parents: 38
diff changeset
203 if retval is None:
3b53d584195f dont print None
Jeff Hammel <jhammel@mozilla.com>
parents: 38
diff changeset
204 pass
3b53d584195f dont print None
Jeff Hammel <jhammel@mozilla.com>
parents: 38
diff changeset
205 elif isinstance(retval, basestring):
5
ca57920aa223 adding better formatting for list
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
206 print retval
38
81500171fda8 change output format
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
207 elif isinstance(retval, dict):
81500171fda8 change output format
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
208 for key in sorted(retval.keys()):
81500171fda8 change output format
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
209 print '%s: %s' % (key, retval[key])
81500171fda8 change output format
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
210 elif hasattr(retval, '__iter__'):
64
18f16bd1ba6b finish backups listing function and formatting for it
Jeff Hammel <jhammel@mozilla.com>
parents: 55
diff changeset
211
18f16bd1ba6b finish backups listing function and formatting for it
Jeff Hammel <jhammel@mozilla.com>
parents: 55
diff changeset
212 # 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
213 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
214 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
215 for val in retval:
18f16bd1ba6b finish backups listing function and formatting for it
Jeff Hammel <jhammel@mozilla.com>
parents: 55
diff changeset
216 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
217
18f16bd1ba6b finish backups listing function and formatting for it
Jeff Hammel <jhammel@mozilla.com>
parents: 55
diff changeset
218 else:
18f16bd1ba6b finish backups listing function and formatting for it
Jeff Hammel <jhammel@mozilla.com>
parents: 55
diff changeset
219 for val in retval:
18f16bd1ba6b finish backups listing function and formatting for it
Jeff Hammel <jhammel@mozilla.com>
parents: 55
diff changeset
220 print val
5
ca57920aa223 adding better formatting for list
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
221 else:
ca57920aa223 adding better formatting for list
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
222 pprint(retval)
38
81500171fda8 change output format
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
223
81500171fda8 change output format
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
224 # return the value
5
ca57920aa223 adding better formatting for list
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
225 return retval
1
979315ed0816 mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
226