From 86d1f55de0e1885836677389eea64f375eaf79c7 Mon Sep 17 00:00:00 2001 From: Frank Tackitt Date: Wed, 11 Dec 2024 12:50:20 -0700 Subject: [PATCH] Configuration reference interpolation (#448) * Configuration reference interpolation Constants are defined in a `[constants]` section, and unused constants will only warn instead of erroring. My intended usage looks like this: ``` [constants] run_current_ab: 1.9 [tmc5160 stepper_x] run_current: ${constants.run_current_ab} [tmc5160 stepper_y] run_current: ${constants.run_current_ab} ``` `${option}` references the current section `${section.option}` looks up anywhere interpolation occurs at most 10 times right now to avoid infinite loops * Add Danger_Features note --- docs/Config_Reference.md | 24 ++++++++++++++ docs/Danger_Features.md | 1 + klippy/configfile.py | 58 ++++++++++++++++++++++++++++------ test/klippy/danger_options.cfg | 4 ++- 4 files changed, 76 insertions(+), 11 deletions(-) diff --git a/docs/Config_Reference.md b/docs/Config_Reference.md index b19bf6f2c..fbb4d8446 100644 --- a/docs/Config_Reference.md +++ b/docs/Config_Reference.md @@ -145,6 +145,30 @@ A collection of Kalico-specific system options #log_webhook_method_register_messages: False ``` +## ⚠️ Configuration references + +In your configuration, you can reference other values to share +configuration between multiple sections. References take the form of +`${option}` to copy a value in the current section, or +`${section.option}` to look up a value elsewhere in your configuration. + +Optionally, a `[constants]` section may be used specifically to store +these values. Unlike the rest of your configuration, unused constants +will show a warning instead of causing an error. + +``` +[constants] +run_current_ab: 1.0 +i_am_not_used: True # Will show "Constant 'i_am_not_used' is unused" + +[tmc5160 stepper_x] +run_current: ${constants.run_current_ab} + +[tmc5160 stepper_y] +run_current: ${tmc5160 stepper_x.run_current} +# Nested references work, but are not advised +``` + ## Common kinematic settings ### [printer] diff --git a/docs/Danger_Features.md b/docs/Danger_Features.md index 3705b3d3c..0ab9e603e 100644 --- a/docs/Danger_Features.md +++ b/docs/Danger_Features.md @@ -14,6 +14,7 @@ - `--rotate-log-at-restart` can be added to your Kalico start script or service to force log rotation every restart. - [`[virtual_sdcard] with_subdirs`](./Config_Reference.md#virtual_sdcard) enables scanning of subdirectories for .gcode files, for the menu and M20/M23 commands - [`[firmware_retraction] z_hop_height`](./Config_Reference.md#firmware_retraction) adds an automatic z hop when using firmware retraction +- [`[constants]` and `${constants.value}`](./Config_Reference.md#configuration-references) allow re-using values in your configuration ## Enhanced behavior diff --git a/klippy/configfile.py b/klippy/configfile.py index a87c81113..211318bbe 100644 --- a/klippy/configfile.py +++ b/klippy/configfile.py @@ -15,6 +15,38 @@ class sentinel: pass +class SectionInterpolation(configparser.Interpolation): + """ + variable interpolation replacing ${[section.]option} + """ + + _KEYCRE = re.compile( + r"\$\{(?:(?P
[^.:${}]+)[.:])?(?P