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