summaryrefslogtreecommitdiff
path: root/cliapp/pluginmgr.py
diff options
context:
space:
mode:
Diffstat (limited to 'cliapp/pluginmgr.py')
-rw-r--r--cliapp/pluginmgr.py52
1 files changed, 26 insertions, 26 deletions
diff --git a/cliapp/pluginmgr.py b/cliapp/pluginmgr.py
index 1b30932..5e99523 100644
--- a/cliapp/pluginmgr.py
+++ b/cliapp/pluginmgr.py
@@ -1,15 +1,15 @@
# Copyright (C) 2009-2012 Lars Wirzenius
-#
+#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
-#
+#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
-#
+#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
@@ -37,22 +37,22 @@ from cliapp import Plugin
class PluginManager(object):
'''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
+ 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'.
-
+
'''
-
+
suffix = '_plugin.py'
def __init__(self):
@@ -83,14 +83,14 @@ class PluginManager(object):
def find_plugin_files(self):
'''Find files that may contain plugins.
-
+
This finds all files named *_plugin.py in all locations.
The returned list is sorted.
-
+
'''
-
+
pathnames = []
-
+
for location in self.locations:
try:
basenames = os.listdir(location)
@@ -100,14 +100,14 @@ class PluginManager(object):
s = os.path.join(location, basename)
if s.endswith(self.suffix) and os.path.exists(s):
pathnames.append(s)
-
+
return sorted(pathnames)
def load_plugins(self):
'''Load plugins from all plugin files.'''
-
+
plugins = dict()
-
+
for pathname in self.plugin_files:
for plugin in self.load_plugin_file(pathname):
if plugin.name in plugins:
@@ -128,10 +128,10 @@ class PluginManager(object):
name, ext = os.path.splitext(os.path.basename(pathname))
f = file(pathname, 'r')
- module = imp.load_module(name, f, pathname,
+ module = imp.load_module(name, f, pathname,
('.py', 'r', imp.PY_SOURCE))
f.close()
-
+
plugins = []
for dummy, member in inspect.getmembers(module, inspect.isclass):
if issubclass(member, Plugin):
@@ -139,37 +139,37 @@ class PluginManager(object):
**self.plugin_keyword_arguments)
if self.compatible_version(p.required_application_version):
plugins.append(p)
-
+
return plugins
def 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.
-
+
'''
req = self.parse_version(required_application_version)
app = self.parse_version(self.application_version)
-
+
return app[0] == req[0] and app >= req
def parse_version(self, version):
'''Parse a string represenation of a version into list of ints.'''
-
+
return [int(s) for s in version.split('.')]
def enable_plugins(self, plugins=None):
'''Enable all or selected plugins.'''
-
+
for plugin in plugins or self.plugins:
plugin.enable_wrapper()
def disable_plugins(self, plugins=None):
'''Disable all or selected plugins.'''
-
+
for plugin in plugins or self.plugins:
plugin.disable_wrapper()