Example: Search Printer Objects

This macro will allow you to type SEARCH_VARS S={some word} from the terminal and
it will respond with all of the matching items in the printer object.

Say I wanted to know what the name and path of for the value of the currently loaded bed
mesh. I could do type search_vars s=profile in my terminal and it will respond with
any items containing the word ‘profile’.

$ SEARCH_VARS s="profile"
// printer.bed_mesh.profile_name : default

Additionally, omitting the “s” parameter will just dump out literally everything and probably catch fire.
You have been warned.

[gcode_macro SEARCH_VARS]
gcode:
    {% set search = params.S|lower %}
    {% set ns = namespace() %}
    {% for item in printer  %}
        {% if ' ' in item %}
            {% set ns.path = ['printer', "['%s']" % (item), ''] %}
        {% else %}
            {% set ns.path = ['printer.', item, ''] %}   
        {% endif %} 

        {% if search in ns.path|lower %}
            { action_respond_info(ns.path|join) }
        {% endif %} 

        {% if printer[item].items() %}
            {% for childkey, child in printer[item].items() recursive %}
                {% set ns.path = ns.path[:loop.depth|int + 1] %}

                {% if ' ' in childkey %}
                    {% set null = ns.path.append("['%s']" % (childkey)) %}
                {% else %}
                    {% set null = ns.path.append(".%s" % (childkey)) %}
                {% endif %} 

                {% if child is mapping  %}
                    { loop(child.items()) }
                {% else %}
                    {% if search in ns.path|lower %}
                        { action_respond_info("%s : %s" % (ns.path|join, child)) }   
                    {% endif %} 
                {% endif %} 
                
            {% endfor %}
        {% endif %} 
    {% endfor %}
14 Likes

I know this is really old, but I think it is close to what I need for something I’m working on. See:
Indirectly indexing through extruder objects - General Discussion - Klipper

It makes sense somewhat (I’m still a novice) that what I was trying to do is different from how you can do macros because this involves paths. It looks like this could be what I need to build the path, but I think the last piece I need to figure out is how do I capture the value that the path references.

So say I had something like below. What would I put where the ???s are to pull the value that the ns.path|join references? i.e. the value of printer.extruder1.temperature

[gcode_macro test]
variable_temp: 0
variable_ext: "extruder1"
gcode:
    {% set ns = namespace() %}
    {% set ns.path = ['printer.', ext, 'temperature'] %}

    SET_GCODE_VARIABLE MACRO=test VARIABLE=temp VALUE=???

Well I found what I needed and it actually had nothing to do with paths. See the solution of my post linked above.