summaryrefslogtreecommitdiff
path: root/paste/util
diff options
context:
space:
mode:
authorianb <devnull@localhost>2005-05-14 05:50:19 +0000
committerianb <devnull@localhost>2005-05-14 05:50:19 +0000
commit9b267d3e23b5935b8be4fcea139186f570f6f23c (patch)
tree46d874cdacad6d5b5653cfa8098d8752d8705476 /paste/util
parent426fea4d2bfa614e7247459ac80818b3c57de523 (diff)
downloadpaste-9b267d3e23b5935b8be4fcea139186f570f6f23c.tar.gz
Starting work on making servers pluggable (have to commit as I rearrange files)
Diffstat (limited to 'paste/util')
-rw-r--r--paste/util/plugin.py54
1 files changed, 54 insertions, 0 deletions
diff --git a/paste/util/plugin.py b/paste/util/plugin.py
new file mode 100644
index 0000000..2e61294
--- /dev/null
+++ b/paste/util/plugin.py
@@ -0,0 +1,54 @@
+import os
+import import_string
+
+paste_dir = os.path.dirname(os.path.dirname(__file__))
+
+def find_plugins(dir):
+ """
+ Looks in directory for plugins, and returns them by name.
+ A plugin can be a .txt file, .py file, or directory.
+ """
+ plugins = []
+ dir = os.path.join(paste_dir, dir))
+ for name in os.listdir(dir):
+ if os.path.isdir(os.path.join(dir, name)):
+ plugins.append(name)
+ ext = os.path.splitext(name)[1].lower()
+ if name in ('.py', '.txt'):
+ plugins.append(name)
+ return plugins
+
+def load_plugin_module(dir, dir_package, name):
+ """
+ Loads a plugin module. Either the module is imported, or if it
+ is a .txt file then the file is loaded and must contain the name
+ of another module to be loaded (empty and comment lines ignored).
+
+ dir_package is the package name of the directory. Dotted names
+ are not supported.
+ """
+ dir = os.path.join(paste_dir, dir)
+ if os.path.exists(os.path.join(dir, name + '.txt')):
+ module_name = parse_txt_plugin(os.path.join(dir, name + '.txt'))
+ else:
+ module_name = dir_package + '.' + name
+ return import_string.import_module(module_name)
+
+def parse_txt_plugin(fn):
+ f = open(fn)
+ try:
+ module_name = None
+ for line in f:
+ line = line.strip()
+ if not line or line.startswith('#'):
+ continue
+ if module_name is not None:
+ raise ValueError(
+ "Plugin file %s contains more than one line that "
+ "could be interpreted as a module name" % fn)
+ if module_name is None:
+ raise ValueError(
+ "Plugin file %s does not contain a module name" % fn)
+ return module_name
+ finally:
+ f.close()