Skip to content

Commit

Permalink
lib.logic: introduce class-wide logic conf filename
Browse files Browse the repository at this point in the history
  • Loading branch information
Morg42 committed Jul 23, 2024
1 parent 5217ca6 commit 54dc46f
Showing 1 changed file with 16 additions and 22 deletions.
38 changes: 16 additions & 22 deletions lib/logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = {}
Expand Down Expand Up @@ -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 = {}
Expand All @@ -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)
Expand Down Expand Up @@ -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', '')
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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':
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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):
Expand All @@ -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

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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


Expand Down

0 comments on commit 54dc46f

Please sign in to comment.