config: Add name to schemas

This commit is contained in:
Thomas Adamcik 2013-04-13 21:46:15 +02:00
parent 5fd4c18792
commit 6e42a381cd
4 changed files with 23 additions and 21 deletions

View File

@ -14,19 +14,19 @@ from mopidy.utils import path
logger = logging.getLogger('mopdiy.config')
config_schemas = {} # TODO: use ordered dict or list?
config_schemas['logging'] = ConfigSchema()
config_schemas['logging'] = ConfigSchema('logging')
config_schemas['logging']['console_format'] = String()
config_schemas['logging']['debug_format'] = String()
config_schemas['logging']['debug_file'] = Path()
config_schemas['logging.levels'] = LogLevelConfigSchema()
config_schemas['logging.levels'] = LogLevelConfigSchema('loglevels')
config_schemas['audio'] = ConfigSchema()
config_schemas['audio'] = ConfigSchema('audio')
config_schemas['audio']['mixer'] = String()
config_schemas['audio']['mixer_track'] = String(optional=True)
config_schemas['audio']['output'] = String()
config_schemas['proxy'] = ConfigSchema()
config_schemas['proxy'] = ConfigSchema('proxy')
config_schemas['proxy']['hostname'] = Hostname(optional=True)
config_schemas['proxy']['username'] = String(optional=True)
config_schemas['proxy']['password'] = String(optional=True, secret=True)

View File

@ -45,7 +45,8 @@ class ConfigSchema(object):
:meth:`format` method that can used for printing out the converted values.
"""
# TODO: Use collections.OrderedDict once 2.6 support is gone (#344)
def __init__(self):
def __init__(self, name):
self.name = name
self._schema = {}
self._order = []
@ -57,10 +58,10 @@ class ConfigSchema(object):
def __getitem__(self, key):
return self._schema[key]
def format(self, name, values):
def format(self, values):
# TODO: should the output be encoded utf-8 since we use that in
# serialize for strings?
lines = ['[%s]' % name]
lines = ['[%s]' % self.name]
for key in self._order:
value = values.get(key)
if value is not None:
@ -97,8 +98,8 @@ class ExtensionConfigSchema(ConfigSchema):
Ensures that ``enabled`` config value is present.
"""
def __init__(self):
super(ExtensionConfigSchema, self).__init__()
def __init__(self, name):
super(ExtensionConfigSchema, self).__init__(name)
self['enabled'] = types.Boolean()
@ -109,11 +110,12 @@ class LogLevelConfigSchema(object):
as understood by the :class:`LogLevel` config value. Does not sub-class
:class:`ConfigSchema`, but implements the same interface.
"""
def __init__(self):
def __init__(self, name):
self.name = name
self._config_value = types.LogLevel()
def format(self, name, values):
lines = ['[%s]' % name]
def format(self, values):
lines = ['[%s]' % self.name]
for key, value in sorted(values.items()):
if value is not None:
lines.append('%s = %s' % (

View File

@ -24,7 +24,7 @@ class Extension(object):
def get_config_schema(self):
"""TODO"""
return config_lib.ExtensionConfigSchema()
return config_lib.ExtensionConfigSchema(self.ext_name)
def validate_environment(self):
"""TODO"""

View File

@ -11,7 +11,7 @@ from tests import unittest
class ConfigSchemaTest(unittest.TestCase):
def setUp(self):
self.schema = schemas.ConfigSchema()
self.schema = schemas.ConfigSchema('test')
self.schema['foo'] = mock.Mock()
self.schema['bar'] = mock.Mock()
self.schema['baz'] = mock.Mock()
@ -22,8 +22,8 @@ class ConfigSchemaTest(unittest.TestCase):
self.schema['bar'].format.return_value = 'asd'
self.schema['baz'].format.return_value = 'zxc'
expected = ['[qwerty]', 'foo = qwe', 'bar = asd', 'baz = zxc']
result = self.schema.format('qwerty', self.values)
expected = ['[test]', 'foo = qwe', 'bar = asd', 'baz = zxc']
result = self.schema.format(self.values)
self.assertEqual('\n'.join(expected), result)
def test_format_unkwown_value(self):
@ -32,7 +32,7 @@ class ConfigSchemaTest(unittest.TestCase):
self.schema['baz'].format.return_value = 'zxc'
self.values['unknown'] = 'rty'
result = self.schema.format('qwerty', self.values)
result = self.schema.format(self.values)
self.assertNotIn('unknown = rty', result)
def test_convert(self):
@ -88,7 +88,7 @@ class ConfigSchemaTest(unittest.TestCase):
class ExtensionConfigSchemaTest(unittest.TestCase):
def test_schema_includes_enabled(self):
schema = schemas.ExtensionConfigSchema()
schema = schemas.ExtensionConfigSchema('test')
self.assertIsInstance(schema['enabled'], types.Boolean)
@ -101,10 +101,10 @@ class LogLevelConfigSchemaTest(unittest.TestCase):
self.assertEqual(logging.INFO, result['baz'])
def test_format(self):
schema = schemas.LogLevelConfigSchema()
schema = schemas.LogLevelConfigSchema('test')
values = {'foo.bar': logging.DEBUG, 'baz': logging.INFO}
expected = ['[levels]', 'baz = info', 'foo.bar = debug']
result = schema.format('levels', values)
expected = ['[test]', 'baz = info', 'foo.bar = debug']
result = schema.format(values)
self.assertEqual('\n'.join(expected), result)