Package yapsy :: Module VersionedPluginManager
[hide private]

Source Code for Module yapsy.VersionedPluginManager

  1  #!/usr/bin/python 
  2  # -*- coding: utf-8; tab-width: 4; indent-tabs-mode: t -*- 
  3   
  4  """ 
  5  Defines the basic interface for a plugin manager that also keeps track 
  6  of versions of plugins 
  7  """ 
  8   
  9  import sys, os 
 10  import logging 
 11  from distutils.version import StrictVersion 
 12   
 13  from PluginManager import PluginManager, PluginInfo, PluginManagerDecorator 
 14  from IPlugin import IPlugin 
 15   
16 -class VersionedPluginInfo(PluginInfo):
17 """ 18 Gather some info about a plugin such as its name, author, 19 description... 20 """ 21
22 - def __init__(self, plugin_name, plugin_path):
23 """ 24 Set the namle and path of the plugin as well as the default 25 values for other usefull variables. 26 """ 27 PluginInfo.__init__(self, plugin_name, plugin_path) 28 # version number is now required to be a StrictVersion object 29 self.version = StrictVersion("0.0")
30
31 - def setVersion(self, vstring):
32 self.version = StrictVersion(vstring)
33 34
35 -class VersionedPluginManager(PluginManagerDecorator):
36 """ 37 Manage several plugins by ordering them in several categories with 38 versioning capabilities. 39 """ 40
41 - def __init__(self, 42 decorated_manager=None, 43 categories_filter={"Default":IPlugin}, 44 directories_list=None, 45 plugin_info_ext="yapsy-plugin"):
46 """ 47 Create the plugin manager and record the ConfigParser instance 48 that will be used afterwards. 49 50 The ``config_change_trigger`` argument can be used to set a 51 specific method to call when the configuration is 52 altered. This will let the client application manage the way 53 they want the configuration to be updated (e.g. write on file 54 at each change or at precise time intervalls or whatever....) 55 """ 56 # Create the base decorator class 57 PluginManagerDecorator.__init__(self,decorated_manager, 58 categories_filter, 59 directories_list, 60 plugin_info_ext) 61 # prepare the mapping of the latest version of each plugin 62 self.setPluginInfoClass(VersionedPluginInfo) 63 self._prepareVersionMapping()
64
65 - def _prepareVersionMapping(self):
66 """ 67 Create a mapping that will make it possible to easily provide 68 the latest version of each plugin. 69 """ 70 self.latest_mapping = {} 71 for categ in self._component.categories_interfaces.keys(): 72 self.latest_mapping[categ] = []
73 74
75 - def setCategoriesFilter(self, categories_filter):
76 """ 77 Set the categories of plugins to be looked for as well as the 78 way to recognise them. 79 80 The ``categories_filter`` first defines the various categories 81 in which the plugins will be stored via its keys and it also 82 defines the interface tha has to be inherited by the actual 83 plugin class belonging to each category. 84 85 A call to this class will also reset the mapping of the latest 86 version for each plugin. 87 """ 88 self._component.setCategoriesFilter(self, categories_filter) 89 # prepare the mapping of the latest version of each plugin 90 self._prepareVersionMapping()
91 92
93 - def getLatestPluginsOfCategory(self,category_name):
94 """ 95 Return the list of all plugins belonging to a category. 96 """ 97 # print "%s.getLatestPluginsOfCategory" % self.__class__ 98 return self.latest_mapping[category_name]
99
100 - def loadPlugins(self, callback=None):
101 """ 102 Load the candidate plugins that have been identified through a 103 previous call to locatePlugins. 104 105 In addition to the baseclass functionality, this subclass also 106 needs to find the latest version of each plugin. 107 """ 108 # print "%s.loadPlugins" % self.__class__ 109 self._component.loadPlugins(callback) 110 111 # Search through all the loaded plugins to find the latest 112 # version of each. 113 for categ, items in self._component.category_mapping.iteritems(): 114 unique_items = {} 115 for item in items: 116 if item.name in unique_items: 117 stored = unique_items[item.name] 118 if item.version > stored.version: 119 unique_items[item.name] = item 120 else: 121 unique_items[item.name] = item 122 self.latest_mapping[categ] = unique_items.values()
123