aboutsummaryrefslogtreecommitdiffstats
path: root/klippy/klippy.py
diff options
context:
space:
mode:
authorKevin O'Connor <kevin@koconnor.net>2020-05-05 14:10:30 -0400
committerKevin O'Connor <kevin@koconnor.net>2020-05-08 11:05:14 -0400
commit787ed452c2abe3bad81ffaf732b0b2609e3534bb (patch)
treeefe45de24864649b4219e6a45c1ebfb82574fe28 /klippy/klippy.py
parent8472c57b594e6f8ff26d7be32b4bdd5072ed439a (diff)
downloadkutter-787ed452c2abe3bad81ffaf732b0b2609e3534bb.tar.gz
kutter-787ed452c2abe3bad81ffaf732b0b2609e3534bb.tar.xz
kutter-787ed452c2abe3bad81ffaf732b0b2609e3534bb.zip
klippy: Rename try_load_module() to load_object()
Rename try_load_module() so that it uses consistent naming for "printer objects". Change the function to raise an error by default if the specified module does not exist. Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Diffstat (limited to 'klippy/klippy.py')
-rw-r--r--klippy/klippy.py17
1 files changed, 11 insertions, 6 deletions
diff --git a/klippy/klippy.py b/klippy/klippy.py
index 0d201adf..84450aea 100644
--- a/klippy/klippy.py
+++ b/klippy/klippy.py
@@ -93,7 +93,7 @@ class Printer:
if module in self.objects:
return [(module, self.objects[module])] + objs
return objs
- def try_load_module(self, config, section):
+ def load_object(self, config, section, default=configfile.sentinel):
if section in self.objects:
return self.objects[section]
module_parts = section.split()
@@ -103,15 +103,20 @@ class Printer:
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):
- return None
+ 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)
init_func = 'load_config'
if len(module_parts) > 1:
init_func = 'load_config_prefix'
init_func = getattr(mod, init_func, None)
- if init_func is not None:
- self.objects[section] = init_func(config.getsection(section))
- return self.objects[section]
+ if init_func is None:
+ if default is not configfile.sentinel:
+ return default
+ raise self.config_error("Unable to load module '%s'" % (section,))
+ self.objects[section] = init_func(config.getsection(section))
+ return self.objects[section]
def _read_config(self):
self.objects['configfile'] = pconfig = configfile.PrinterConfig(self)
config = pconfig.read_main_config()
@@ -121,7 +126,7 @@ class Printer:
for m in [pins, mcu]:
m.add_printer_objects(config)
for section_config in config.get_prefix_sections(''):
- self.try_load_module(config, section_config.get_name())
+ self.load_object(config, section_config.get_name(), None)
for m in [toolhead]:
m.add_printer_objects(config)
# Validate that there are no undefined parameters in the config file