From 5217ca68f67eed7f121d8d8c25369c37947f6b2a Mon Sep 17 00:00:00 2001 From: Morg42 <43153739+Morg42@users.noreply.github.com> Date: Tue, 23 Jul 2024 17:54:23 +0200 Subject: [PATCH] lib.smarthome: add generic get conffile/confdir method --- lib/constants.py | 3 ++- lib/smarthome.py | 50 ++++++++++++++++++++++++++++++++++-------------- 2 files changed, 38 insertions(+), 15 deletions(-) diff --git a/lib/constants.py b/lib/constants.py index 8c60cefe7b..11c6c358a3 100644 --- a/lib/constants.py +++ b/lib/constants.py @@ -112,9 +112,10 @@ BASE_LOGIC = 'logic' BASE_STRUCT = 'struct' BASE_HOLIDAY = 'holidays' +BASE_ADMIN = 'admin' DIRS = (DIR_VAR, DIR_LIB, DIR_CACHE, DIR_ENV, DIR_TPL, DIR_PLUGINS, DIR_MODULES, DIR_ETC, DIR_ITEMS, DIR_STRUCTS, DIR_LOGICS, DIR_UF, DIR_SCENES) -BASES = (BASE_SH, BASE_LOG, BASE_MODULE, BASE_PLUGIN, BASE_LOGIC) +BASES = (BASE_SH, BASE_LOG, BASE_MODULE, BASE_PLUGIN, BASE_LOGIC, BASE_STRUCT, BASE_HOLIDAY, BASE_ADMIN) FILES = DIRS + BASES #attributes for 'autotimer' parameter diff --git a/lib/smarthome.py b/lib/smarthome.py index cfc7347cde..6bbf255ec1 100644 --- a/lib/smarthome.py +++ b/lib/smarthome.py @@ -98,7 +98,7 @@ import lib.shyaml from lib.shpypi import Shpypi from lib.triggertimes import TriggerTimes -from lib.constants import (YAML_FILE, CONF_FILE, DEFAULT_FILE, BASE_LOG, BASE_LOGIC, BASE_MODULE, BASE_PLUGIN, BASE_SH, DIR_CACHE, DIR_ENV, DIR_ETC, DIR_ITEMS, DIR_LIB, DIR_LOGICS, DIR_PLUGINS, DIR_SCENES, DIR_STRUCTS, DIR_TPL, DIR_UF, DIR_VAR) +from lib.constants import (YAML_FILE, CONF_FILE, DEFAULT_FILE, DIRS, BASES, BASE_LOG, BASE_LOGIC, BASE_MODULE, BASE_PLUGIN, BASE_SH, DIR_CACHE, DIR_ENV, DIR_ETC, DIR_ITEMS, DIR_LIB, DIR_LOGICS, DIR_MODULES, DIR_PLUGINS, DIR_SCENES, DIR_STRUCTS, DIR_TPL, DIR_UF, DIR_VAR) import lib.userfunctions as uf from lib.systeminfo import Systeminfo @@ -165,6 +165,7 @@ def initialize_dir_vars(self): self._var_dir = os.path.join(self._base_dir, DIR_VAR) self._lib_dir = os.path.join(self._base_dir, DIR_LIB) self._plugins_dir = os.path.join(self._base_dir, DIR_PLUGINS) + self._modules_dir = os.path.join(self._base_dir, DIR_MODULES) # env and var dirs self._env_dir = os.path.join(self._lib_dir, DIR_ENV + os.path.sep) @@ -535,33 +536,54 @@ def get_structsdir(self) -> str: """ return self._structs_dir - - def get_logicsdir(self) -> str: + def get_vardir(self): """ - Function to return the logics config directory + Function to return the var directory used by SmartHomeNG - :return: Config directory as an absolute path + :return: var directory as an absolute path + :rtype: str """ - return self._logic_dir + return self._var_dir - def get_functionsdir(self) -> str: + def get_config_dir(self, config): """ - Function to return the userfunctions config directory + Function to return a config dir used by SmartHomeNG + replace / prevent a plethora of get_dir() functions - :return: Config directory as an absolute path + Returns '' for invalid config strings. + + :return: directory as an absolute path + :rtype: str """ - return self._functions_dir + # method would work fine without this check, but... + # make sure we don't allow "any" function call here + if config not in DIRS: + return '' + if hasattr(self, f'get_{config}dir'): + return getattr(self, f'get_{config}dir')() + elif hasattr(self, f'_{config}_dir'): + return getattr(self, f'_{config}_dir') + + return '' - def get_vardir(self): + + def get_config_file(self, config, extension=YAML_FILE): """ - Function to return the var directory used by SmartHomeNG + Function to return a config file used by SmartHomeNG - :return: var directory as an absolute path + Returns '' for invalid config strings. + + :return: file name as an absolute path :rtype: str """ - return self._var_dir + # method would work fine without this check, but... + # make sure we don't allow "any" function call here + if config not in BASES: + return '' + + return os.path.join(self.get_etcdir(), config + extension) def getBaseDir(self):