Skip to content

Commit

Permalink
lib.smarthome: add generic get conffile/confdir method
Browse files Browse the repository at this point in the history
  • Loading branch information
Morg42 committed Jul 23, 2024
1 parent b68f71f commit 5217ca6
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 15 deletions.
3 changes: 2 additions & 1 deletion lib/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
50 changes: 36 additions & 14 deletions lib/smarthome.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

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

0 comments on commit 5217ca6

Please sign in to comment.