diff options
author | Casey Rodarmor <casey@rodarmor.com> | 2016-01-16 19:00:15 -0800 |
---|---|---|
committer | Anish Athalye <me@anishathalye.com> | 2016-02-06 15:14:35 -0500 |
commit | eeb4c284fb71dd62eaf5ee7017e23b64c2c899d7 (patch) | |
tree | 0f79d448c9f0864153d96d36bf43b263b2a15e6d /dotbot/util | |
parent | ba9e9cbe709b3a736d10dae0922b09a5eb0cde87 (diff) |
Add plugin loader
Diffstat (limited to 'dotbot/util')
-rw-r--r-- | dotbot/util/module.py | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/dotbot/util/module.py b/dotbot/util/module.py new file mode 100644 index 0000000..af6b0ed --- /dev/null +++ b/dotbot/util/module.py @@ -0,0 +1,29 @@ +import sys, os.path + +# We keep references to loaded modules so they don't get garbage collected. +loaded_modules = [] + +def load(path): + basename = os.path.basename(path) + module_name, extension = os.path.splitext(basename) + plugin = load_module(module_name, path) + loaded_modules.append(plugin) + +if sys.version_info >= (3, 5): + import importlib.util + + def load_module(module_name, path): + spec = importlib.util.spec_from_file_location(module_name, path) + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + return module +elif sys.version_info >= (3, 3): + from importlib.machinery import SourceFileLoader + + def load_module(module_name, path): + return SourceFileLoader(module_name, path).load_module() +else: + import imp + + def load_module(module_name, path): + return imp.load_source(module_name, path) |