daemon wont create a config file anymore

This commit is contained in:
sezanzeb 2020-11-25 19:20:44 +01:00
parent 4e08221058
commit 1a41166770
2 changed files with 18 additions and 8 deletions

View File

@ -46,28 +46,30 @@ class _Config:
def set_autoload_preset(self, device, preset): def set_autoload_preset(self, device, preset):
"""Set a preset to be automatically applied on start.""" """Set a preset to be automatically applied on start."""
if self._config.get('autoload') is None:
self._config['autoload'] = {}
self._config['autoload'][device] = preset self._config['autoload'][device] = preset
def iterate_autoload_presets(self): def iterate_autoload_presets(self):
"""get tuples of (device, preset).""" """get tuples of (device, preset)."""
return self._config['autoload'].items() return self._config.get('autoload', {}).items()
def load_config(self): def load_config(self):
"""Load the config from the file system.""" """Load the config from the file system."""
self.clear_config()
if not os.path.exists(CONFIG_PATH): if not os.path.exists(CONFIG_PATH):
# has not yet been saved # treated like an empty config
logger.info('Creating initial config') logger.info('Config file "%s" doesn\'t exist')
self._config = copy.deepcopy(INITIAL_CONFIG)
self.save_config()
return return
with open(CONFIG_PATH, 'r') as file: with open(CONFIG_PATH, 'r') as file:
self._config = copy.deepcopy(INITIAL_CONFIG)
self._config.update(json.load(file)) self._config.update(json.load(file))
logger.info('Loaded config from %s', CONFIG_PATH) logger.info('Loaded config from "%s"', CONFIG_PATH)
def clear_config(self): def clear_config(self):
"""Needed for tests.""" """Reset the configuration to the initial values."""
self._config = copy.deepcopy(INITIAL_CONFIG) self._config = copy.deepcopy(INITIAL_CONFIG)
def save_config(self): def save_config(self):

View File

@ -31,6 +31,9 @@ class TestConfig(unittest.TestCase):
config.save_config() config.save_config()
def test_autoload(self): def test_autoload(self):
del config._config['autoload']
self.assertEqual(len(config.iterate_autoload_presets()), 0)
config.set_autoload_preset('d1', 'a') config.set_autoload_preset('d1', 'a')
self.assertEqual(len(config.iterate_autoload_presets()), 1) self.assertEqual(len(config.iterate_autoload_presets()), 1)
@ -46,6 +49,11 @@ class TestConfig(unittest.TestCase):
) )
def test_save_load(self): def test_save_load(self):
self.assertEqual(len(config.iterate_autoload_presets()), 0)
config.load_config()
self.assertEqual(len(config.iterate_autoload_presets()), 0)
config.set_autoload_preset('d1', 'a') config.set_autoload_preset('d1', 'a')
config.set_autoload_preset('d2', 'b') config.set_autoload_preset('d2', 'b')
config.save_config() config.save_config()