# HG changeset patch # User Jeff Hammel # Date 1332950582 25200 # Node ID c831eb58fb5298c1e5f3a739a5a8c8b8a3d15b59 # Parent f3b3f4046a8a7ca459250760e8332a679750bac8 minor fixes diff -r f3b3f4046a8a -r c831eb58fb52 configuration/configuration.py --- a/configuration/configuration.py Wed Mar 28 08:38:16 2012 -0700 +++ b/configuration/configuration.py Wed Mar 28 09:03:02 2012 -0700 @@ -9,7 +9,7 @@ import sys import optparse -# imports for contigent configuration providers +# imports for configuration providers try: import json except ImportError: @@ -22,10 +22,20 @@ except ImportError: yaml = None -__all__ = ['Configuration', 'configuration_providers', 'types', 'MissingValueException', 'ConfigurationProviderException', 'TypeCastException', 'ConfigurationOption'] +__all__ = ['Configuration', + 'configuration_providers', + 'types', + 'UnknownOptionException', + 'MissingValueException', + 'ConfigurationProviderException', + 'TypeCastException', + 'ConfigurationOption'] ### exceptions +class UnknownOptionException(Exception): + """exception raised when a non-configuration value is present in the configuration""" + class MissingValueException(Exception): """exception raised when a required value is missing""" @@ -35,6 +45,7 @@ class TypeCastException(Exception): """exception raised when a configuration item cannot be coerced to a type""" + ### configuration providers for serialization/deserialization configuration_providers = [] @@ -219,7 +230,7 @@ ### methods for iteration - ### TODO: make this a real iterator + ### TODO: make the class a real iterator def items(self): # TODO: allow options to be a list of 2-tuples @@ -231,18 +242,18 @@ """ check validity of configuration to be added """ - # TODO: should probably deepcopy config # ensure options in configuration are in self.options unknown_options = [i for i in config if i not in self.options] if unknown_options: - # TODO: more specific error type - raise Exception("Unknown options: %s" % ', '.join(unknown_options)) + raise UnknownOptionException("Unknown options: %s" % ', '.join(unknown_options)) - # TODO: ensure options are of the right type (if specified) + # ensure options are of the right type (if specified) for key, value in config.items(): _type = self.options[key].get('type') - if _type is not None: + if _type is None and 'default' in self.options[key]: + _type = type(self.options[key]['default']) + if _type is not None and not isinstance(value, _type): try: config[key] = _type(value) except BaseException, e: @@ -252,6 +263,7 @@ def validate(self): """validate resultant configuration""" + for key, value in self.options.items(): if key not in self.config: required = value.get('required') @@ -260,8 +272,7 @@ required_message = required else: required_message = "Parameter %s is required but not present" % key - # TODO: more specific exception - # Also, this should probably raise all missing values vs + # 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 @@ -270,6 +281,7 @@ def __call__(self, *args): """add items to configuration and check it""" + for config in args: self.add(config)