annotate commandparser/command.py @ 11:03db23600c1f

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