Coverage for portality / lib / plugin.py: 82%
51 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-05-04 09:41 +0100
« prev ^ index » next coverage.py v7.13.5, created at 2026-05-04 09:41 +0100
1import importlib
4# Note that we delay import of app to the functions which need it,
5# since we may want to load plugins during app creation too, and otherwise
6# we'd get circular import problems
8class PluginException(Exception):
9 pass
12def load_class_raw(classpath):
13 modpath = ".".join(classpath.split(".")[:-1])
14 classname = classpath.split(".")[-1]
15 try:
16 mod = importlib.import_module(modpath)
17 except ImportError:
18 return None
19 klazz = getattr(mod, classname, None)
20 return klazz
23def load_class(classpath, cache_class_ref=True):
24 from portality.core import app
25 klazz = app.config.get("PLUGIN_CLASS_REFS", {}).get(classpath)
26 if klazz is not None:
27 return klazz
29 klazz = load_class_raw(classpath)
30 if klazz is None:
31 app.logger.info("Could not load function {x}".format(x=classpath))
32 return None
34 if cache_class_ref:
35 if "PLUGIN_CLASS_REFS" not in app.config:
36 app.config["PLUGIN_CLASS_REFS"] = {}
37 app.config["PLUGIN_CLASS_REFS"][classpath] = klazz
39 return klazz
42def load_module(modpath):
43 return importlib.import_module(modpath)
46def load_function_raw(fnpath):
47 modpath = ".".join(fnpath.split(".")[:-1])
48 fnname = fnpath.split(".")[-1]
49 try:
50 mod = importlib.import_module(modpath)
51 except ImportError:
52 return None
53 fn = getattr(mod, fnname, None)
54 return fn
57def load_function(fnpath, cache_fn_ref=True):
58 from portality.core import app
59 fn = app.config.get("PLUGIN_FN_REFS", {}).get(fnpath)
60 if fn is not None:
61 return fn
63 fn = load_function_raw(fnpath)
64 if fn is None:
65 app.logger.info("Could not load function {x}".format(x=fnpath))
66 return None
68 if cache_fn_ref:
69 if "PLUGIN_FN_REFS" not in app.config:
70 app.config["PLUGIN_FN_REFS"] = {}
71 app.config["PLUGIN_FN_REFS"][fnpath] = fn
73 return fn