Skip to content

Commit

Permalink
Merge pull request #663 from Morg42/config-1
Browse files Browse the repository at this point in the history
core: directory/conf vars cleanup
  • Loading branch information
Morg42 authored Jul 22, 2024
2 parents 26f1de6 + 137ba75 commit 1e26156
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 52 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
81 changes: 43 additions & 38 deletions lib/smarthome.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,18 @@
import logging
import logging.handlers
import logging.config
import platform # TODO: remove? unused
# import platform # TODO: remove? unused
import shutil

import signal
import subprocess
import threading
import time
import traceback
try:
import psutil # TODO: remove? unused
except ImportError:
pass
# try:
# import psutil # TODO: remove? unused
# except ImportError:
# pass

BASE = os.path.sep.join(os.path.realpath(__file__).split(os.path.sep)[:-2])
PIDFILE = os.path.join(BASE, 'var', 'run', 'smarthome.pid')
Expand Down Expand Up @@ -153,31 +153,37 @@ def initialize_dir_vars(self):
self._base_dir = BASE
self.base_dir = self._base_dir # **base_dir** is deprecated. Use method get_basedir() instead. - for external modules using that var (backend, ...?)

self._extern_conf_dir = BASE

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

# decide where to look for config files
# 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._base_dir
self._conf_dir = self._extern_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')
self._plugins_dir = os.path.join(self._base_dir, 'plugins')
self._structs_dir = os.path.join(self._conf_dir, 'structs')
self._env_dir = os.path.join(self._lib_dir, 'env' + os.path.sep)

# env and var dirs
self._env_dir = os.path.join(self._lib_dir, 'env' + os.path.sep)
self._env_logic_conf_basename = os.path.join(self._env_dir, 'logic')
self._cache_dir = os.path.join(self._var_dir, 'cache' + os.path.sep)

# user config dirs
self._items_dir = os.path.join(self._conf_dir, 'items' + os.path.sep)
self._logic_conf_basename = os.path.join(self._conf_dir, 'logic')
self._structs_dir = os.path.join(self._conf_dir, 'structs')
self._logic_dir = os.path.join(self._conf_dir, 'logics' + os.path.sep)
self._cache_dir = os.path.join(self._var_dir, 'cache' + os.path.sep)
self._log_conf_basename = os.path.join(self._etc_dir, 'logging')
self._functions_dir = os.path.join(self._conf_dir, 'functions' + os.path.sep)
self._scenes_dir = os.path.join(self._conf_dir, 'scenes' + os.path.sep)

# system config files
self._smarthome_conf_basename = os.path.join(self._etc_dir, 'smarthome')
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, 'logic')


def create_directories(self):
Expand All @@ -187,8 +193,8 @@ def create_directories(self):
os.makedirs(self._structs_dir, mode=0o775, exist_ok=True)

os.makedirs(self._var_dir, mode=0o775, exist_ok=True)
os.makedirs(os.path.join(self._var_dir, 'backup'), mode=0o775, exist_ok=True)
os.makedirs(self._cache_dir, mode=0o775, exist_ok=True)
os.makedirs(os.path.join(self._var_dir, 'backup'), mode=0o775, exist_ok=True)
os.makedirs(os.path.join(self._var_dir, 'db'), mode=0o775, exist_ok=True)
os.makedirs(os.path.join(self._var_dir, 'log'), mode=0o775, exist_ok=True)
os.makedirs(os.path.join(self._var_dir, 'run'), mode=0o775, exist_ok=True)
Expand All @@ -215,6 +221,9 @@ def __init__(self, MODE, extern_conf_dir='', config_etc=False):
self._mode = MODE

self._config_etc = config_etc
self._extern_conf_dir = BASE
if extern_conf_dir != '':
self._extern_conf_dir = extern_conf_dir

self.initialize_vars()
self.initialize_dir_vars()
Expand All @@ -227,9 +236,6 @@ def __init__(self, MODE, extern_conf_dir='', config_etc=False):
self.PYTHON_VERSION = lib.utils.get_python_version()
self.python_bin = sys.executable

if extern_conf_dir != '':
self._extern_conf_dir = extern_conf_dir

# get systeminfo closs
self.systeminfo = Systeminfo

Expand All @@ -245,25 +251,6 @@ def __init__(self, MODE, extern_conf_dir='', config_etc=False):
# self.version = shngversion.get_shng_version()
self.connections = None

# reinitialize dir vars with path to extern configuration directory
self._etc_dir = os.path.join(self._extern_conf_dir, 'etc')

# decide where to look for config files
if self._config_etc:
self._conf_dir = self._etc_dir
else:
self._conf_dir = self._extern_conf_dir

self._items_dir = os.path.join(self._conf_dir, 'items' + os.path.sep)
self._functions_dir = os.path.join(self._conf_dir, 'functions' + os.path.sep)
self._logic_dir = os.path.join(self._conf_dir, 'logics' + os.path.sep)
self._scenes_dir = os.path.join(self._conf_dir, 'scenes' + os.path.sep)
self._smarthome_conf_basename = os.path.join(self._etc_dir, 'smarthome')
self._logic_conf_basename = os.path.join(self._etc_dir, 'logic')
self._module_conf_basename = os.path.join(self._etc_dir, 'module')
self._plugin_conf_basename = os.path.join(self._etc_dir, 'plugin')
self._log_conf_basename = os.path.join(self._etc_dir, 'logging')

self._pidfile = PIDFILE

# check config files
Expand Down Expand Up @@ -548,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 1e26156

Please sign in to comment.