def _get_option_error(name, spec, config_options): required = 'default' not in spec and 'env_fallback' not in spec if required and name not in config_options: return 'Option %s is missing' % name else: value = maybe_cast_list(config_options.get(name, empty), spec['type']) if value is not empty and not isinstance(value, spec['type']): return 'Wrong type: found {}, expected {}'.format( type(value), spec['type']) return None
def check_options(self): for option_name, option_spec in self.supported_options.iteritems(): option_value = maybe_cast_list(self.read_option(option_name), option_spec['type']) if option_value and not isinstance(option_value, option_spec['type']): raise ConfigurationError('Value for option %s should be of type: %s' % (option_name, option_spec['type'])) if 'default' in option_spec: continue if 'env_fallback' in option_spec and option_value is None: if not os.environ.get(option_spec['env_fallback']): raise ConfigurationError( 'Missing value for option {}. (tried also: {} from env)'.format( option_name, option_spec['env_fallback']) ) elif option_value is None: raise ConfigurationError('Missing value for option %s' % option_name)
def test_maybe_cast_list(self): # Don't do anything if it isn't a list assert maybe_cast_list(None, list) is None assert maybe_cast_list(50, list) == 50 # Works when we expect normal lists assert type(maybe_cast_list([], list)) == list # Cast to more specific list subclasses whenever possible assert type(maybe_cast_list([4, 5], (int_list))) == int_list assert type(maybe_cast_list(['asd', 5], (str_list))) == str_list # Return original value if can't cast assert type(maybe_cast_list(['asd', 5], (int_list))) == list # Try to cast to every list subclass assert type(maybe_cast_list(['asd', 5], (int_list, str_list))) == str_list
def check_options(self): for option_name, option_spec in self.supported_options.iteritems(): option_value = maybe_cast_list(self.read_option(option_name), option_spec['type']) if option_value and not isinstance(option_value, option_spec['type']): raise ConfigurationError( 'Value for option %s should be of type: %s' % (option_name, option_spec['type'])) if 'default' in option_spec: continue if 'env_fallback' in option_spec and option_value is None: if not os.environ.get(option_spec['env_fallback']): raise ConfigurationError( 'Missing value for option {}. (tried also: {} from env)' .format(option_name, option_spec['env_fallback'])) elif option_value is None: raise ConfigurationError('Missing value for option %s' % option_name)