Skip to content

Commit

Permalink
S allius/issue180 (#265)
Browse files Browse the repository at this point in the history
* move default_config.toml into src/cnf/.

* improve file handling

* remove obsolete rules
  • Loading branch information
s-allius authored Dec 23, 2024
1 parent 412013f commit 3234e87
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 52 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion app/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 0 additions & 33 deletions app/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 $< $@
File renamed without changes.
6 changes: 4 additions & 2 deletions app/src/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand All @@ -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")
Expand Down
20 changes: 10 additions & 10 deletions app/tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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': {
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion app/tests/test_config_read_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():

Expand Down
10 changes: 5 additions & 5 deletions app/tests/test_config_read_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions ha_addons/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 3234e87

Please sign in to comment.