From 9d0e4b841f905f0034a7c6fefefcf041e38b90a3 Mon Sep 17 00:00:00 2001 From: alfrix Date: Fri, 26 Jan 2024 13:48:42 -0300 Subject: [PATCH] config: allow xdg .config folder fully deprecate old klipper_config and home folder config config order of preference 1. printer_data/config 2. xdg .config/KlipperScreen 3. klipperscreen repo directory --- docs/Changelog.md | 4 +++ docs/Configuration.md | 7 +++--- ks_includes/config.py | 57 ++++++++++++++++++++----------------------- screen.py | 3 ++- 4 files changed, 35 insertions(+), 36 deletions(-) diff --git a/docs/Changelog.md b/docs/Changelog.md index fb51d859..886dad95 100644 --- a/docs/Changelog.md +++ b/docs/Changelog.md @@ -2,6 +2,10 @@ Breaking changes will be listed here. +#### [2024_01_26]() +* Deprecated old ~/klipper_config folder users need to place the configfile +in ~/printer_data/config, ~/.config/KlipperScreen, or directly in the repo + #### [2023 10 08](https://github.com/KlipperScreen/KlipperScreen/commit/b6199a4f24beb02dc0f8956f60c88e3ba3468927) * Deprecated [bed_screws] and [z_calibrate_position] sections those configs should go in [printer name] diff --git a/docs/Configuration.md b/docs/Configuration.md index 4f0a173b..a0af00f9 100644 --- a/docs/Configuration.md +++ b/docs/Configuration.md @@ -235,10 +235,9 @@ enable: {{ 'MY_MACRO' in printer.gcode_macros.list }} KlipperScreen will search for a configuration file in the following order: -1. _~/KlipperScreen.conf_ -2. _${KlipperScreen_Directory}/KlipperScreen.conf_ -3. _~/printer_data/config/KlipperScreen.conf_ -4. _~/klipper_config/KlipperScreen.conf_ +1. _~/printer_data/config/KlipperScreen.conf_ +2. _~/.config/KlipperScreen/KlipperScreen.conf_ +3. _${KlipperScreen_Directory}/KlipperScreen.conf_ If you need a custom location for the configuration file, you can add -c or --configfile to the systemd file and specify the location of your configuration file. diff --git a/ks_includes/config.py b/ks_includes/config.py index 580dae55..c2f8bb9f 100644 --- a/ks_includes/config.py +++ b/ks_includes/config.py @@ -22,6 +22,9 @@ SCREEN_BLANKING_OPTIONS = [ ] klipperscreendir = pathlib.Path(__file__).parent.resolve().parent +home = os.path.expanduser("~/") +klipper_config = os.path.join(home, "printer_data", "config") +xdg_config = os.path.join(home, ".config", "KlipperScreen") class ConfigError(Exception): @@ -389,35 +392,27 @@ class KlipperScreenConfig: saved_def.append(line[(len(self.do_not_edit_prefix) + 1):]) return ["\n".join(user_def), None if saved_def is None else "\n".join(saved_def)] + @staticmethod + def check_path_exists(base_dir, filename): + for name in (filename, filename.lower()): + full_path = os.path.join(base_dir, name) + if os.path.exists(full_path): + return full_path + return None + def get_config_file_location(self, file): - # Passed config (-c) by default is ~/KlipperScreen.conf + # Passed config (-c) by default is blank logging.info(f"Passed config (-c): {file}") - if os.path.exists(file): + if file not in (".", "..") and os.path.exists(file): return file - file = os.path.join(klipperscreendir, self.configfile_name) - if os.path.exists(file): - return file - file = os.path.join(klipperscreendir, self.configfile_name.lower()) - if os.path.exists(file): - return file + # List of directories to search for the config file + directories = [klipper_config, xdg_config, klipperscreendir] - klipper_config = os.path.join(os.path.expanduser("~/"), "printer_data", "config") - file = os.path.join(klipper_config, self.configfile_name) - if os.path.exists(file): - return file - file = os.path.join(klipper_config, self.configfile_name.lower()) - if os.path.exists(file): - return file - - # OLD config folder - klipper_config = os.path.join(os.path.expanduser("~/"), "klipper_config") - file = os.path.join(klipper_config, self.configfile_name) - if os.path.exists(file): - return file - file = os.path.join(klipper_config, self.configfile_name.lower()) - if os.path.exists(file): - return file + for directory in directories: + path = self.check_path_exists(directory, self.configfile_name) + if path: + return path # fallback return self.default_config_path @@ -517,17 +512,17 @@ class KlipperScreenConfig: if self.config_path != self.default_config_path: filepath = self.config_path else: - filepath = os.path.expanduser("~/") - klipper_config = os.path.join(filepath, "printer_data", "config") - old_klipper_config = os.path.join(filepath, "klipper_config") if os.path.exists(klipper_config): filepath = os.path.join(klipper_config, self.configfile_name) - elif os.path.exists(old_klipper_config): - filepath = os.path.join(old_klipper_config, self.configfile_name) else: - filepath = os.path.join(filepath, self.configfile_name) + try: + if not os.path.exists(xdg_config): + pathlib.Path(xdg_config).mkdir(parents=True, exist_ok=True) + filepath = os.path.join(xdg_config, self.configfile_name) + except Exception as e: + logging.error(e) + filepath = klipperscreendir logging.info(f'Creating a new config file in {filepath}') - try: with open(filepath, 'w') as file: file.write(contents) diff --git a/screen.py b/screen.py index be4a9cd9..158734a5 100755 --- a/screen.py +++ b/screen.py @@ -1097,7 +1097,8 @@ def main(): homedir = os.path.expanduser("~") parser.add_argument( - "-c", "--configfile", default=os.path.join(homedir, "KlipperScreen.conf"), metavar='', + "-c", "--configfile", + default="", metavar='', help="Location of KlipperScreen configuration file" ) logdir = os.path.join(homedir, "printer_data", "logs")