From 9e41eff187b68a19375ce7c79b5967f7b2699e60 Mon Sep 17 00:00:00 2001 From: Trygve Aaberge Date: Mon, 11 Aug 2014 14:48:04 +0200 Subject: [PATCH] config: Support passing directories to mopidy --config --- docs/changelog.rst | 2 ++ mopidy/config/__init__.py | 40 ++++++++++++++++++++++-------------- tests/config/test_config.py | 6 ++++++ tests/data/conf.d/file1.conf | 2 ++ tests/data/conf.d/file2.conf | 2 ++ 5 files changed, 37 insertions(+), 15 deletions(-) create mode 100644 tests/data/conf.d/file1.conf create mode 100644 tests/data/conf.d/file2.conf diff --git a/docs/changelog.rst b/docs/changelog.rst index 8409054a..42075ecd 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -17,6 +17,8 @@ Bug fix release. - Logging: Fix that some loggers would be disabled if :confval:`logging/config_file` was set. (Fixes: :issue:`740`) +- Configuration: :option:`mopidy --config` now supports directories. + v0.19.3 (2014-08-03) ==================== diff --git a/mopidy/config/__init__.py b/mopidy/config/__init__.py index 3b63a1ab..09cdc0ca 100644 --- a/mopidy/config/__init__.py +++ b/mopidy/config/__init__.py @@ -116,21 +116,14 @@ def _load(files, defaults, overrides): parser.readfp(io.BytesIO(default)) # Load config from a series of config files - for filename in files: - try: - with io.open(filename, 'rb') as filehandle: - parser.readfp(filehandle) - except configparser.MissingSectionHeaderError as e: - logger.warning('%s does not have a config section, not loaded.', - filename) - except configparser.ParsingError as e: - linenos = ', '.join(str(lineno) for lineno, line in e.errors) - logger.warning( - '%s has errors, line %s has been ignored.', filename, linenos) - except IOError: - # TODO: if this is the initial load of logging config we might not - # have a logger at this point, we might want to handle this better. - logger.debug('Config file %s not found; skipping', filename) + for name in files: + if os.path.isdir(name): + for filename in os.listdir(name): + filename = os.path.join(name, filename) + if os.path.isfile(filename): + _load_file(parser, filename) + else: + _load_file(parser, name) # If there have been parse errors there is a python bug that causes the # values to be lists, this little trick coerces these into strings. @@ -146,6 +139,23 @@ def _load(files, defaults, overrides): return raw_config +def _load_file(parser, filename): + try: + with io.open(filename, 'rb') as filehandle: + parser.readfp(filehandle) + except configparser.MissingSectionHeaderError as e: + logger.warning('%s does not have a config section, not loaded.', + filename) + except configparser.ParsingError as e: + linenos = ', '.join(str(lineno) for lineno, line in e.errors) + logger.warning( + '%s has errors, line %s has been ignored.', filename, linenos) + except IOError: + # TODO: if this is the initial load of logging config we might not + # have a logger at this point, we might want to handle this better. + logger.debug('Config file %s not found; skipping', filename) + + def _validate(raw_config, schemas): # Get validated config config = {} diff --git a/tests/config/test_config.py b/tests/config/test_config.py index 52035825..6a0ee12b 100644 --- a/tests/config/test_config.py +++ b/tests/config/test_config.py @@ -60,6 +60,12 @@ class LoadConfigTest(unittest.TestCase): result = config._load([file1, file2], [], []) self.assertEqual(expected, result) + def test_load_directory(self): + directory = path_to_data_dir('conf.d') + expected = {'foo': {'bar': 'baz'}, 'foo2': {'bar': 'baz'}} + result = config._load([directory], [], []) + self.assertEqual(expected, result) + def test_load_file_with_utf8(self): expected = {'foo': {'bar': 'æøå'.encode('utf-8')}} result = config._load([path_to_data_dir('file3.conf')], [], []) diff --git a/tests/data/conf.d/file1.conf b/tests/data/conf.d/file1.conf new file mode 100644 index 00000000..e6396bff --- /dev/null +++ b/tests/data/conf.d/file1.conf @@ -0,0 +1,2 @@ +[foo] +bar = baz diff --git a/tests/data/conf.d/file2.conf b/tests/data/conf.d/file2.conf new file mode 100644 index 00000000..ef189703 --- /dev/null +++ b/tests/data/conf.d/file2.conf @@ -0,0 +1,2 @@ +[foo2] +bar = baz