Mercurial > hg > configuration
comparison configuration/configuration.py @ 108:a2184db43fe2
fix dict command line processing
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Thu, 03 May 2012 15:46:13 -0700 |
parents | 83d66a9bdef0 |
children | 09642528be02 |
comparison
equal
deleted
inserted
replaced
107:83d66a9bdef0 | 108:a2184db43fe2 |
---|---|
109 | 109 |
110 class ConfigurationOption(optparse.Option): | 110 class ConfigurationOption(optparse.Option): |
111 """option that keeps track if it is seen""" | 111 """option that keeps track if it is seen""" |
112 # TODO: this should be configurable or something | 112 # TODO: this should be configurable or something |
113 def take_action(self, action, dest, opt, value, values, parser): | 113 def take_action(self, action, dest, opt, value, values, parser): |
114 optparse.Option.take_action(self, action, dest, opt, value, values, parser) | |
115 | |
116 # add the parsed option to the set of things parsed | |
117 if not hasattr(parser, 'parsed'): | |
118 parser.parsed = set() | |
119 parser.parsed.add(dest) | |
120 | 114 |
121 # switch on types | 115 # switch on types |
122 formatter = getattr(parser, 'cli_formatter') | 116 formatter = getattr(parser, 'cli_formatter') |
123 if formatter: | 117 if formatter: |
124 formatter = formatter(dest) | 118 formatter = formatter(dest) |
125 if formatter: | 119 if formatter: |
126 setattr(values, dest, formatter(getattr(values, dest))) | 120 value = formatter(value) |
121 | |
122 # call the optparse front-end | |
123 optparse.Option.take_action(self, action, dest, opt, value, values, parser) | |
124 | |
125 # add the parsed option to the set of things parsed | |
126 if not hasattr(parser, 'parsed'): | |
127 parser.parsed = set() | |
128 parser.parsed.add(dest) | |
127 | 129 |
128 ### plugins for option types | 130 ### plugins for option types |
129 | 131 |
130 class BaseCLI(object): | 132 class BaseCLI(object): |
131 """base_cli for all option types""" | 133 """base_cli for all option types""" |
200 | 202 |
201 class DictCLI(ListCLI): | 203 class DictCLI(ListCLI): |
202 | 204 |
203 delimeter = '=' | 205 delimeter = '=' |
204 | 206 |
207 def __call__(self, name, value): | |
208 | |
209 # optparse can't handle dict types OOTB | |
210 default = value.get('default') | |
211 if isinstance(default, dict): | |
212 value = copy.deepcopy(value) | |
213 value['default'] = default.items() | |
214 | |
215 return ListCLI.__call__(self, name, value) | |
216 | |
205 def take_action(self, value): | 217 def take_action(self, value): |
206 bad = [i for i in value if self.delimeter not in i] | 218 if self.delimeter not in value: |
207 if bad: | 219 raise AssertionError("Each value must be delimited by '%s': %s" % (self.delimeter, value)) |
208 raise AssertionError("Each value must be delimited by '%s': %s" % (self.delimeter, bad)) | 220 return value.split(self.delimeter, 1) |
209 return dict([i.split(self.delimeter, 1) for i in value]) | |
210 | 221 |
211 # TODO: 'dict'-type cli interface | 222 # TODO: 'dict'-type cli interface |
212 | 223 |
213 types = {bool: BoolCLI(), | 224 types = {bool: BoolCLI(), |
214 int: IntCLI(), | 225 int: IntCLI(), |
374 ### methods for optparse | 385 ### methods for optparse |
375 ### XXX could go in a subclass | 386 ### XXX could go in a subclass |
376 | 387 |
377 def cli_formatter(self, option): | 388 def cli_formatter(self, option): |
378 if option in self.option_dict: | 389 if option in self.option_dict: |
379 handler = self.types[self.option_dict[option].get('type')] | 390 handler = self.types[self.option_type(option)] |
380 return getattr(handler, 'take_action', lambda x: x) | 391 return getattr(handler, 'take_action', lambda x: x) |
381 | 392 |
382 def option_type(self, name): | 393 def option_type(self, name): |
383 """get the type of an option named `name`""" | 394 """get the type of an option named `name`""" |
384 | 395 |