config: Address review comments.

This commit is contained in:
Thomas Adamcik 2013-04-01 21:19:38 +02:00
parent e00b7a63f0
commit 3509ec4b37
3 changed files with 22 additions and 21 deletions

View File

@ -27,9 +27,6 @@ class ConfigError(MopidyException):
def __getitem__(self, key): def __getitem__(self, key):
return self._errors[key] return self._errors[key]
def __iter__(self):
return self._errors.iterkeys()
@property @property
def message(self): def message(self):
lines = [] lines = []

View File

@ -87,6 +87,9 @@ class String(ConfigValue):
validate_choice(value, self.choices) validate_choice(value, self.choices)
return value return value
def serialize(self, value):
return value.encode('utf-8')
class Integer(ConfigValue): class Integer(ConfigValue):
def deserialize(self, value): def deserialize(self, value):
@ -121,10 +124,10 @@ class List(ConfigValue):
if '\n' in value: if '\n' in value:
return re.split(r'\s*\n\s*', value.strip()) return re.split(r'\s*\n\s*', value.strip())
else: else:
return re.split(r',\s*', value.strip()) return re.split(r'\s*,\s*', value.strip())
def serialize(self, value): def serialize(self, value):
return '\n '.join(value) return '\n '.join(v.encode('utf-8') for v in value)
class LogLevel(ConfigValue): class LogLevel(ConfigValue):
@ -148,7 +151,7 @@ class Hostname(ConfigValue):
try: try:
socket.getaddrinfo(value, None) socket.getaddrinfo(value, None)
except socket.error: except socket.error:
raise ValueError('must be a resolveable hostname or valid IP.') raise ValueError('must be a resolveable hostname or valid IP')
return value return value
@ -160,13 +163,14 @@ class Port(Integer):
class ConfigSchema(object): class ConfigSchema(object):
"""Logical group of config values that corespond to a config section. """Logical group of config values that correspond to a config section.
Schemas are setup by assigning config keys with config values to instances. Schemas are set up by assigning config keys with config values to instances.
Once setup `convert` can be called with a list of `(key, value)` tuples to Once setup :meth:`convert` can be called with a list of `(key, value)` tuples to
process. For convienience we also support a `format` method that can used process. For convienience we also support :meth:`format` method that can used
for printing out the converted values. for printing out the converted values.
""" """
# TODO: Use collections.OrderedDict once 2.6 support is gone (#344)
def __init__(self): def __init__(self):
self._schema = {} self._schema = {}
self._order = [] self._order = []
@ -212,7 +216,7 @@ class ConfigSchema(object):
class ExtensionConfigSchema(ConfigSchema): class ExtensionConfigSchema(ConfigSchema):
"""Sub-classed ConfigSchema for use in extensions. """Sub-classed :class:`ConfigSchema` for use in extensions.
Ensures that `enabled` config value is present and that section name is Ensures that `enabled` config value is present and that section name is
prefixed with ext. prefixed with ext.
@ -229,17 +233,17 @@ class LogLevelConfigSchema(object):
"""Special cased schema for handling a config section with loglevels. """Special cased schema for handling a config section with loglevels.
Expects the config keys to be logger names and the values to be log levels Expects the config keys to be logger names and the values to be log levels
as understood by the LogLevel config value. Does not sub-class ConfigSchema, as understood by the :class:`LogLevel` config value. Does not sub-class
but implements the same interface. :class:`ConfigSchema`, but implements the same interface.
""" """
def __init__(self): def __init__(self):
self._configvalue = LogLevel() self._config_value = LogLevel()
def format(self, name, values): def format(self, name, values):
lines = ['[%s]' % name] lines = ['[%s]' % name]
for key, value in sorted(values.items()): for key, value in sorted(values.items()):
if value is not None: if value is not None:
lines.append('%s = %s' % (key, self._configvalue.format(value))) lines.append('%s = %s' % (key, self._config_value.format(value)))
return '\n'.join(lines) return '\n'.join(lines)
def convert(self, items): def convert(self, items):
@ -249,7 +253,7 @@ class LogLevelConfigSchema(object):
for key, value in items: for key, value in items:
try: try:
if value.strip(): if value.strip():
values[key] = self._configvalue.deserialize(value) values[key] = self._config_value.deserialize(value)
except ValueError as e: # deserialization failed except ValueError as e: # deserialization failed
errors[key] = str(e) errors[key] = str(e)

View File

@ -182,11 +182,11 @@ class ListTest(unittest.TestCase):
class BooleanTest(unittest.TestCase): class BooleanTest(unittest.TestCase):
levels = {'critical' : logging.CRITICAL, levels = {'critical': logging.CRITICAL,
'error' : logging.ERROR, 'error': logging.ERROR,
'warning' : logging.WARNING, 'warning': logging.WARNING,
'info' : logging.INFO, 'info': logging.INFO,
'debug' : logging.DEBUG} 'debug': logging.DEBUG}
def test_deserialize_converts_to_numeric_loglevel(self): def test_deserialize_converts_to_numeric_loglevel(self):
value = config.LogLevel() value = config.LogLevel()