Skip to content

Conversation

@jamespharaoh
Copy link

@jamespharaoh jamespharaoh commented Jun 2, 2022

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_VARIABLE and SAVE_VARIABLE. The first filter repr simply calls the python function of the same name, and the second calls
pipes.quote.

Enabling the jinja2.ext.do extension makes it easier to work with these values, for example you
can set a dictionary member with {% do d.update ({k: v}) %}.

Here's an example to manage a saved dictionary of string values:

[gcode_macro DATA_SET]
gcode:
  {% set data = printer.save_variables.variables.data | default ({}) %}
  {% do data.update ({ params.NAME: params.VALUE }) %}
  SAVE_VARIABLE VARIABLE=data VALUE={data | repr | shell_quote}

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_quote filter should also be useful for calling other extended gcode commands safely with runtime-generated parameter values.

…ension

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_VARIABLE' and 'SAVE_VARIABLE'. The first
filter 'repr' simply calls the python function of the same name, and the second calls
'pipes.quote'.

Enabling the 'jinja2.ext.do' extension makes it easier to work with these values, for example you
can set a dictionary member with '{% do d.update ({ k: v })'.

Here's an example to manage a saved dictionary of string values:

  [gcode_macro DATA_SET]
  gcode:
    {% set data = printer.save_variables.variables.data | default ({}) %}
    {% do data.update ({ params.NAME: params.VALUE }) %}
    SAVE_VARIABLE VARIABLE=data VALUE={data | repr | shell_quote}
@Sineos
Copy link
Collaborator

Sineos commented Jun 2, 2022

See #5149

@KevinOConnor
Copy link
Collaborator

Thanks. However, I have similar feedback to my feedback in PR #5149 (briefly, I fear this adds complexity and the result is more variants of "ugly").

Separately, I believe it should be possible to accomplish repr functionality already by using some_string.__repr__().

-Kevin

@KevinOConnor KevinOConnor added the pending feedback Topic is pending feedback from submitter label Jun 8, 2022
@github-actions
Copy link

It looks like this GitHub Pull Request has become inactive. If there are any further updates, you can add a comment here or open a new ticket.

Best regards,
~ Your friendly GitIssueBot

PS: I'm just an automated script, not a human being.

@github-actions github-actions bot added the inactive Not currently being worked on label Jun 30, 2022
@github-actions github-actions bot closed this Jun 30, 2022
@github-actions github-actions bot locked and limited conversation to collaborators Jun 30, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

inactive Not currently being worked on pending feedback Topic is pending feedback from submitter

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants