PluginLoader.py :  » Network » Luma » luma-2.4 » lib » luma » base » backend » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Network » Luma 
Luma » luma 2.4 » lib » luma » base » backend » PluginLoader.py
# -*- coding: utf-8 -*-

###########################################################################
#    Copyright (C) 2003 by Wido Depping                                      
#    <widod@users.sourceforge.net>                                                             
#
# Copyright: See COPYING file that comes with this distribution
#
###########################################################################

from os import listdir
import os.path
import sys
import imp

import environment
from base.utils.backend.LogObject import LogObject


class PluginLoader(object):
    """A class for finding and loading Luma plugins.
    
    self.PLUGINS: A dictionary of the plugins. Keys are the plugin names. 
    The values contain metainformation for each plugin. These informations 
    are stored in a dictionary with the following keys:
    
    PLUGIN_NAME: Name of the plugin (string).
    
    PLUGIN_VERSION: The version of the plugin (string).
    
    PLUGIN_AUTHOR: Plugin author (string).
    
    PLUGIN_FILE: The base python script for the plugin (string).
    
    PLUGIN_LOAD: Indicates if the plugin should be loaded (integer).
    
    PLUGIN_PATH: Base path of the plugin (string).
    
    PLUGIN_CODE: The python code object (callable code).
    
    
    """

    def __init__(self, pluginsToLoad=[]):
        self.PLUGINS = {}
        
        # get the base diretory of the plugins as a string
        self.pluginBaseDir = os.path.join(environment.lumaInstallationPrefix,  "lib", "luma", "plugins")
        
        self.pluginDirList = []
        self.pluginDirList = self.getPluginList()

        self.importPluginMetas(pluginsToLoad)

###############################################################################

    def getPluginList(self):
        """ Returns a list of diretories, where possible plugins a stored.
        """
        
        tmpList = []
        try:
            # test for every file listed, if it is a directory
            for x in listdir(self.pluginBaseDir):
                tmpPath = os.path.join(self.pluginBaseDir, x)
                if os.path.isdir(tmpPath):
                    tmpList.append(x)
                    
            return tmpList
        except OSError, errorData:
            errorString = "Could not read from directory where plugins are stored. Reason:\n"
            errorString += str(errorData)
            environment.logMessage(LogObject("Debug", errorString))

###############################################################################

    def importPluginMetas(self, pluginsToLoad=[]):
        """ Read the meta information for every plugin directory which is 
        found.
        
        pluginsToLoad is a list of plugins which should be loaded.
        """
        
        for x in self.pluginDirList:
            if x == "CVS":
                continue
                
            pluginMetaObject = {}
            
            try:
                pluginMetaObject = self.readMetaInfo(x, pluginsToLoad)
                self.PLUGINS[pluginMetaObject["pluginName"]] = pluginMetaObject
            except PluginMetaError, x:
                errorString = "Plugin from the following directory could not be loaded:\n"
                errorString = errorString + str(x)
                environment.logMessage(LogObject("Error", errorString))

###############################################################################

    def readMetaInfo(self, pluginPath, pluginsToLoad):
        """ Read the meta information for a plugin given by its directory.
        
        If the plugin is in pluginsToLoad, the flag for using this plugin will 
        be set.
        """
        
        attributeList = ["lumaPlugin", "pluginName", "author",
                            "pluginUserString", "version", "getIcon", 
                            "getPluginWidget", "getPluginSettingsWidget"]
        metaInformation = {}
        
        importedModule = None
        try:
            modulePath = os.path.join("plugins", pluginPath)
            foundModule = imp.find_module(modulePath)
            importedModule = imp.load_module(pluginPath, *foundModule)
        except ImportError, errorData:
            errorString = "Plugin meta information could not be loaded. Reason:\n"
            errorString += str(errorData)
            environment.logMessage(LogObject("Error", errorString))
            raise PluginMetaError, errorData
            
        missingAttributes = []
        for x in attributeList:
            if not hasattr(importedModule, x):
                missingAttributes.append(x)
        
        if len(missingAttributes) > 0:
            errorString = "Loaded module " + pluginPath + " is not a Luma plugin."
            errorString = errorString + "The following attributes are missing: \n"
            for x in missingAttributes:
                errorString = errorString + x + " "
            raise PluginMetaError, errorString
        
        metaInformation["pluginName"] = importedModule.pluginName
        metaInformation["pluginUserString"] = importedModule.pluginUserString
        metaInformation["author"] = importedModule.author
        metaInformation["version"] = importedModule.version
        metaInformation["getPluginWidget"] = importedModule.getPluginWidget
        metaInformation["getPluginSettingsWidget"] = importedModule.getPluginSettingsWidget
        
        iconPath = os.path.join(environment.lumaInstallationPrefix, "share", "luma", "icons", "plugins", pluginPath)
        icon = importedModule.getIcon(iconPath)
        metaInformation["icon"] = icon
        
        metaInformation["load"] = False
        if pluginsToLoad == 'ALL':
            metaInformation["load"] = True
        else:
            for x in pluginsToLoad:
                if x == metaInformation["pluginName"]:
                    metaInformation["load"] = True
                    break
        
        return metaInformation

###############################################################################

class PluginMetaError(Exception): 
    """ Custom exception class. """
    
    pass 
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.