From 6e42a381cdb1884eb17e7433301b37fa7f243822 Mon Sep 17 00:00:00 2001 From: Thomas Adamcik Date: Sat, 13 Apr 2013 21:46:15 +0200 Subject: [PATCH] config: Add name to schemas --- mopidy/config/__init__.py | 8 ++++---- mopidy/config/schemas.py | 18 ++++++++++-------- mopidy/ext.py | 2 +- tests/config/schemas_test.py | 16 ++++++++-------- 4 files changed, 23 insertions(+), 21 deletions(-) diff --git a/mopidy/config/__init__.py b/mopidy/config/__init__.py index 48334942..450ec626 100644 --- a/mopidy/config/__init__.py +++ b/mopidy/config/__init__.py @@ -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) diff --git a/mopidy/config/schemas.py b/mopidy/config/schemas.py index b074e79a..19b5d665 100644 --- a/mopidy/config/schemas.py +++ b/mopidy/config/schemas.py @@ -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' % ( diff --git a/mopidy/ext.py b/mopidy/ext.py index c90e75e3..aa8ec5e0 100644 --- a/mopidy/ext.py +++ b/mopidy/ext.py @@ -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""" diff --git a/tests/config/schemas_test.py b/tests/config/schemas_test.py index e7d6dde1..487136c2 100644 --- a/tests/config/schemas_test.py +++ b/tests/config/schemas_test.py @@ -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)