From 61f90d60167ca721510841295ff1995448225c58 Mon Sep 17 00:00:00 2001 From: aclist <92275929+aclist@users.noreply.github.com> Date: Sat, 27 Jun 2026 23:51:20 +0900 Subject: [PATCH] fix: copy bare config files to named directory (#375) --- dzgui/app_init.py | 33 ++++++++++++++++++++++++++++++- dzgui/config/xdg.py | 9 +++++---- tests/test_bare_conf_files.py | 37 +++++++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 5 deletions(-) create mode 100644 tests/test_bare_conf_files.py diff --git a/dzgui/app_init.py b/dzgui/app_init.py index 078f1f3..2535f0e 100644 --- a/dzgui/app_init.py +++ b/dzgui/app_init.py @@ -1,9 +1,10 @@ import logging import os +import shutil from typing import TYPE_CHECKING -from dzgui.const.constants import APP_NAME +from dzgui.const.constants import APP_NAME, APP_NAME_LOWER from dzgui.const.enum import Preferences from dzgui.config.query import lookup from dzgui.config.userprefs import UserPrefs @@ -50,6 +51,34 @@ def setup_logger(log_path: "Path") -> None: logger.addHandler(fh) + +def copy_bare_configs(config: "Path", resolution: "Path") -> None: + # NOTE: temporary workaround for #375 + conf = "config.json" + state = [ + "dzg.history", + "dzg.versions", + "dzg.res.json", + "dzg.filters.json", + "dzg.columns.json", + "dzg.notes.json", + "ips.csv", + ".month" + ] + if APP_NAME_LOWER not in str(config): + new_file = config.parent / APP_NAME_LOWER / conf + make_parents(new_file) + if config.is_file(): + shutil.copy(config, new_file) + if APP_NAME_LOWER not in str(resolution): + state_path = resolution.parent + for state_file in state: + old_file = state_path / state_file + if old_file.is_file(): + new_file = state_path / APP_NAME_LOWER / state_file + make_parents(new_file) + shutil.copy(old_file, new_file) + def load_gui(version: str, is_debug: bool) -> None: lock = lock_acquire() # noqa @@ -58,6 +87,8 @@ def load_gui(version: str, is_debug: bool) -> None: xdg_paths = get_xdg_paths() XDG = parse_filepaths(xdg_paths) + copy_bare_configs(XDG.config, XDG.resolution) + if XDG.resolution.parent.is_dir() is False: make_parents(XDG.resolution) diff --git a/dzgui/config/xdg.py b/dzgui/config/xdg.py index 2748c8f..8494bae 100644 --- a/dzgui/config/xdg.py +++ b/dzgui/config/xdg.py @@ -55,11 +55,12 @@ def get_xdg_paths() -> dict: resolved_paths = {} for path in xdg_paths: - rp = os.environ.get(path) - if rp is not None and is_writeable(rp): - resolved_paths[path] = Path(rp) + real_path = os.environ.get(path) + if real_path is not None and is_writeable(real_path): + new_path = Path(real_path) else: - resolved_paths[path] = xdg_paths[path] / APP_NAME_LOWER + new_path = xdg_paths[path] + resolved_paths[path] = new_path / APP_NAME_LOWER return resolved_paths diff --git a/tests/test_bare_conf_files.py b/tests/test_bare_conf_files.py new file mode 100644 index 0000000..4dffa6b --- /dev/null +++ b/tests/test_bare_conf_files.py @@ -0,0 +1,37 @@ +import pytest +import tempfile + +from dzgui.app_init import copy_bare_configs +from pathlib import Path + +@pytest.mark.mods +def test_bare_file_import(): + conf_string = "DZGUI_CONF\n" + state_string = "DZGUI_STATE\n" + state = [ + "dzg.history", + "dzg.versions", + "dzg.res.json", + "dzg.filters.json", + "dzg.columns.json", + "dzg.notes.json", + "ips.csv", + ".month" + ] + + tmp = tempfile.TemporaryDirectory() + tmp2 = tempfile.TemporaryDirectory() + tmp_conf = Path(tmp.name) + tmp_state = Path(tmp2.name) + + tmp_conf_file = tmp_conf / "config.json" + tmp_conf_file.write_text(conf_string) + for file in state: + tmp_state.joinpath(file).write_text(state_string) + + tmp_state_file = tmp_state / "dzg.res.json" + copy_bare_configs(tmp_conf_file, tmp_state_file) + + assert (tmp_conf / "dzgui/config.json").read_text() == conf_string + for file in state: + assert (tmp_state / "dzgui" / file).read_text() == state_string