summary refs log tree commit diff
path: root/synapse/util
diff options
context:
space:
mode:
authorRichard van der Hoff <richard@matrix.org>2019-09-24 17:03:50 +0100
committerRichard van der Hoff <richard@matrix.org>2019-09-24 17:03:50 +0100
commit9b7c4f40782b09000141d7311fc39d387336350a (patch)
tree0c64c6ace7581c2f4e83c6b79159a11d35fc20e1 /synapse/util
parentMerge remote-tracking branch 'origin/develop' into rav/saml_mapping_work (diff)
parentremove unused parameter to get_user_id_by_threepid (#6099) (diff)
downloadsynapse-9b7c4f40782b09000141d7311fc39d387336350a.tar.xz
Merge remote-tracking branch 'origin/develop' into rav/saml_mapping_work
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