gcode_macro: Add 'repr' and 'shell_quote' jinja2 filters and 'do' extension #5542
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Passing a variable through both these filters makes it easy to store any type of value, including
dictionaries and lists, using commands such as
SET_GCODE_VARIABLEandSAVE_VARIABLE. The first filterreprsimply calls the python function of the same name, and the second callspipes.quote.Enabling the
jinja2.ext.doextension makes it easier to work with these values, for example youcan set a dictionary member with
{% do d.update ({k: v}) %}.Here's an example to manage a saved dictionary of string values:
In my case, I am trying to set up print profiles for different printer configurations, filament types, and print styles. Without this type of support I might use separate named variables, eg
PROFILE_{profile_name}_{key}, which doesn't seem ideal. Alternatively I might try and do all the escaping myself which would likely be very messy and either incomplete (ie incorrect in edge cases) or overly complicated.The
shell_quotefilter should also be useful for calling other extended gcode commands safely with runtime-generated parameter values.