Mercurial > hg > configuration
comparison configuration/configuration.py @ 101:f4590492cb4c
handle load option a bit better
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Mon, 30 Apr 2012 13:21:48 -0700 |
parents | aa5c663dd8b7 |
children | c530f6265deb |
comparison
equal
deleted
inserted
replaced
100:aa5c663dd8b7 | 101:f4590492cb4c |
---|---|
221 __all__ += [i.__class__.__name__ for i in types.values()] | 221 __all__ += [i.__class__.__name__ for i in types.values()] |
222 | 222 |
223 class Configuration(optparse.OptionParser): | 223 class Configuration(optparse.OptionParser): |
224 """declarative configuration object""" | 224 """declarative configuration object""" |
225 | 225 |
226 options = {} # configuration basis | 226 options = {} # configuration basis definition |
227 load_option = 'load' # where to put the load option | |
227 | 228 |
228 def __init__(self, configuration_providers=configuration_providers, types=types, load=None, dump='--dump', **parser_args): | 229 def __init__(self, configuration_providers=configuration_providers, types=types, load=None, dump='--dump', **parser_args): |
229 | 230 |
230 # sanity check | 231 # sanity check |
231 if isinstance(self.options, dict): | 232 if isinstance(self.options, dict): |
258 self.parsed = set() | 259 self.parsed = set() |
259 self.optparse_options(self) | 260 self.optparse_options(self) |
260 # add option(s) for configuration_providers | 261 # add option(s) for configuration_providers |
261 if load: | 262 if load: |
262 self.add_option(load, | 263 self.add_option(load, |
263 dest='load', action='append', | 264 dest=self.load_option, action='append', |
264 help="load configuration from a file") | 265 help="load configuration from a file") |
265 | 266 |
266 # add an option for dumping | 267 # add an option for dumping |
267 formats = self.formats() | 268 formats = self.formats() |
268 if formats and dump: | 269 if formats and dump: |
392 # get CLI configuration options | 393 # get CLI configuration options |
393 cli_config = dict([(key, value) for key, value in options.__dict__.items() | 394 cli_config = dict([(key, value) for key, value in options.__dict__.items() |
394 if key in self.option_dict and key in self.parsed]) | 395 if key in self.option_dict and key in self.parsed]) |
395 | 396 |
396 # deserialize configuration | 397 # deserialize configuration |
397 configuration_files = getattr(options, 'load', args) | 398 configuration_files = getattr(options, self.load_option, args) |
399 if not configuration_files: | |
400 configuration_files = [] | |
401 if isinstance(configuration_files, basestring): | |
402 configuration_files = [configuration_files] | |
398 missing = [i for i in configuration_files | 403 missing = [i for i in configuration_files |
399 if not os.path.exists(i)] | 404 if not os.path.exists(i)] |
400 if missing: | 405 if missing: |
401 self.error("Missing files: %s" % ', '.join(missing)) | 406 self.error("Missing files: %s" % ', '.join(missing)) |
402 config = [] | 407 config = [] |