Skip to content

Commit

Permalink
core: fix pathname/conffile access
Browse files Browse the repository at this point in the history
  • Loading branch information
Morg42 committed Jul 22, 2024
1 parent 715a705 commit 137ba75
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 19 deletions.
16 changes: 8 additions & 8 deletions lib/logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ def __init__(self, smarthome, userlogicconf, envlogicconf):
self._userlogicconf = userlogicconf
self._env_dir = smarthome._env_dir
self._envlogicconf = envlogicconf
self._etc_dir = smarthome._etc_dir
self._logic_dir = smarthome._logic_dir
self._etc_dir = smarthome.get_etcdir()
self._logic_dir = smarthome.get_logicsdir()
self._workers = []
self._logics = {}
#self._bytecode = {}
Expand Down Expand Up @@ -897,19 +897,19 @@ def _count_filename_uses(self, conf, filename):
Count the number of logics (sections) that reference this filename
"""
count = 0
for name in conf:
section = conf.get(name, None)
fn = section.get('filename', None)
if fn is not None and fn.lower() == filename.lower():
count += 1
if conf:
for name in conf:
section = conf.get(name, None)
fn = section.get('filename', None)
if fn is not None and fn.lower() == filename.lower():
count += 1
return count


def filename_used_count(self, filename):
# load /etc/logic.yaml
conf_filename = os.path.join(self._get_etc_dir(), 'logic')
conf = shyaml.yaml_load_roundtrip(conf_filename)

count = self._count_filename_uses(conf, filename)
return count

Expand Down
25 changes: 20 additions & 5 deletions lib/smarthome.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,16 +155,12 @@ def initialize_dir_vars(self):

self._etc_dir = os.path.join(self._extern_conf_dir, 'etc')

print(f'etc -> {self._etc_dir}')

# self._conf_dir contains the base dir for config folders
if self._config_etc:
self._conf_dir = self._etc_dir
else:
self._conf_dir = self._extern_conf_dir

print(f'conf -> {self._conf_dir}')

# shng system dirs
self._var_dir = os.path.join(self._base_dir, 'var')
self._lib_dir = os.path.join(self._base_dir, 'lib')
Expand All @@ -187,7 +183,8 @@ def initialize_dir_vars(self):
self._log_conf_basename = os.path.join(self._etc_dir, 'logging')
self._module_conf_basename = os.path.join(self._etc_dir, 'module')
self._plugin_conf_basename = os.path.join(self._etc_dir, 'plugin')
self._logic_conf_basename = os.path.join(self._etc_dir, 'logics')
self._logic_conf_basename = os.path.join(self._etc_dir, 'logic')


def create_directories(self):
"""
Expand Down Expand Up @@ -538,6 +535,24 @@ def get_structsdir(self) -> str:
return self._structs_dir


def get_logicsdir(self) -> str:
"""
Function to return the logics config directory
:return: Config directory as an absolute path
"""
return self._logic_dir


def get_functionsdir(self) -> str:
"""
Function to return the userfunctions config directory
:return: Config directory as an absolute path
"""
return self._functions_dir


def get_vardir(self):
"""
Function to return the var directory used by SmartHomeNG
Expand Down
5 changes: 4 additions & 1 deletion lib/userfunctions.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,10 @@ def init_lib(shng_base_dir=None, sh=None):
else:
base_dir = os.getcwd()

_func_dir = os.path.join(base_dir, _uf_subdir)
if _sh:
_func_dir = _sh.get_functionsdir()
else:
_func_dir = os.path.join(base_dir, _uf_subdir)

user_modules = []
if os.path.isdir(_func_dir):
Expand Down
2 changes: 1 addition & 1 deletion modules/admin/api_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def __init__(self, module):
self.base_dir = self._sh.get_basedir()
self.logger = logging.getLogger(__name__.split('.')[0] + '.' + __name__.split('.')[1] + '.' + __name__.split('.')[2][4:])

self.etc_dir = self._sh._etc_dir
self.etc_dir = self._sh.get_etcdir()
self.modules_dir = os.path.join(self.base_dir, 'modules')

self.core_conf = shyaml.yaml_load(os.path.join(self.modules_dir, 'core', 'module.yaml'))
Expand Down
6 changes: 3 additions & 3 deletions modules/admin/api_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ def __init__(self, module):
self.base_dir = self._sh.get_basedir()
self.logger = logging.getLogger(__name__.split('.')[0] + '.' + __name__.split('.')[1] + '.' + __name__.split('.')[2][4:])

self.etc_dir = self._sh._etc_dir
self.etc_dir = self._sh.get_etcdir()
self.items_dir = self._sh._items_dir
self.functions_dir = self._sh._functions_dir
self.functions_dir = self._sh.get_functionsdir()
self.scenes_dir = self._sh._scenes_dir
self.logics_dir = self._sh._logic_dir
self.logics_dir = self._sh.get_logicsdir()
self.extern_conf_dir = self._sh._extern_conf_dir
self.modules_dir = os.path.join(self.base_dir, 'modules')
return
Expand Down
2 changes: 1 addition & 1 deletion modules/admin/api_logics.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def __init__(self, module):

self.etc_dir = self._sh._etc_dir

self.logics_dir = os.path.join(self.base_dir, 'logics')
self.logics_dir = self._sh.get_logicsdir()
self.logics = Logics.get_instance()
self.logger.info("__init__ self.logics = {}".format(self.logics))
self.plugins = Plugins.get_instance()
Expand Down

0 comments on commit 137ba75

Please sign in to comment.