示例#1
0
    def __init__(self,cfg):
        if not 'extensions' in cfg:
            cfg['extensions'] = {'path':[] , 'trusted': [] }
        self.plugin_man =TrustedPluginManager.TrustedPluginManager(
                      directories_list=cfg['extensions']['path'],
                      plugin_info_ext="mm-plugin",  
         			  categories_filter={"Default":Extension.Extension}, 
                      trustList=  cfg['extensions']['trusted']
                     )

        ##Allow the trusted path to be overridden
        self.trusteddirs = cfg['extensions'].get('trustedpath')
        if not self.trusteddirs:
            self.trusteddirs = [ DEFAULT_TRUSTEDPLUGIN_PATH ]
        self.trusted_man = CorePluginManager(
                      directories_list = self.trusteddirs,
                      plugin_info_ext="mm-plugin",  
         			  categories_filter={"Default":Extension.Extension}, 
                     )

        self.trusted_man.setPluginInfoClass(ExtensionInfo)
        self.trusted_man.locatePlugins()
        self.plugin_man.locatePlugins()
    #    self.known_extentions = cfg['extensions']['known']
        self.helpers=dict()
        self.mixins=dict()
示例#2
0
class ExtensionLib(object):
    """
   This is a singleton class which handles MysteryMachines extention engine.
   It supports the concept of trusted and untrusted extensions, untrusted
   extensions cannot be used at runtime.
   The set of available extensions and the set of trusted extensions are completely
   distinct.  Meta data can only be enumurated fro available extensions.

    :version:
    :author:
    """

    """ ATTRIBUTES


    known_extensions  (private)
    trusted_extensions  (private)
    plugin_man  (private)

    """
    def __init__(self,cfg):
        if not 'extensions' in cfg:
            cfg['extensions'] = {'path':[] , 'trusted': [] }
        self.plugin_man =TrustedPluginManager.TrustedPluginManager(
                      directories_list=cfg['extensions']['path'],
                      plugin_info_ext="mm-plugin",  
         			  categories_filter={"Default":Extension.Extension}, 
                      trustList=  cfg['extensions']['trusted']
                     )

        ##Allow the trusted path to be overridden
        self.trusteddirs = cfg['extensions'].get('trustedpath')
        if not self.trusteddirs:
            self.trusteddirs = [ DEFAULT_TRUSTEDPLUGIN_PATH ]
        self.trusted_man = CorePluginManager(
                      directories_list = self.trusteddirs,
                      plugin_info_ext="mm-plugin",  
         			  categories_filter={"Default":Extension.Extension}, 
                     )

        self.trusted_man.setPluginInfoClass(ExtensionInfo)
        self.trusted_man.locatePlugins()
        self.plugin_man.locatePlugins()
    #    self.known_extentions = cfg['extensions']['known']
        self.helpers=dict()
        self.mixins=dict()

    def register_helper(self,cls,helper_factory):
        """
        DEPRECATED
        
        Registers a factory callable which returns helper instances
        to be used by class cls. How cls uses helpers is up to
        it.

        Helpers are distinguished from mixin's primarily by being
        called volunterlly by there assoicated class ,and by each 
        instance of the helped class haveing it's own helper instances.
        """
        if cls in self.helpers:
           if not helper_factory in self.helpers[cls]:
                self.helpers[cls] += helper_factory
        else:
           self.helpers[cls] = [ helper_factory ]


    def unregister_helper(self,cls,helper_factory):
        """
        DEPRECATED
        Removes a hlper from the registration database.
        """
        self.helpers[cls].remove(helper_factory) 

    def get_helpers_for(self, className):
        """
        DEPRECATED

         Returns helpers for className

        @param string className : className to search for mixins for
        @return Extension :
        """
        if className in self.helpers:
            return self.helpers[className] 
        else:
            return () 

    def IsTrusted(self, extension):
        """
        Determines wheter an extension is trusted. 

        @param Extension extension : 
        @return  : true - if the extension is trusted
        """
        if extension in self.trusted_man.getAllPlugins(): return True
        else: return self.plugin_man.isPluginOk(extension)

    def SetTrust(self, ext, trusted):
        """
        Sets wether an extension is trusted or not.     

        @param Extension ext : 
        @param bool trusted : 
        """
        if ext in self.trusted_man.getAllPlugins():
            raise TrustError("Cannot change the trust status of system Plugins")

        if trusted:
            self.plugin_man.trustPlugin(ext)
        else:
            self.plugin_man.untrustPlugin(ext)

    def getExtension(self, name, category = "Default", version = None):
        """
        Returns extensionInfo for extension named name if and only if extension is trusted.


        @param string name :   Name of extension to return 
        @param string category:Category if extension
        @param string version: Reserved for later use. Must be None.
        @return Extension :
        """
        plugin = self.trusted_man.getPluginByName(name, category)
        return plugin or self.plugin_man.getPluginByName(name, category)


    def get_extension_list(self):
        """
        Generates the list of extension names known about.
        """
        # Iterate through system plugins
        for plugin in self.trusted_man.getPluginCandidates():
             yield plugin
        for plugin in self.trusted_man.getPluginsLoaded():
            yield plugin


        #Do trusted plugins
        for plugin in self.plugin_man.getPluginCandidates():
             yield plugin
        
        # Return rejected plugins.
        for plugin in self.plugin_man.getRejectedPluginInfo():
            yield plugin
    
        #Return loaded plugins.
        for plugin in self.plugin_man.getPluginsLoaded():
            yield plugin

    def findPluginByFeature(self,extension_point,featurecode, version = None ):
        """
        Finds the plugin which provides feature code 'featurecode'
        in extension point 'extension_point'.
        """

        for plugin in itertools.chain(self.plugin_man.getPluginCandidates(),self.plugin_man.getPluginsLoaded() ,self.trusted_man.getPluginCandidates() ,self.trusted_man.getPluginsLoaded()):
            if plugin.provides(extension_point,featurecode):
                if VersionNr(None) and  VersionNr(version) <= VersionNr(plugin.version)  : yield plugin

    def findFeaturesOnPoint(self,extension_point):
        """
        Finds the plugin which provides feature code 'featurecode'
        in extension point 'extension_point'.
        """
        features = set() 
        for plugin in itertools.chain(self.plugin_man.getPluginCandidates(),self.plugin_man.getPluginsLoaded() ,self.trusted_man.getPluginCandidates() ,self.trusted_man.getPluginsLoaded()):
            features |= set(plugin.features_on_point(extension_point))
        return features

    

    def loadPlugin(self,plugin):
        """Ensure that plugin is loaded. Attempts to load plugin, if possible.

        This is a NOOP if plugin is already loaded.
        """
        if plugin.isLoaded(): return

        if plugin in  self.plugin_man.getPluginCandidates():
            self.plugin_man.loadPlugin(plugin)
        elif plugin in  self.trusted_man.getPluginCandidates():
            self.trusted_man.loadPlugin(plugin)
        else: raise RuntimeError("Cant load foreign plugin")    

    def IsSystemPlugin(self,plugin):
        return ((plugin in self.trusted_man.getAllPlugins() ) or 
               (plugin in  self.trusted_man.getPluginCandidates() ) )