Package Pyblosxom :: Module plugin_utils
[hide private]
[frames] | no frames]

Source Code for Module Pyblosxom.plugin_utils

  1  ####################################################################### 
  2  # This file is part of PyBlosxom. 
  3  # 
  4  # Copyright (c) 2003, 2004, 2005, 2006 Wari Wahab 
  5  #  
  6  # PyBlosxom is distributed under the MIT license.  See the file LICENSE 
  7  # for distribution details. 
  8  # 
  9  # $Id: plugin_utils.py 1012 2007-05-19 17:57:07Z willhelm $ 
 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  # this holds the list of plugins that have been loaded.  if you're running 
 25  # PyBlosxom as a long-running process, this only gets cleared when the 
 26  # process is restarted. 
 27  plugins = [] 
 28   
 29  # this holds a list of callbacks (any function that begins with cp_) and the 
 30  # list of function instances that support that callback. 
 31  # if you're running PyBlosxom as a long-running process, this only 
 32  # gets cleared when the process is restarted. 
 33  callbacks = {} 
 34   
35 -def catalogue_plugin(plugin_module):
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
53 -def get_callback_chain(chain):
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
63 -def initialize_plugins(plugin_dirs, plugin_list):
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 # we clear out the callbacks dict so we can rebuild them 89 callbacks.clear() 90 91 # handle plugin_dirs here 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
108 -def get_plugin_by_name(name):
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
125 -def get_module_name(filename):
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
139 -def get_plugin_list(plugin_list, plugin_dirs):
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 # remove plugins that start with a _ 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 # vim: shiftwidth=4 tabstop=4 expandtab 172