From 9399911490a8163ea40b14eac1431ff469eeaf3e Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Fri, 19 Jan 2018 21:25:58 -0500 Subject: [PATCH] klippy: Add support for dynamic loading of python modules Check if a config section matches a python filename in the klippy/extras/ directory. If it does, load that python code to handle the config section. Signed-off-by: Kevin O'Connor --- klippy/extras/__init__.py | 5 +++++ klippy/klippy.py | 20 +++++++++++++++++--- 2 files changed, 22 insertions(+), 3 deletions(-) create mode 100644 klippy/extras/__init__.py diff --git a/klippy/extras/__init__.py b/klippy/extras/__init__.py new file mode 100644 index 00000000..bd206ac5 --- /dev/null +++ b/klippy/extras/__init__.py @@ -0,0 +1,5 @@ +# Package definition for the extras directory +# +# Copyright (C) 2018 Kevin O'Connor +# +# This file may be distributed under the terms of the GNU GPLv3 license. diff --git a/klippy/klippy.py b/klippy/klippy.py index da891717..2bc5269d 100644 --- a/klippy/klippy.py +++ b/klippy/klippy.py @@ -4,7 +4,7 @@ # Copyright (C) 2016-2018 Kevin O'Connor # # This file may be distributed under the terms of the GNU GPLv3 license. -import sys, optparse, logging, time, threading +import sys, os, optparse, logging, time, threading import collections, ConfigParser, importlib import util, reactor, queuelogger, msgproto import gcode, pins, mcu, chipmisc, toolhead, extruder, heater, fan @@ -184,6 +184,16 @@ class Printer: stats = [cb(eventtime) for cb in self.stats_cb] logging.info("Stats %.1f: %s", eventtime, ' '.join(stats)) return eventtime + 1. + def _try_load_module(self, config, section): + if section in self.objects: + return + module_name = section.split()[0] + py_name = os.path.join(os.path.dirname(__file__), + 'extras', module_name + '.py') + if not os.path.exists(py_name): + return + mod = importlib.import_module('extras.' + module_name) + self.objects[section] = mod.load_config(config.getsection(section)) def _load_config(self): self.fileconfig = ConfigParser.RawConfigParser() config_file = self.start_args['config_file'] @@ -195,13 +205,17 @@ class Printer: ConfigLogger(self.fileconfig, self.bglogger) # Create printer components config = ConfigWrapper(self, 'printer') - for m in [pins, mcu, chipmisc, toolhead, extruder, heater, fan]: + for m in [pins, mcu]: + m.add_printer_objects(self, config) + for section in self.fileconfig.sections(): + self._try_load_module(config, section) + for m in [chipmisc, toolhead, extruder, heater, fan]: m.add_printer_objects(self, config) # Validate that there are no undefined parameters in the config file valid_sections = { s: 1 for s, o in self.all_config_options } for section in self.fileconfig.sections(): section = section.lower() - if section not in valid_sections: + if section not in valid_sections and section not in self.objects: raise self.config_error("Unknown config file section '%s'" % ( section,)) for option in self.fileconfig.options(section):