From 8c31853d440d6aa6541ed03d21c1d92e62149d54 Mon Sep 17 00:00:00 2001 From: Mike Racine Date: Tue, 29 Oct 2019 19:16:56 -0400 Subject: [PATCH] More configurer validation changes, should fill in missing defaults into config --- halibot/halconfigurer.py | 17 +++++++++++------ halibot/halibot.py | 11 ++++++++++- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/halibot/halconfigurer.py b/halibot/halconfigurer.py index 88ac83a..9dd6f11 100644 --- a/halibot/halconfigurer.py +++ b/halibot/halconfigurer.py @@ -89,21 +89,26 @@ def validate_config(self, config, fill_default=True): for key, validator in self.options.items(): tmp = config.get(key) + # Unsure on whether the item this one depends on NOT being present in config is warranted to make this required + required = not validator.depends or validator.depends not in config or config.get(validator.depends) + # Ensure the key exists, and if we aren't taking defaults, ensure we report - if not tmp and not fill_default: + if not tmp and not required: + break + elif not tmp and not fill_default: missing.append(key) - continue elif not tmp: ret[key] = validator.default - continue - - # Propogate ValueError if there is one - ret[key] = validator.validate(tmp) + else: + # Propogate ValueError if there is one + ret[key] = validator.validate(tmp) if missing and not fill_default: str = "Missing key" + ("s" if len(missing) > 1 else "") + ": " + ", ".join(missing) raise KeyError(str) + return ret + # Validate an individual key/value pair. Probably used for runtime changes/configurerers. def validate_key(self, key, value): validator = self.options.get(key) diff --git a/halibot/halibot.py b/halibot/halibot.py index 123eb0d..5a7bdf0 100644 --- a/halibot/halibot.py +++ b/halibot/halibot.py @@ -12,6 +12,7 @@ from .halmodule import HalModule from .halagent import HalAgent from .halauth import HalAuth +from .halconfigurer import HalConfigurer # Avoid appending "." if it i if "." not in sys.path: @@ -120,7 +121,11 @@ def load_object(self, pkg, conf={}): if len(split) == 2: obj = self._get_class_from_package(*split) if obj and self._check_version(obj): - return obj(self, conf=conf) + # Check config and any missing values + cfgr = obj.Configurer() + conf = cfgr.validate_config(conf) + conf['of'] = pkg + return obj(self, conf=conf) else: self.log.error("Invalid class identifier {}, must contain only 1 ':'".format(conf["of"])) return None @@ -132,6 +137,10 @@ def _instantiate_objects(self, key): conf = inst[k] obj = self.load_object(conf["of"], conf=conf) + if obj.config != conf: + self.config[key + "-instances"][k] = obj.config + self.config._write_config() + if obj: self.add_instance(k, obj)