Mercurial > hg > configuration
changeset 133:f01d351ccf2b
STUB: configuration/configuration.py
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Sun, 30 Mar 2014 19:53:20 -0700 |
parents | af5e83a4763b |
children | 591701680deb |
files | configuration/configuration.py |
diffstat | 1 files changed, 17 insertions(+), 17 deletions(-) [+] |
line wrap: on
line diff
--- a/configuration/configuration.py Sun Mar 30 19:28:09 2014 -0700 +++ b/configuration/configuration.py Sun Mar 30 19:53:20 2014 -0700 @@ -170,21 +170,18 @@ if not flags: args = ['--no-%s' % name] if not help: - kw['help'] = 'disable %s' % name + kw['help'] = 'disable {}'.format(name) else: kw['action'] = 'store_true' if not help: - kw['help'] = 'enable %s' % name + kw['help'] = 'enable {}'.format(name) return args, kw + class ListCLI(BaseCLI): def __call__(self, name, value): args, kw = BaseCLI.__call__(self, name, value) - - # TODO: could use 'extend' - # - http://hg.mozilla.org/build/mozharness/file/5f44ba08f4be/mozharness/base/config.py#l41 - kw['action'] = 'append' return args, kw @@ -221,6 +218,7 @@ raise AssertionError("Each value must be delimited by '%s': %s" % (self.delimeter, value)) return value.split(self.delimeter, 1) +# types of CLI arguments types = {bool: BoolCLI(), int: IntCLI(), float: FloatCLI(), @@ -231,6 +229,7 @@ __all__ += [i.__class__.__name__ for i in types.values()] + class Configuration(optparse.OptionParser): """declarative configuration object""" @@ -251,11 +250,10 @@ # sanity check if isinstance(self.options, dict): self.option_dict = self.options - elif isinstance(self.options, list): - # XXX could also be tuple, etc + elif isinstance(self.options, (list, tuple)): self.option_dict = dict(self.options) else: - raise NotImplementedError + raise NotImplementedError("Configuration: `options` should be castable to a dict") # setup configuration self.config = {} @@ -296,10 +294,12 @@ help="Output configuration file; Formats: %s" % formats)) - ### methods for iteration - ### TODO: make the class a real iterator + ### iteration def items(self): + """items in options""" + # TODO: make the class a real iterator + # allow options to be a list of 2-tuples if isinstance(self.options, dict): return self.options.items() @@ -315,7 +315,7 @@ # ensure options in configuration are in self.options unknown_options = [i for i in config if i not in self.option_dict] if unknown_options: - raise UnknownOptionException("Unknown options: %s" % ', '.join(unknown_options)) + raise UnknownOptionException("Unknown options: {}".format(', '.join(unknown_options))) # ensure options are of the right type (if specified) for key, value in config.items(): @@ -334,7 +334,7 @@ try: config[key] = _type(value) except BaseException, e: - raise TypeCastException("Could not coerce %s, %s, to type %s: %s" % (key, value, _type.__name__, e)) + raise TypeCastException("Could not coerce '%s'=%s, to type %s: %s" % (key, value, _type.__name__, e)) return config @@ -348,11 +348,11 @@ if isinstance(required, basestring): required_message = required else: - required_message = "Parameter %s is required but not present" % key + required_message = "Parameter {} is required but not present".format(key) # TODO: this should probably raise all missing values vs # one by one raise MissingValueException(required_message) - # TODO: configuration should be locked after this is called + ### methods for adding configuration @@ -372,7 +372,6 @@ def __call__(self, *args): """add items to configuration and check it""" - # TODO: configuration should be locked after this is called # start with defaults self.config = self.default_config() @@ -428,7 +427,7 @@ if 'default' in value: default = value['default'] if default is None: - return None + return None # not <type 'NoneType'> return type(value['default']) def optparse_options(self, parser): @@ -520,6 +519,7 @@ # TODO: have a way of specifying format other than filename self.serialize(dump) + ### serialization/deserialization def formats(self):