Skip to content

Commit

Permalink
adding test, fixing config export
Browse files Browse the repository at this point in the history
  • Loading branch information
jaesivsm committed Oct 22, 2017
1 parent 070630d commit 5c7a430
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 12 deletions.
23 changes: 23 additions & 0 deletions tests/test_the_conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}}],
Expand All @@ -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())
3 changes: 3 additions & 0 deletions the_conf/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
27 changes: 15 additions & 12 deletions the_conf/the_conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])

0 comments on commit 5c7a430

Please sign in to comment.