From 3234e87b55728c52d5680e63b307366874a7900c Mon Sep 17 00:00:00 2001 From: Stefan Allius <122395479+s-allius@users.noreply.github.com> Date: Tue, 24 Dec 2024 00:13:32 +0100 Subject: [PATCH] S allius/issue180 (#265) * move default_config.toml into src/cnf/. * improve file handling * remove obsolete rules --- CHANGELOG.md | 2 ++ app/Dockerfile | 1 - app/Makefile | 33 --------------------- app/{config => src/cnf}/default_config.toml | 0 app/src/server.py | 6 ++-- app/tests/test_config.py | 20 ++++++------- app/tests/test_config_read_env.py | 2 +- app/tests/test_config_read_json.py | 10 +++---- ha_addons/Makefile | 1 + 9 files changed, 23 insertions(+), 52 deletions(-) rename app/{config => src/cnf}/default_config.toml (100%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 380f10c..dc0aa57 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [unreleased] +- fix the path handling for logging.ini and default_config.toml [#180](https://github.com/s-allius/tsun-gen3-proxy/issues/180) + ## [0.12.0] - 2024-12-22 - add hadolint configuration diff --git a/app/Dockerfile b/app/Dockerfile index 84ffe88..df85b81 100644 --- a/app/Dockerfile +++ b/app/Dockerfile @@ -59,7 +59,6 @@ RUN python -m pip install --no-cache-dir --no-cache --no-index /root/wheels/* && # copy the content of the local src and config directory to the working directory COPY --chmod=0700 entrypoint.sh /root/entrypoint.sh -COPY config . COPY src . RUN echo ${VERSION} > /proxy-version.txt \ && date > /build-date.txt diff --git a/app/Makefile b/app/Makefile index b68f782..fb96df2 100644 --- a/app/Makefile +++ b/app/Makefile @@ -7,23 +7,6 @@ IMAGE = tsun-gen3-proxy # Folders SRC=. -SRC_PROXY=$(SRC)/src -CNF_PROXY=$(SRC)/config - -DST=rootfs -DST_PROXY=$(DST)/home/proxy - -# collect source files -SRC_FILES := $(wildcard $(SRC_PROXY)/*.py)\ - $(wildcard $(SRC_PROXY)/*.ini)\ - $(wildcard $(SRC_PROXY)/cnf/*.py)\ - $(wildcard $(SRC_PROXY)/gen3/*.py)\ - $(wildcard $(SRC_PROXY)/gen3plus/*.py) -CNF_FILES := $(wildcard $(CNF_PROXY)/*.toml) - -# determine destination files -TARGET_FILES = $(SRC_FILES:$(SRC_PROXY)/%=$(DST_PROXY)/%) -CONFIG_FILES = $(CNF_FILES:$(CNF_PROXY)/%=$(DST_PROXY)/%) export BUILD_DATE := ${shell date -Iminutes} VERSION := $(shell cat $(SRC)/.version) @@ -48,20 +31,4 @@ preview rc rel: docker buildx bake -f docker-bake.hcl $@ - .PHONY: debug dev preview rc rel - - -$(CONFIG_FILES): $(DST_PROXY)/% : $(CNF_PROXY)/% - @echo Copy $< to $@ - @mkdir -p $(@D) - @cp $< $@ - -$(TARGET_FILES): $(DST_PROXY)/% : $(SRC_PROXY)/% - @echo Copy $< to $@ - @mkdir -p $(@D) - @cp $< $@ - -$(DST)/requirements.txt : $(SRC)/requirements.txt - @echo Copy $< to $@ - @cp $< $@ diff --git a/app/config/default_config.toml b/app/src/cnf/default_config.toml similarity index 100% rename from app/config/default_config.toml rename to app/src/cnf/default_config.toml diff --git a/app/src/server.py b/app/src/server.py index e7c44af..abc9aae 100644 --- a/app/src/server.py +++ b/app/src/server.py @@ -156,8 +156,10 @@ def main(): # pragma: no cover setattr(logging.handlers, "log_path", args.log_path) setattr(logging.handlers, "log_backups", args.log_backups) + os.makedirs(args.log_path, exist_ok=True) - logging.config.fileConfig('logging.ini') + src_dir = os.path.dirname(__file__) + '/' + logging.config.fileConfig(src_dir + 'logging.ini') logging.info(f'Server "{serv_name} - {version}" will be started') logging.info(f'current dir: {os.getcwd()}') logging.info(f"config_path: {args.config_path}") @@ -184,7 +186,7 @@ def main(): # pragma: no cover asyncio.set_event_loop(loop) # read config file - Config.init(ConfigReadToml("default_config.toml")) + Config.init(ConfigReadToml(src_dir + "cnf/default_config.toml")) ConfigReadEnv() ConfigReadJson(args.config_path + "config.json") ConfigReadToml(args.config_path + "config.toml") diff --git a/app/tests/test_config.py b/app/tests/test_config.py index d229dac..96b7478 100644 --- a/app/tests/test_config.py +++ b/app/tests/test_config.py @@ -144,7 +144,7 @@ def ConfigComplete(): } def test_default_config(): - Config.init(ConfigReadToml("app/config/default_config.toml")) + Config.init(ConfigReadToml("app/src/cnf/default_config.toml")) validated = Config.def_config assert validated == {'gen3plus': {'at_acl': {'mqtt': {'allow': ['AT+'], 'block': []}, 'tsun': {'allow': ['AT+Z', 'AT+UPURL', 'AT+SUPDATE'], 'block': []}}}, 'tsun': {'enabled': True, 'host': 'logger.talent-monitoring.com', 'port': 5005}, 'solarman': {'enabled': True, 'host': 'iot.talent-monitoring.com', 'port': 10000}, 'mqtt': {'host': 'mqtt', 'port': 1883, 'user': None, 'passwd': None}, 'ha': {'auto_conf_prefix': 'homeassistant', 'discovery_prefix': 'homeassistant', 'entity_prefix': 'tsun', 'proxy_node_id': 'proxy', 'proxy_unique_id': 'P170000000000001'}, 'inverters': { @@ -193,7 +193,7 @@ def test_full_config(ConfigComplete): def test_read_empty(ConfigDefault): test_buffer.rd = "" - Config.init(ConfigReadToml("app/config/default_config.toml")) + Config.init(ConfigReadToml("app/src/cnf/default_config.toml")) for _ in patch_open(): ConfigReadToml("config/config.toml") err = Config.get_error() @@ -216,14 +216,14 @@ def test_no_file(): assert defcnf == None def test_no_file2(): - Config.init(ConfigReadToml("app/config/default_config.toml")) + Config.init(ConfigReadToml("app/src/cnf/default_config.toml")) assert Config.err == None ConfigReadToml("_no__file__no_") err = Config.get_error() assert err == None def test_invalid_filename(): - Config.init(ConfigReadToml("app/config/default_config.toml")) + Config.init(ConfigReadToml("app/src/cnf/default_config.toml")) assert Config.err == None ConfigReadToml(None) err = Config.get_error() @@ -232,7 +232,7 @@ def test_invalid_filename(): def test_read_cnf1(): test_buffer.rd = "solarman.enabled = false" - Config.init(ConfigReadToml("app/config/default_config.toml")) + Config.init(ConfigReadToml("app/src/cnf/default_config.toml")) for _ in patch_open(): ConfigReadToml("config/config.toml") err = Config.get_error() @@ -279,7 +279,7 @@ def test_read_cnf1(): def test_read_cnf2(): test_buffer.rd = "solarman.enabled = 'FALSE'" - Config.init(ConfigReadToml("app/config/default_config.toml")) + Config.init(ConfigReadToml("app/src/cnf/default_config.toml")) for _ in patch_open(): ConfigReadToml("config/config.toml") err = Config.get_error() @@ -322,7 +322,7 @@ def test_read_cnf2(): def test_read_cnf3(ConfigDefault): test_buffer.rd = "solarman.port = 'FALSE'" - Config.init(ConfigReadToml("app/config/default_config.toml")) + Config.init(ConfigReadToml("app/src/cnf/default_config.toml")) for _ in patch_open(): ConfigReadToml("config/config.toml") err = Config.get_error() @@ -334,7 +334,7 @@ def test_read_cnf3(ConfigDefault): def test_read_cnf4(): test_buffer.rd = "solarman.port = 5000" - Config.init(ConfigReadToml("app/config/default_config.toml")) + Config.init(ConfigReadToml("app/src/cnf/default_config.toml")) for _ in patch_open(): ConfigReadToml("config/config.toml") err = Config.get_error() @@ -377,7 +377,7 @@ def test_read_cnf4(): def test_read_cnf5(): test_buffer.rd = "solarman.port = 1023" - Config.init(ConfigReadToml("app/config/default_config.toml")) + Config.init(ConfigReadToml("app/src/cnf/default_config.toml")) for _ in patch_open(): ConfigReadToml("config/config.toml") err = Config.get_error() @@ -386,7 +386,7 @@ def test_read_cnf5(): def test_read_cnf6(): test_buffer.rd = "solarman.port = 65536" - Config.init(ConfigReadToml("app/config/default_config.toml")) + Config.init(ConfigReadToml("app/src/cnf/default_config.toml")) for _ in patch_open(): ConfigReadToml("config/config.toml") err = Config.get_error() diff --git a/app/tests/test_config_read_env.py b/app/tests/test_config_read_env.py index 3bf33fc..b9ba8f4 100644 --- a/app/tests/test_config_read_env.py +++ b/app/tests/test_config_read_env.py @@ -44,7 +44,7 @@ def test_extend_key(): assert conf == {'': 'testuser'} def test_read_env_config(): - Config.init(ConfigReadToml("app/config/default_config.toml")) + Config.init(ConfigReadToml("app/src/cnf/default_config.toml")) assert Config.get('mqtt') == {'host': 'mqtt', 'port': 1883, 'user': None, 'passwd': None} for _ in patch_getenv(): diff --git a/app/tests/test_config_read_json.py b/app/tests/test_config_read_json.py index 696a529..e1d4c6a 100644 --- a/app/tests/test_config_read_json.py +++ b/app/tests/test_config_read_json.py @@ -84,7 +84,7 @@ def ConfigTomlEmpty(): def test_no_config(ConfigDefault): test_buffer.rd = "" # empty buffer, no json - Config.init(ConfigReadToml("app/config/default_config.toml")) + Config.init(ConfigReadToml("app/src/cnf/default_config.toml")) for _ in patch_open(): ConfigReadJson() err = Config.get_error() @@ -96,7 +96,7 @@ def test_no_config(ConfigDefault): def test_no_file(ConfigDefault): test_buffer.rd = "" # empty buffer, no json - Config.init(ConfigReadToml("app/config/default_config.toml")) + Config.init(ConfigReadToml("app/src/cnf/default_config.toml")) for _ in patch_open(): ConfigReadJson("_no__file__no_") err = Config.get_error() @@ -108,7 +108,7 @@ def test_no_file(ConfigDefault): def test_invalid_filename(ConfigDefault): test_buffer.rd = "" # empty buffer, no json - Config.init(ConfigReadToml("app/config/default_config.toml")) + Config.init(ConfigReadToml("app/src/cnf/default_config.toml")) for _ in patch_open(): ConfigReadJson(None) err = Config.get_error() @@ -340,7 +340,7 @@ def test_cnv6(): def test_empty_config(ConfigDefault): test_buffer.rd = "{}" # empty json - Config.init(ConfigReadToml("app/config/default_config.toml")) + Config.init(ConfigReadToml("app/src/cnf/default_config.toml")) for _ in patch_open(): ConfigReadJson() err = Config.get_error() @@ -401,7 +401,7 @@ def test_full_config(ConfigComplete): ] } """ - Config.init(ConfigReadToml("app/config/default_config.toml")) + Config.init(ConfigReadToml("app/src/cnf/default_config.toml")) for _ in patch_open(): ConfigReadJson() err = Config.get_error() diff --git a/ha_addons/Makefile b/ha_addons/Makefile index b0faba8..bbfe982 100644 --- a/ha_addons/Makefile +++ b/ha_addons/Makefile @@ -25,6 +25,7 @@ TEMPL=templates SRC_FILES := $(wildcard $(SRC_PROXY)/*.py)\ $(wildcard $(SRC_PROXY)/*.ini)\ $(wildcard $(SRC_PROXY)/cnf/*.py)\ + $(wildcard $(SRC_PROXY)/cnf/*.toml)\ $(wildcard $(SRC_PROXY)/gen3/*.py)\ $(wildcard $(SRC_PROXY)/gen3plus/*.py) CNF_FILES := $(wildcard $(CNF_PROXY)/*.toml)