comparison commandparser/command.py @ 10:a3599e4db9a4

python 2.4 compatability
author Jeff Hammel <jhammel@mozilla.com>
date Mon, 02 Apr 2012 10:21:59 -0700
parents 0069096e8e22
children e0a3148e67a8
comparison
equal deleted inserted replaced
9:0069096e8e22 10:a3599e4db9a4
10 10
11 try: 11 try:
12 import json 12 import json
13 except ImportError: 13 except ImportError:
14 import simplejson as json 14 import simplejson as json
15
16 # BBB python 2.4
17 cleandoc = getattr(inspect, 'cleandoc', lambda x: x.strip())
15 18
16 __all__ = ['Undefined', 'CommandParser'] 19 __all__ = ['Undefined', 'CommandParser']
17 20
18 class Undefined(object): 21 class Undefined(object):
19 def __init__(self, default): 22 def __init__(self, default):
157 return retval 160 return retval
158 161
159 def command(self, function): 162 def command(self, function):
160 name = function.func_name 163 name = function.func_name
161 if function.__doc__: 164 if function.__doc__:
162 doc = inspect.cleandoc(function.__doc__) 165 doc = cleandoc(function.__doc__)
163 else: 166 else:
164 doc = '' 167 doc = ''
165 argspec = inspect.getargspec(function) 168 args, varargs, varkw, defaults = inspect.getargspec(function)
166 defaults = argspec.defaults
167 if defaults: 169 if defaults:
168 args = argspec.args[1:-len(defaults)] 170 args = args[1:-len(defaults)]
169 optional = dict(zip(argspec.args[-len(defaults):], defaults)) 171 optional = dict(zip(args[-len(defaults):], defaults))
170 else: 172 else:
171 args = argspec.args[1:] 173 args = args[1:]
172 optional = None 174 optional = None
173 command = {'doc': doc, 175 command = {'doc': doc,
174 'name': name, 176 'name': name,
175 'args': args, 177 'args': args,
176 'optional': optional, 178 'optional': optional,
177 'varargs': argspec.varargs 179 'varargs': varargs
178 } 180 }
179 return command 181 return command
180 182
181 def commandargs2str(self, command): 183 def commandargs2str(self, command):
182 if isinstance(command, basestring): 184 if isinstance(command, basestring):