Skip to content

Commit

Permalink
plugins v1
Browse files Browse the repository at this point in the history
  • Loading branch information
bwnance committed Oct 31, 2023
1 parent e7893db commit 5e031e5
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 32 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ klippy/.version
ci_build/
ci_cache/
_test_.*
klippy/plugins/*
!klippy/plugins/__init__.py
4 changes: 4 additions & 0 deletions klippy/extras/danger_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ def __init__(self, config):
"error_on_unused_config_options", True
)

self.allow_plugin_override = config.getboolean(
"allow_plugin_override", False
)


def load_config(config):
return DangerOptions(config)
71 changes: 39 additions & 32 deletions klippy/klippy.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ def __init__(self, main_reactor, bglogger, start_args):
self.run_result = None
self.event_handlers = {}
self.objects = collections.OrderedDict()
self.danger_options = None
# Init printer components that must be setup prior to config
for m in [gcode, webhooks]:
m.add_early_printer_objects(self)
Expand Down Expand Up @@ -129,17 +130,47 @@ def load_object(self, config, section, default=configfile.sentinel):
return self.objects[section]
module_parts = section.split()
module_name = module_parts[0]
py_name = os.path.join(
extras_py_name = os.path.join(
os.path.dirname(__file__), "extras", module_name + ".py"
)
py_dirname = os.path.join(
extras_py_dirname = os.path.join(
os.path.dirname(__file__), "extras", module_name, "__init__.py"
)
if not os.path.exists(py_name) and not os.path.exists(py_dirname):

plugins_py_dirname = os.path.join(
os.path.dirname(__file__), "plugins", module_name, "__init__.py"
)
plugins_py_name = os.path.join(
os.path.dirname(__file__), "plugins", module_name + ".py"
)

found_in_extras = os.path.exists(extras_py_name) or os.path.exists(
extras_py_dirname
)
found_in_plugins = os.path.exists(plugins_py_name)
if not found_in_extras and not found_in_plugins:
if default is not configfile.sentinel:
return default
raise self.config_error("Unable to load module '%s'" % (section,))
mod = importlib.import_module("extras." + module_name)

if (
found_in_extras
and found_in_plugins
and not self.danger_options.allow_plugin_override
):
raise self.config_error(
"Module '%s' found in both extras and plugins!" % (section,)
)

if found_in_plugins:
mod_spec = importlib.util.spec_from_file_location(
"extras." + module_name, plugins_py_name
)
mod = importlib.util.module_from_spec(mod_spec)
mod_spec.loader.exec_module(mod)
else:
mod = importlib.import_module("extras." + module_name)

init_func = "load_config"
if len(module_parts) > 1:
init_func = "load_config_prefix"
Expand All @@ -154,10 +185,10 @@ def load_object(self, config, section, default=configfile.sentinel):
def _read_config(self):
self.objects["configfile"] = pconfig = configfile.PrinterConfig(self)
config = pconfig.read_main_config()
danger_options = self.load_object(config, "danger_options")
self.danger_options = self.load_object(config, "danger_options")
if (
self.bglogger is not None
and danger_options.log_config_file_at_startup
and self.danger_options.log_config_file_at_startup
):
pconfig.log_config(config)
# Create printer components
Expand All @@ -168,7 +199,7 @@ def _read_config(self):
for m in [toolhead]:
m.add_printer_objects(config)
# Validate that there are no undefined parameters in the config file
error_on_unused = danger_options.error_on_unused_config_options
error_on_unused = self.danger_options.error_on_unused_config_options
pconfig.check_unused_options(config, error_on_unused)

def _build_protocol_error_message(self, e):
Expand Down Expand Up @@ -437,32 +468,8 @@ def main():
logging.info("Starting Klippy...")
git_info = util.get_git_version()
git_vers = git_info["version"]
extra_files = [
fname
for code, fname in git_info["file_status"]
if (
code in ("??", "!!")
and fname.endswith(".py")
and (
fname.startswith("klippy/kinematics/")
or fname.startswith("klippy/extras/")
)
)
]
modified_files = [
fname for code, fname in git_info["file_status"] if code == "M"
]

extra_git_desc = ""
if extra_files:
if not git_vers.endswith("-dirty"):
git_vers = git_vers + "-dirty"
if len(extra_files) > 10:
extra_files[10:] = ["(+%d files)" % (len(extra_files) - 10,)]
extra_git_desc += "\nUntracked files: %s" % (", ".join(extra_files),)
if modified_files:
if len(modified_files) > 10:
modified_files[10:] = ["(+%d files)" % (len(modified_files) - 10,)]
extra_git_desc += "\nModified files: %s" % (", ".join(modified_files),)
extra_git_desc += "\nBranch: %s" % (git_info["branch"])
extra_git_desc += "\nRemote: %s" % (git_info["remote"])
extra_git_desc += "\nTracked URL: %s" % (git_info["url"])
Expand Down
5 changes: 5 additions & 0 deletions klippy/plugins/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Package definition for the plugin directory
#
# Copyright (C) 2023 Brandon Nance <[email protected]>
#
# This file may be distributed under the terms of the GNU GPLv3 license.

0 comments on commit 5e031e5

Please sign in to comment.