Back to index

python-cliapp  1.20120630
Public Member Functions | Public Attributes | Static Public Attributes | Private Attributes
cliapp.pluginmgr.PluginManager Class Reference

List of all members.

Public Member Functions

def __init__
def plugin_files
def plugins
def __getitem__
def find_plugin_files
def load_plugins
def is_older
def load_plugin_file
def compatible_version
def parse_version
def enable_plugins
def disable_plugins

Public Attributes

 locations
 plugin_arguments
 plugin_keyword_arguments
 application_version

Static Public Attributes

string suffix = '_plugin.py'

Private Attributes

 _plugins
 _plugin_files

Detailed Description

Manage plugins.

This class finds and loads plugins, and keeps a list of them that
can be accessed in various ways.

The locations are set via the locations attribute, which is a list.

When a plugin is loaded, an instance of its class is created. This
instance is initialized using normal and keyword arguments specified
in the plugin manager attributes plugin_arguments and 
plugin_keyword_arguments.

The version of the application using the plugin manager is set via
the application_version attribute. This defaults to '0.0.0'.

Definition at line 37 of file pluginmgr.py.


Constructor & Destructor Documentation

Definition at line 58 of file pluginmgr.py.

00058 
00059     def __init__(self):
00060         self.locations = []
00061         self._plugins = None
00062         self._plugin_files = None
00063         self.plugin_arguments = []
00064         self.plugin_keyword_arguments = {}
00065         self.application_version = '0.0.0'


Member Function Documentation

def cliapp.pluginmgr.PluginManager.__getitem__ (   self,
  name 
)

Definition at line 78 of file pluginmgr.py.

00078 
00079     def __getitem__(self, name):
00080         for plugin in self.plugins:
00081             if plugin.name == name:
00082                 return plugin
00083         raise KeyError('Plugin %s is not known' % name)

Here is the call graph for this function:

def cliapp.pluginmgr.PluginManager.compatible_version (   self,
  required_application_version 
)
Check that the plugin is version-compatible with the application.

This checks the plugin's required_application_version against
the declared application version and returns True if they are
compatible, and False if not.

Definition at line 145 of file pluginmgr.py.

00145 
00146     def compatible_version(self, required_application_version):
00147         '''Check that the plugin is version-compatible with the application.
00148         
00149         This checks the plugin's required_application_version against
00150         the declared application version and returns True if they are
00151         compatible, and False if not.
00152         
00153         '''
00154 
00155         req = self.parse_version(required_application_version)
00156         app = self.parse_version(self.application_version)
00157         
00158         return app[0] == req[0] and app >= req

Here is the call graph for this function:

Here is the caller graph for this function:

def cliapp.pluginmgr.PluginManager.disable_plugins (   self,
  plugins = None 
)
Disable all or selected plugins.

Definition at line 170 of file pluginmgr.py.

00170 
00171     def disable_plugins(self, plugins=None):
00172         '''Disable all or selected plugins.'''
00173         
00174         for plugin in plugins or self.plugins:
00175             plugin.disable_wrapper()
00176 

Here is the call graph for this function:

def cliapp.pluginmgr.PluginManager.enable_plugins (   self,
  plugins = None 
)
Enable all or selected plugins.

Definition at line 164 of file pluginmgr.py.

00164 
00165     def enable_plugins(self, plugins=None):
00166         '''Enable all or selected plugins.'''
00167         
00168         for plugin in plugins or self.plugins:
00169             plugin.enable_wrapper()

Here is the call graph for this function:

Find files that may contain plugins.

This finds all files named *_plugin.py in all locations.
The returned list is sorted.

Definition at line 84 of file pluginmgr.py.

00084 
00085     def find_plugin_files(self):
00086         '''Find files that may contain plugins.
00087         
00088         This finds all files named *_plugin.py in all locations.
00089         The returned list is sorted.
00090         
00091         '''
00092         
00093         pathnames = []
00094         
00095         for location in self.locations:
00096             try:
00097                 basenames = os.listdir(location)
00098             except os.error:
00099                 continue
00100             for basename in basenames:
00101                 s = os.path.join(location, basename)
00102                 if s.endswith(self.suffix) and os.path.exists(s):
00103                     pathnames.append(s)
00104         
00105         return sorted(pathnames)

Here is the caller graph for this function:

def cliapp.pluginmgr.PluginManager.is_older (   self,
  version1,
  version2 
)
Is version1 older than version2?

Definition at line 122 of file pluginmgr.py.

00122 
00123     def is_older(self, version1, version2):
00124         '''Is version1 older than version2?'''
00125         return self.parse_version(version1) < self.parse_version(version2)

Here is the call graph for this function:

Here is the caller graph for this function:

def cliapp.pluginmgr.PluginManager.load_plugin_file (   self,
  pathname 
)
Return plugin classes in a plugin file.

Definition at line 126 of file pluginmgr.py.

00126 
00127     def load_plugin_file(self, pathname):
00128         '''Return plugin classes in a plugin file.'''
00129 
00130         name, ext = os.path.splitext(os.path.basename(pathname))
00131         f = file(pathname, 'r')
00132         module = imp.load_module(name, f, pathname, 
00133                                  ('.py', 'r', imp.PY_SOURCE))
00134         f.close()
00135         
00136         plugins = []
00137         for dummy, member in inspect.getmembers(module, inspect.isclass):
00138             if issubclass(member, Plugin):
00139                 p = member(*self.plugin_arguments,
00140                            **self.plugin_keyword_arguments)
00141                 if self.compatible_version(p.required_application_version):
00142                     plugins.append(p)
00143         
00144         return plugins

Here is the call graph for this function:

Here is the caller graph for this function:

Load plugins from all plugin files.

Definition at line 106 of file pluginmgr.py.

00106 
00107     def load_plugins(self):
00108         '''Load plugins from all plugin files.'''
00109         
00110         plugins = dict()
00111         
00112         for pathname in self.plugin_files:
00113             for plugin in self.load_plugin_file(pathname):
00114                 if plugin.name in plugins:
00115                     p = plugins[plugin.name]
00116                     if self.is_older(p.version, plugin.version):
00117                         plugins[plugin.name] = plugin
00118                 else:
00119                     plugins[plugin.name] = plugin
00120 
00121         return plugins.values()

Here is the call graph for this function:

Here is the caller graph for this function:

def cliapp.pluginmgr.PluginManager.parse_version (   self,
  version 
)
Parse a string represenation of a version into list of ints.

Definition at line 159 of file pluginmgr.py.

00159 
00160     def parse_version(self, version):
00161         '''Parse a string represenation of a version into list of ints.'''
00162         
00163         return [int(s) for s in version.split('.')]

Here is the caller graph for this function:

Definition at line 67 of file pluginmgr.py.

00067 
00068     def plugin_files(self):
00069         if self._plugin_files is None:
00070             self._plugin_files = self.find_plugin_files()
00071         return self._plugin_files

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 73 of file pluginmgr.py.

00073 
00074     def plugins(self):
00075         if self._plugins is None:
00076             self._plugins = self.load_plugins()
00077         return self._plugins

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Definition at line 61 of file pluginmgr.py.

Definition at line 60 of file pluginmgr.py.

Definition at line 64 of file pluginmgr.py.

Definition at line 59 of file pluginmgr.py.

Definition at line 62 of file pluginmgr.py.

Definition at line 63 of file pluginmgr.py.

string cliapp.pluginmgr.PluginManager.suffix = '_plugin.py' [static]

Definition at line 56 of file pluginmgr.py.


The documentation for this class was generated from the following file: