summary refs log tree commit diff
path: root/synapse/util/module_loader.py
diff options
context:
space:
mode:
authorErik Johnston <erik@matrix.org>2019-09-25 17:16:28 +0100
committerErik Johnston <erik@matrix.org>2019-09-25 17:16:28 +0100
commitd2bd0bc6b188cca8c85ec34c79a70596ec0e46b1 (patch)
tree15032c3b14c0c9e3e52db8cd5fa4a04d985f034e /synapse/util/module_loader.py
parentUse if `is not None` (diff)
parentThreepid validity checks on msisdns should not be dependent on 'threepid_beha... (diff)
downloadsynapse-d2bd0bc6b188cca8c85ec34c79a70596ec0e46b1.tar.xz
Merge branch 'develop' of github.com:matrix-org/synapse into erikj/cleanup_user_ips
Diffstat (limited to 'synapse/util/module_loader.py')
-rw-r--r--synapse/util/module_loader.py20
1 files changed, 19 insertions, 1 deletions
diff --git a/synapse/util/module_loader.py b/synapse/util/module_loader.py
index 522acd5aa8..7ff7eb1e4d 100644
--- a/synapse/util/module_loader.py
+++ b/synapse/util/module_loader.py
@@ -14,12 +14,13 @@
 # limitations under the License.
 
 import importlib
+import importlib.util
 
 from synapse.config._base import ConfigError
 
 
 def load_module(provider):
-    """ Loads a module with its config
+    """ Loads a synapse module with its config
     Take a dict with keys 'module' (the module name) and 'config'
     (the config dict).
 
@@ -38,3 +39,20 @@ def load_module(provider):
         raise ConfigError("Failed to parse config for %r: %r" % (provider["module"], e))
 
     return provider_class, provider_config
+
+
+def load_python_module(location: str):
+    """Load a python module, and return a reference to its global namespace
+
+    Args:
+        location (str): path to the module
+
+    Returns:
+        python module object
+    """
+    spec = importlib.util.spec_from_file_location(location, location)
+    if spec is None:
+        raise Exception("Unable to load module at %s" % (location,))
+    mod = importlib.util.module_from_spec(spec)
+    spec.loader.exec_module(mod)
+    return mod