klippy: No need to store fileconfig in main printer object

Just pass the fileconfig reference to the ConfigWrapper instances.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2018-02-03 12:17:42 -05:00
parent 33bdc2fc32
commit 7e3adde542
1 changed files with 22 additions and 24 deletions

View File

@ -50,8 +50,9 @@ class ConfigWrapper:
error = ConfigParser.Error
class sentinel:
pass
def __init__(self, printer, section):
def __init__(self, printer, fileconfig, section):
self.printer = printer
self.fileconfig = fileconfig
self.section = section
def get_printer(self):
return self.printer
@ -60,7 +61,7 @@ class ConfigWrapper:
def _get_wrapper(self, parser, option, default,
minval=None, maxval=None, above=None, below=None):
if (default is not self.sentinel
and not self.printer.fileconfig.has_option(self.section, option)):
and not self.fileconfig.has_option(self.section, option)):
return default
self.printer.all_config_options[
(self.section.lower(), option.lower())] = 1
@ -89,18 +90,16 @@ class ConfigWrapper:
option, self.section, below))
return v
def get(self, option, default=sentinel):
return self._get_wrapper(self.printer.fileconfig.get, option, default)
return self._get_wrapper(self.fileconfig.get, option, default)
def getint(self, option, default=sentinel, minval=None, maxval=None):
return self._get_wrapper(
self.printer.fileconfig.getint, option, default, minval, maxval)
def getfloat(self, option, default=sentinel
, minval=None, maxval=None, above=None, below=None):
return self._get_wrapper(
self.printer.fileconfig.getfloat, option, default
, minval, maxval, above, below)
self.fileconfig.getint, option, default, minval, maxval)
def getfloat(self, option, default=sentinel,
minval=None, maxval=None, above=None, below=None):
return self._get_wrapper(self.fileconfig.getfloat, option, default,
minval, maxval, above, below)
def getboolean(self, option, default=sentinel):
return self._get_wrapper(
self.printer.fileconfig.getboolean, option, default)
return self._get_wrapper(self.fileconfig.getboolean, option, default)
def getchoice(self, option, choices, default=sentinel):
c = self.get(option, default)
if c not in choices:
@ -109,11 +108,11 @@ class ConfigWrapper:
option, self.section))
return choices[c]
def getsection(self, section):
return ConfigWrapper(self.printer, section)
return ConfigWrapper(self.printer, self.fileconfig, section)
def has_section(self, section):
return self.printer.fileconfig.has_section(section)
return self.fileconfig.has_section(section)
def get_prefix_sections(self, prefix):
return [self.getsection(s) for s in self.printer.fileconfig.sections()
return [self.getsection(s) for s in self.fileconfig.sections()
if s.startswith(prefix)]
class ConfigLogger():
@ -145,7 +144,6 @@ class Printer:
self.is_shutdown = False
self.async_shutdown_msg = ""
self.run_result = None
self.fileconfig = None
self.stats_cb = []
self.state_cb = []
def get_start_args(self):
@ -194,31 +192,31 @@ class Printer:
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()
def _read_config(self):
fileconfig = ConfigParser.RawConfigParser()
config_file = self.start_args['config_file']
res = self.fileconfig.read(config_file)
res = fileconfig.read(config_file)
if not res:
raise self.config_error("Unable to open config file %s" % (
config_file,))
if self.bglogger is not None:
ConfigLogger(self.fileconfig, self.bglogger)
ConfigLogger(fileconfig, self.bglogger)
# Create printer components
config = ConfigWrapper(self, 'printer')
config = ConfigWrapper(self, fileconfig, 'printer')
for m in [pins, mcu]:
m.add_printer_objects(self, config)
for section in self.fileconfig.sections():
for section in fileconfig.sections():
self._try_load_module(config, section)
for m in [chipmisc, toolhead, extruder, heater]:
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():
for section in fileconfig.sections():
section = section.lower()
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):
for option in fileconfig.options(section):
option = option.lower()
if (section, option) not in self.all_config_options:
raise self.config_error(
@ -232,7 +230,7 @@ class Printer:
def _connect(self, eventtime):
self.reactor.unregister_timer(self.connect_timer)
try:
self._load_config()
self._read_config()
for cb in self.state_cb:
if self.state_message is not message_startup:
return self.reactor.NEVER