From 5c7a43091596b35ae620ec454569fc0a34ab1f1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Schmidts?= Date: Sun, 22 Oct 2017 21:57:54 +0200 Subject: [PATCH] adding test, fixing config export --- tests/test_the_conf.py | 23 +++++++++++++++++++++++ the_conf/node.py | 3 +++ the_conf/the_conf.py | 27 +++++++++++++++------------ 3 files changed, 41 insertions(+), 12 deletions(-) diff --git a/tests/test_the_conf.py b/tests/test_the_conf.py index ce36c83..8f581b4 100644 --- a/tests/test_the_conf.py +++ b/tests/test_the_conf.py @@ -40,6 +40,19 @@ def test_conf_from_obj(self): tc.option = 1 self.assertEqual('1', tc.option) + def test_casting(self): + metaconf = {'parameters': [ + {'option1': {'type': str, 'default': 'a'}}, + {'option2': {'type': int, 'default': '1'}}, + ], + 'config_files': []} + tc = TheConf(metaconf, cmd_line_opts=[]) + self.assertEqual('a', tc.option1) + self.assertEqual(1, tc.option2) + tc.option1, tc.option2 = 'b', '2' + self.assertEqual('b', tc.option1) + self.assertEqual(2, tc.option2) + def test_trigger_error_w_required(self): metaconf = {'parameters': [ {'option': {'type': str, 'required': True}}], @@ -48,3 +61,13 @@ def test_trigger_error_w_required(self): TheConf, metaconf, cmd_line_opts=[]) tc = TheConf(metaconf, cmd_line_opts=['--option=stuff']) self.assertEqual('stuff', tc.option) + + def test_extract_config(self): + metaconf = {'parameters': [{'option': [ + {'option': {'type': str, 'default': 'a'}}]}], + 'config_files': []} + tc = TheConf(metaconf, cmd_line_opts=[]) + self.assertEqual('a', tc.option.option) + self.assertEqual({}, tc.extract_config()) + tc.option.option = 'b' + self.assertEqual({'option': {'option': 'b'}}, tc.extract_config()) diff --git a/the_conf/node.py b/the_conf/node.py index fa5229f..69d6c85 100644 --- a/the_conf/node.py +++ b/the_conf/node.py @@ -61,6 +61,9 @@ def _load_parameter(self, name, settings): if has_default and settings['required']: raise ValueError( "%r required parameter can't have default value" % path) + + if 'type' in settings and 'default' in settings: + settings['default'] = settings['type'](settings['default']) self._parameters[name] = settings def _set_to_path(self, path, value, overwrite=False): diff --git a/the_conf/the_conf.py b/the_conf/the_conf.py index ce0f969..59a2c1d 100644 --- a/the_conf/the_conf.py +++ b/the_conf/the_conf.py @@ -71,20 +71,23 @@ def load(self): raise ValueError('loading finished and %r is not set' % '.'.join(path)) - def write(self, config_file=None): - if config_file is None and not self._config_files: - raise ValueError('no config file to write in') + def extract_config(self): config = {} for paths, value, param in self._get_path_val_param(): if value is node.NoValue: continue + if 'default' in param and value == param['default']: + continue curr_config = config - for path in paths: - if path != paths[-1]: - if path not in curr_config: - curr_config[path] = {} - curr_config = curr_config[path] - else: - curr_config[path] = value - - files.write(config, config_file or self._config_files[0]) + for path in paths[:-1]: + curr_config[path] = {} + curr_config = curr_config[path] + curr_config[paths[-1]] = value + return config + + def write(self, config_file=None): + if config_file is None and not self._config_files: + raise ValueError('no config file to write in') + + files.write(self.extract_config(), + config_file or self._config_files[0])