1
2
3
4
5
6
7
8
9
10
11 """
12 Holds a series of utility functions for cataloguing, retrieving, and
13 manipulating callback functions and chains. Refer to the documentation
14 for which callbacks are available and their behavior.
15 """
16
17 __revision__ = "$Revision: 1012 $"
18
19 import os
20 import glob
21 import sys
22 import os.path
23
24
25
26
27 plugins = []
28
29
30
31
32
33 callbacks = {}
34
36 """
37 Goes through the plugin's contents and catalogues all the functions
38 that start with cb_. Functions that start with cb_ are callbacks.
39
40 @param plugin_module: the module to catalogue
41 @type plugin_module: module
42 """
43 listing = dir(plugin_module)
44
45 listing = [item for item in listing if item.startswith("cb_")]
46
47 for mem in listing:
48 func = getattr(plugin_module, mem)
49 memadj = mem[3:]
50 if callable(func):
51 callbacks.setdefault(memadj, []).append(func)
52
54 """
55 Returns a list of functions registered with the callback.
56
57 @returns: list of functions registered with the callback (or an
58 empty list)
59 @rtype: list of functions
60 """
61 return callbacks.get(chain, [])
62
64 """
65 Imports and initializes plugins from the directories in the list
66 specified by "plugins_dir". If no such list exists, then we don't
67 load any plugins.
68
69 If the user specifies a "load_plugins" list of plugins to load, then
70 we explicitly load those plugins in the order they're listed. If the
71 load_plugins key does not exist, then we load all the plugins in the
72 plugins directory using an alphanumeric sorting order.
73
74 NOTE: If PyBlosxom is part of a long-running process, you must
75 restart PyBlosxom in order to pick up any changes to your plugins.
76
77 @param plugin_dirs: the list of directories to add to the sys.path
78 because that's where our plugins are located.
79 @type plugin_dirs: list of strings
80
81 @param plugin_list: the list of plugins to load, or if None, we'll
82 load all the plugins we find in those dirs.
83 @type plugin_list: list of strings or None
84 """
85 if plugins:
86 return
87
88
89 callbacks.clear()
90
91
92 for mem in plugin_dirs:
93 if os.path.isdir(mem):
94 sys.path.append(mem)
95 else:
96 raise Exception("Plugin directory '%s' does not exist. " \
97 "Please check your config file." % mem)
98
99 plugin_list = get_plugin_list(plugin_list, plugin_dirs)
100
101 for mem in plugin_list:
102 _module = __import__(mem)
103 for comp in mem.split(".")[1:]:
104 _module = getattr(_module, comp)
105 catalogue_plugin(_module)
106 plugins.append(_module)
107
109 """
110 This retrieves a plugin instance (it's a Python module instance)
111 by name.
112
113 @param name: the name of the plugin to retrieve (ex: "xmlrpc")
114 @type name: string
115
116 @returns: the Python module instance for the plugin or None
117 @rtype: Python module
118 """
119 if plugins:
120 for mem in plugins:
121 if mem.__name__ == name:
122 return mem
123 return None
124
126 """
127 Takes a filename and returns the module name from the filename.
128
129 Example: passing in "/blah/blah/blah/module.ext" returns "module"
130
131 @param filename: the filename in question (with a full path)
132 @type filename: string
133
134 @returns: the filename without path or extension
135 @rtype: string
136 """
137 return os.path.splitext(os.path.split(filename)[1])[0]
138
140 """
141 This handles the situation where the user has provided a series of
142 plugin dirs, but has not specified which plugins they want to load
143 from those dirs. In this case, we load all possible plugins except
144 the ones whose names being with _ .
145
146 @param plugin_list: List of plugins to load
147 @type plugin_list: list or None
148
149 @param plugin_dirs: A list of directories where plugins can be loaded from
150 @type plugin_dirs: list
151
152 @return: list of python module names of the plugins to load
153 @rtype: list of strings
154 """
155 if plugin_list == None:
156 plugin_list = []
157 for mem in plugin_dirs:
158 file_list = glob.glob(os.path.join(mem, "*.py"))
159
160 file_list = [get_module_name(filename) for filename in file_list]
161
162
163 file_list = [plugin for plugin in file_list \
164 if not plugin.startswith('_')]
165 plugin_list += file_list
166
167 plugin_list.sort()
168
169 return plugin_list
170
171
172