summary refs log tree commit diff
path: root/synapse/util
diff options
context:
space:
mode:
authorErik Johnston <erik@matrix.org>2019-09-25 17:53:13 +0100
committerErik Johnston <erik@matrix.org>2019-09-25 17:53:13 +0100
commit4fb3c129aa96377d5b78c9e39d42eee2a4e9ae88 (patch)
tree7780de5531566372372992521f2476551a51be73 /synapse/util
parentReview comments (diff)
parentMerge pull request #6089 from matrix-org/erikj/cleanup_user_ips (diff)
downloadsynapse-4fb3c129aa96377d5b78c9e39d42eee2a4e9ae88.tar.xz
Merge branch 'develop' of github.com:matrix-org/synapse into erikj/cleanup_user_ips_2
Diffstat (limited to 'synapse/util')
-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