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 %}
4 Likes