From 3e505d7a7162faa8c64ee43d466d419448f6230b Mon Sep 17 00:00:00 2001 From: Jordan Ruthe Date: Sat, 6 Mar 2021 12:43:47 -0500 Subject: [PATCH] config: Add include feature --- docs/Configuration.md | 7 +++++++ docs/changelog.md | 3 +++ ks_includes/config.py | 41 ++++++++++++++++++++++++++++++++++++++++- 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/docs/Configuration.md b/docs/Configuration.md index 3e45c25a..92db2242 100644 --- a/docs/Configuration.md +++ b/docs/Configuration.md @@ -9,6 +9,13 @@ If one of those files are found, it will be used over the default configuration. merged with the custom configuration, so if you do not define any menus the default menus will be used.The default config is included here: [ks_includes/KlipperScreen.conf](../ks_includes/KlipperScreen.conf) +## Include files +``` +[include conf.d/*.conf] +# Include another configuration file. Wildcards (*) will expand to match anything. +``` + + ## Main Options ``` [main] diff --git a/docs/changelog.md b/docs/changelog.md index 301ec703..45bb6022 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -1,5 +1,8 @@ ## Changelog +### 2021 03 06 +* Added ability to include configuration files + #### 2021 03 05 * Multiple printers are now available in the main branch. diff --git a/ks_includes/config.py b/ks_includes/config.py index b861fc59..3a2a85cf 100644 --- a/ks_includes/config.py +++ b/ks_includes/config.py @@ -74,14 +74,22 @@ class KlipperScreenConfig: user_def, saved_def = self.separate_saved_config(self.config_path) self.defined_config = configparser.ConfigParser() self.defined_config.read_string(user_def) + + includes = [i[8:] for i in self.defined_config.sections() if i.startswith("include ")] + for include in includes: + self._include_config("/".join(self.config_path.split("/")[:-1]),include) + self.log_config(self.defined_config) self.config.read_string(user_def) if saved_def != None: self.config.read_string(saved_def) + logging.info("====== Saved Def ======\n%s\n=======================" % saved_def) except KeyError: raise ConfigError(f"Error reading config: {self.config_path}") + except: + logging.exception("Unknown error with config") - printers = [i for i in self.config.sections() if i.startswith("printer ")] + printers = sorted([i for i in self.config.sections() if i.startswith("printer ")]) self.printers = [] for printer in printers: self.printers.append({ @@ -117,6 +125,37 @@ class KlipperScreenConfig: self.config.set(vals['section'], name, vals['value']) #self.build_main_menu(self.config) + def _include_config(self, dir, path): + full_path = path if path[0] == "/" else "%s/%s" % (dir, path) + parse_files = [] + + if "*" in full_path: + parent_dir = "/".join(full_path.split("/")[:-1]) + file = full_path.split("/")[-1] + if not os.path.exists(parent_dir): + logging.info("Config Error: Directory %s does not exist" % parent_dir) + return + files = os.listdir(parent_dir) + regex = "^%s$" % file.replace('*','.*') + for file in files: + if re.match(regex, file): + parse_files.append(os.path.join(parent_dir, file)) + else: + if not os.path.exists(os.path.join(full_path)): + logging.info("Config Error: %s does not exist" % full_path) + return + parse_files.append(full_path) + + logging.info("Parsing files: %s" % parse_files) + for file in parse_files: + config = configparser.ConfigParser() + config.read(file) + includes = [i[8:] for i in config.sections() if i.startswith("include ")] + for include in includes: + self._include_config("/".join(full_path.split("/")[:-1]), include) + self.config.read(file) + self.defined_config.read(file) + def separate_saved_config(self, config_path): user_def = [] saved_def = None