[Patchew-devel] [PATCH 5/6] mod: Lazy access module config from DB
Fam Zheng
famz at redhat.com
Wed Oct 31 01:28:55 UTC 2018
_init_module is called in app.ready where DB access is bad: the tables
may not be ready yet. Also we always wanted the module config to be
effective so there isn't much point in loading it here.
Signed-off-by: Fam Zheng <famz at redhat.com>
---
mod.py | 30 ++++++++++--------------------
1 file changed, 10 insertions(+), 20 deletions(-)
diff --git a/mod.py b/mod.py
index 90880ea..4446171 100644
--- a/mod.py
+++ b/mod.py
@@ -29,6 +29,13 @@ class PatchewModule(object):
_module_init_config(self.__class__)
return PC.objects.get(name=self.name)
+ def enabled(self):
+ try:
+ m = self.get_model()
+ return m.enabled
+ except:
+ return True
+
def get_config_raw(self):
return self.get_model().config or ""
@@ -164,18 +171,6 @@ def _module_init_config(cls):
pc.save()
return pc
-def _init_module(cls):
- name = cls.name
- if name in _loaded_modules:
- raise Exception("The module named '%s' is already loaded" % name)
- pc = _module_init_config(cls)
- if not pc.enabled:
- return None
- i = cls()
- # TODO: let i.enabled follows pc.enabled
- i.enabled = pc.enabled
- return i
-
def load_modules():
_module_path = settings.MODULE_DIR
sys.path.append(_module_path)
@@ -187,17 +182,12 @@ def load_modules():
except:
traceback.print_exc()
for cls in PatchewModule.__subclasses__():
- try:
- i = _init_module(cls)
- if not i:
- continue
- _loaded_modules[cls.name] = i
+ if cls.name not in _loaded_modules:
+ _loaded_modules[cls.name] = cls()
print("Loaded module:", cls.name)
- except Exception as e:
- print("Cannot load module '%s':" % cls, e)
def dispatch_module_hook(hook_name, **params):
- for i in [x for x in list(_loaded_modules.values()) if x.enabled]:
+ for i in [x for x in list(_loaded_modules.values()) if x.enabled()]:
if hasattr(i, hook_name):
try:
getattr(i, hook_name)(**params)
--
2.17.2
More information about the Patchew-devel
mailing list