Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gcode_macro: adds RELOAD_GCODE_MACROS #305

Merged
merged 1 commit into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ See the [Danger Features document](https://dangerklipper.io/Danger_Features.html

- [gcode: HEATER_INTERRUPT gcode command](https://github.com/DangerKlippers/danger-klipper/pull/94)

- [gcode: RELOAD_GCODE_MACROS command](https://github.com/DangerKlippers/danger-klipper/pull/305)

- [probe: dockable Probe](https://github.com/DangerKlippers/danger-klipper/pull/43) ([klipper#4328](https://github.com/Klipper3d/klipper/pull/4328))

- [probe: drop the first result](https://github.com/DangerKlippers/danger-klipper/pull/2) ([klipper#3397](https://github.com/Klipper3d/klipper/issues/3397))
Expand Down
1 change: 1 addition & 0 deletions docs/Danger_Features.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@

- The jinja `do` extension has been enabled. You can now call functions in your macros without resorting to dirty hacks: `{% do array.append(5) %}`
- The python [`math`](https://docs.python.org/3/library/math.html) library is available to macros. `{math.sin(math.pi * variable)}` and more!
- New [`RELOAD_GCODE_MACROS`](./G-Codes.md#reload_gcode_macros) G-Code command to reload `[gcode_macro]` templates without requiring a restart.

## [Plugins](./Plugins.md)
Extend your Danger Klipper installation with custom plugins.
Expand Down
8 changes: 7 additions & 1 deletion docs/G-Codes.md
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ commands are available when a
- `MOVE_TO_DETACH_PROBE`: Move away from the dock to disconnect the probe
from the toolhead.
- `MOVE_AVOIDING_DOCK [X=<value>] [Y=<value>] [SPEED=<value>]`: Move to the
defined point (absolute coordinates) avoiding the safe dock area
defined point (absolute coordinates) avoiding the safe dock area

### [dual_carriage]

Expand Down Expand Up @@ -742,6 +742,12 @@ enabled (also see the
This command allows one to change the value of a gcode_macro variable
at run-time. The provided VALUE is parsed as a Python literal.

#### RELOAD_GCODE_MACROS
`RELOAD_GCODE_MACROS`: This command reads the config files and reloads
all previously loaded gcode templates. It does not load new `[gcode_macro]`
objects or unload deleted ones. Variables modified with SET_GCODE_VARIABLE
remain unaffected.

### [gcode_move]

The gcode_move module is automatically loaded.
Expand Down
9 changes: 6 additions & 3 deletions klippy/configfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,9 +273,12 @@ def __init__(self, printer):
self.unused_options = []
self.save_config_pending = False
gcode = self.printer.lookup_object("gcode")
gcode.register_command(
"SAVE_CONFIG", self.cmd_SAVE_CONFIG, desc=self.cmd_SAVE_CONFIG_help
)
if "SAVE_CONFIG" not in gcode.ready_gcode_handlers:
gcode.register_command(
"SAVE_CONFIG",
self.cmd_SAVE_CONFIG,
desc=self.cmd_SAVE_CONFIG_help,
)

def get_printer(self):
return self.printer
Expand Down
4 changes: 3 additions & 1 deletion klippy/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ def connect(self, eventtime):
message_count = len(msgparser.get_messages())
app = msgparser.get_app_info()
version, build_versions = msgparser.get_version_info()
self.output(f"Loaded {message_count} commands ({app} {version} / {build_versions})")
self.output(
f"Loaded {message_count} commands ({app} {version} / {build_versions})"
)
self.output(
"MCU config: %s"
% (
Expand Down
45 changes: 43 additions & 2 deletions klippy/extras/gcode_macro.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# This file may be distributed under the terms of the GNU GPLv3 license.
import traceback, logging, ast, copy, json
import jinja2, math

import configfile

######################################################################
# Template handling
Expand Down Expand Up @@ -79,6 +79,25 @@ def run_gcode_from_command(self, context=None):
self.gcode.run_script_from_command(self.render(context))


class Template:
def __init__(self, printer, env, name, script) -> None:
self.name = name
self.printer = printer
self.env = env
self.function = TemplateWrapper(self.printer, self.env, name, script)

def __call__(self, context=None):
return self.function(context)

def __getattr__(self, name):
return getattr(self.function, name)

def reload(self, script):
self.function = TemplateWrapper(
self.printer, self.env, self.name, script
)


# Main gcode macro template tracking
class PrinterGCodeMacro:
def __init__(self, config):
Expand All @@ -87,13 +106,18 @@ def __init__(self, config):
"{%", "%}", "{", "}", extensions=["jinja2.ext.do"]
)

self.gcode = self.printer.lookup_object("gcode")
self.gcode.register_command(
"RELOAD_GCODE_MACROS", self.cmd_RELOAD_GCODE_MACROS
)

def load_template(self, config, option, default=None):
name = "%s:%s" % (config.get_name(), option)
if default is None:
script = config.get(option)
else:
script = config.get(option, default)
return TemplateWrapper(self.printer, self.env, name, script)
return Template(self.printer, self.env, name, script)

def _action_emergency_stop(self, msg="action_emergency_stop"):
self.printer.invoke_shutdown("Shutdown due to %s" % (msg,))
Expand Down Expand Up @@ -124,6 +148,23 @@ def create_template_context(self, eventtime=None):
"math": math,
}

def cmd_RELOAD_GCODE_MACROS(self, gcmd):
pconfig = configfile.PrinterConfig(self.printer)
new_config = pconfig.read_main_config()
for name, obj in self.printer.lookup_objects("gcode_macro"):
try:
new_section = new_config.getsection(name)
except:
continue

if name in [
s.get_name()
for s in new_config.get_prefix_sections("gcode_macro")
]:
template = obj.template
new_script = new_section.get("gcode").strip()
template.reload(new_script)


def load_config(config):
return PrinterGCodeMacro(config)
Expand Down
6 changes: 6 additions & 0 deletions test/klippy/gcode_jinja2_ext_do.test
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,9 @@ NONE_TEST

# Move again
G1 Z9

# Reload macros command
RELOAD_GCODE_MACROS

# test again
NONE_TEST
Loading