From 54dc46fde4adb4a38fe1aa7b37f2e9283c413fa4 Mon Sep 17 00:00:00 2001 From: Morg42 <43153739+Morg42@users.noreply.github.com> Date: Tue, 23 Jul 2024 19:04:41 +0200 Subject: [PATCH] lib.logic: introduce class-wide logic conf filename --- lib/logic.py | 38 ++++++++++++++++---------------------- 1 file changed, 16 insertions(+), 22 deletions(-) diff --git a/lib/logic.py b/lib/logic.py index 892042539f..c687c51119 100644 --- a/lib/logic.py +++ b/lib/logic.py @@ -59,7 +59,7 @@ from lib.utils import Utils from lib.constants import PLUGIN_PARSE_LOGIC -from lib.constants import (YAML_FILE, CONF_FILE, DIR_LOGICS, BASE_LOGIC) +from lib.constants import (YAML_FILE, CONF_FILE, DIR_LOGICS, DIR_ETC, BASE_LOGIC, BASE_ADMIN) from lib.item import Items from lib.plugin import Plugins @@ -97,8 +97,9 @@ def __init__(self, smarthome, userlogicconf, envlogicconf): self._userlogicconf = userlogicconf self._env_dir = smarthome._env_dir self._envlogicconf = envlogicconf - self._etc_dir = smarthome.get_etcdir() - self._logic_dir = smarthome.get_logicsdir() + self._etc_dir = smarthome.get_config_dir(DIR_ETC) + self._logic_dir = smarthome.get_config_dir(DIR_LOGICS) + self._logic_conf = smarthome.get_config_file(BASE_LOGIC) self._workers = [] self._logics = {} #self._bytecode = {} @@ -126,7 +127,7 @@ def __init__(self, smarthome, userlogicconf, envlogicconf): self._load_logic(name, _config) # load /etc/admin.yaml - admconf_filename = os.path.join(self._get_etc_dir(), 'admin') + admconf_filename = self._sh.get_config_file(BASE_ADMIN) _admin_conf = shyaml.yaml_load_roundtrip(admconf_filename) if _admin_conf.get('logics', None) is None: self._groups = {} @@ -137,7 +138,7 @@ def __init__(self, smarthome, userlogicconf, envlogicconf): def _save_groups(self): # load /etc/admin.yaml - admconf_filename = os.path.join(self._get_etc_dir(), 'admin') + admconf_filename = self._sh.get_config_file(BASE_ADMIN) _admin_conf = shyaml.yaml_load_roundtrip(admconf_filename) _admin_conf['logics']['groups'] = self._groups shyaml.yaml_save_roundtrip(admconf_filename, _admin_conf, create_backup=True) @@ -618,8 +619,7 @@ def return_logictype(self, name): else: logger.info("return_logictype: name {} is not loaded".format(name)) # load /etc/logic.yaml if logic is not in the loaded logics - conf_filename = os.path.join(self._get_etc_dir(), BASE_LOGIC) - config = shyaml.yaml_load_roundtrip(conf_filename) + config = shyaml.yaml_load_roundtrip(self._logic_conf) if config is not None: if name in config: filename = config[name].get('filename', '') @@ -654,8 +654,7 @@ def return_defined_logics(self, withtype=False): logic_list = [] # load /etc/logic.yaml - conf_filename = os.path.join(self._get_etc_dir(), BASE_LOGIC) - config = shyaml.yaml_load_roundtrip(conf_filename) + config = shyaml.yaml_load_roundtrip(self._logic_conf) if config is not None: for section in config: @@ -729,8 +728,7 @@ def read_config_section(self, section): return False # load /etc/logic.yaml - conf_filename = os.path.join(self._get_etc_dir(), BASE_LOGIC) - _conf = shyaml.yaml_load_roundtrip(conf_filename) + _conf = shyaml.yaml_load_roundtrip(self._logic_conf) config_list = [] if _conf is not None: @@ -780,8 +778,7 @@ def set_config_section_key(self, section, key, value): """ # load /etc/logic.yaml - conf_filename = os.path.join(self._get_etc_dir(), BASE_LOGIC) - conf = shyaml.yaml_load_roundtrip(conf_filename) + conf = shyaml.yaml_load_roundtrip(self._logic_conf) logger.info("set_config_section_key: section={}, key={}, value={}".format(section, key, str(value))) if value == None: @@ -791,7 +788,7 @@ def set_config_section_key(self, section, key, value): conf[section][key] = value # save /etc/logic.yaml - shyaml.yaml_save_roundtrip(conf_filename, conf, True) + shyaml.yaml_save_roundtrip(self._logic_conf, conf, True) # activate visu_acl without reloading the logic if key == 'visu_acl': @@ -828,8 +825,7 @@ def update_config_section(self, active, section, config_list): return False # load /etc/logic.yaml - conf_filename = os.path.join(self._get_etc_dir(), BASE_LOGIC) - conf = shyaml.yaml_load_roundtrip(conf_filename) + conf = shyaml.yaml_load_roundtrip(self._logic_conf) if conf is None: conf = shyaml.get_emptynode() @@ -889,7 +885,7 @@ def update_config_section(self, active, section, config_list): if conf[section] == shyaml.get_emptynode(): conf[section] = None - shyaml.yaml_save_roundtrip(conf_filename, conf, True) + shyaml.yaml_save_roundtrip(self._logic_conf, conf, True) def _count_filename_uses(self, conf, filename): @@ -908,8 +904,7 @@ def _count_filename_uses(self, conf, filename): def filename_used_count(self, filename): # load /etc/logic.yaml - conf_filename = os.path.join(self._get_etc_dir(), BASE_LOGIC) - conf = shyaml.yaml_load_roundtrip(conf_filename) + conf = shyaml.yaml_load_roundtrip(self._logic_conf) count = self._count_filename_uses(conf, filename) return count @@ -939,8 +934,7 @@ def delete_logic(self, name, with_code=False): self.unload_logic(name) # load /etc/logic.yaml - conf_filename = os.path.join(self._get_etc_dir(), BASE_LOGIC) - conf = shyaml.yaml_load_roundtrip(conf_filename) + conf = shyaml.yaml_load_roundtrip(self._logic_conf) section = conf.get(name, None) if section is None: @@ -974,7 +968,7 @@ def delete_logic(self, name, with_code=False): logger.info(f"delete_logic: Section '{name}' from configuration deleted") # save /etc/logic.yaml - shyaml.yaml_save_roundtrip(conf_filename, conf, True) + shyaml.yaml_save_roundtrip(self._logic_conf, conf, True) return True