PluginProjectPyramid.py

Tue, 06 Nov 2012 17:13:58 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Tue, 06 Nov 2012 17:13:58 +0100
changeset 25
2dd3d1cf573a
parent 22
c358b356b214
child 32
107e51521b1e
permissions
-rw-r--r--

Fixed a bug related to uninstalling the plug-in via the external plug-in uninstaller.

# -*- coding: utf-8 -*-

# Copyright (c) 2012 Detlev Offenbach <detlev@die-offenbachs.de>
#

"""
Module implementing the Pyramid project plugin.
"""

import os
import glob
import fnmatch

from PyQt4.QtCore import QCoreApplication, QObject, QTranslator

from E5Gui.E5Application import e5App

import Preferences

from Globals import isWindowsPlatform, isMacPlatform

from Project.ProjectBrowser import SourcesBrowserFlag, FormsBrowserFlag, \
    TranslationsBrowserFlag, OthersBrowserFlag

from ProjectPyramid.Project import Project

# Start-of-Header
name = "Pyramid Project Plugin"
author = "Detlev Offenbach <detlev@die-offenbachs.de>"
autoactivate = True
deactivateable = True
version = "0.4.0"
className = "ProjectPyramidPlugin"
packageName = "ProjectPyramid"
shortDescription = "Project support for Pyramid projects."
longDescription = """This plugin implements project support for Pyramid projects."""
needsRestart = False
pyqtApi = 2
# End-of-Header

error = ""

pyramidPluginObject = None


def createPyramidPage(configDlg):
    """
    Module function to create the Pyramid configuration page.
    
    @return reference to the configuration page
    """
    global pyramidPluginObject
    from ProjectPyramid.ConfigurationPage.PyramidPage import PyramidPage
    page = PyramidPage(pyramidPluginObject)
    return page
    

def getConfigData():
    """
    Module function returning data as required by the configuration dialog.
    
    @return dictionary containing the relevant data
    """
    if e5App().getObject("UserInterface").versionIsNewer('5.0.99', '20120101'):
        return {
            "pyramidPage": \
                [QCoreApplication.translate("ProjectPyramidPlugin", "Pyramid"),
                 os.path.join("ProjectPyramid", "icons",
                              "pyramid.png"),
                 createPyramidPage, None, None],
        }
    else:
        return {}


def apiFiles(language):
    """
    Module function to return the API files made available by this plugin.
    
    @return list of API filenames (list of string)
    """
    if language in ["Python3",  "Python2"]:
        apisDir = \
            os.path.join(os.path.dirname(__file__), "ProjectPyramid", "APIs")
        apis = glob.glob(os.path.join(apisDir, '*.api'))
    else:
        apis = []
    return apis


def prepareUninstall():
    """
    Module function to prepare for an uninstallation.
    """
    Preferences.removeProjectBrowserFlags(ProjectPyramidPlugin.PreferencesKey)
    Preferences.Prefs.settings.remove(ProjectPyramidPlugin.PreferencesKey)


class ProjectPyramidPlugin(QObject):
    """
    Class implementing the Pyramid project plugin.
    """
    PreferencesKey = "Pyramid"
    
    lexerAssociations = {
        "*.mako": "Pygments|HTML+Mako",
        "*.pt": "Pygments|HTML+Genshi",
        "*.txt": "Pygments|Genshi",
        "*.html": "Pygments|HTML+Genshi",
        "*.htm": "Pygments|HTML+Genshi",
    }
    
    def __init__(self, ui):
        """
        Constructor
        
        @param ui reference to the user interface object (UI.UserInterface)
        """
        QObject.__init__(self, ui)
        self.__ui = ui
        self.__initialize()
        
        self.__defaults = {
            "VirtualEnvironmentPy2": "",
            "VirtualEnvironmentPy3": "",
            "Python2ConsoleType": "python",
            "Python3ConsoleType": "python",
            "PyramidDocUrl": "http://docs.pylonsproject.org/en/latest/docs/pyramid.html",
        }
        if isWindowsPlatform():
            self.__defaults["ConsoleCommand"] = "cmd.exe /c"
        elif isMacPlatform():
            self.__defaults["ConsoleCommand"] = "xterm -e"
        else:
            self.__defaults["ConsoleCommand"] = "konsole -e"
        
        self.__translator = None
        self.__loadTranslator()
    
    def __initialize(self):
        """
        Private slot to (re)initialize the plugin.
        """
        self.__object = None
        
        self.__mainAct = None
        self.__mainMenu = None
        
        self.__e5project = e5App().getObject("Project")
    
    def __checkVersions(self):
        """
        Private function to check that the eric5 version is ok.
        
        @return flag indicating version is ok (boolean)
        """
        global error
        
        if self.__ui.versionIsNewer('5.0.99', '20120101'):
            error = ""
        else:
            error = self.trUtf8("eric5 version is too old, {0}, {1} or newer needed.")\
                .format("5.1.0", "20120101")
            return False
        
        return True
    
    def activate(self):
        """
        Public method to activate this plugin.
        
        @return tuple of None and activation status (boolean)
        """
        if not self.__checkVersions():
            return None, False
        
        global pyramidPluginObject
        pyramidPluginObject = self
        
        self.__object = Project(self, self.__ui)
        self.__object.initActions()
        e5App().registerPluginObject("ProjectPyramid", self.__object)
        
        self.__mainMenu = self.__object.initMenu()
        
        try:
            self.__e5project.registerProjectType("Pyramid", self.trUtf8("Pyramid"),
                self.fileTypesCallback,
                lexerAssociationCallback=self.lexerAssociationCallback,
                binaryTranslationsCallback=self.binaryTranslationsCallback,
                progLanguages=["Python2", "Python3"])
        except TypeError:
            # for backward compatibility
            self.__e5project.registerProjectType("Pyramid", self.trUtf8("Pyramid"),
                self.fileTypesCallback,
                lexerAssociationCallback=self.lexerAssociationCallback,
                binaryTranslationsCallback=self.binaryTranslationsCallback)
        Preferences.setProjectBrowserFlagsDefault("Pyramid",
            SourcesBrowserFlag | \
            FormsBrowserFlag | \
            TranslationsBrowserFlag | \
            OthersBrowserFlag,
        )
        
        if self.__e5project.isOpen():
            self.__projectOpened()
            self.__object.projectOpenedHooks()
        
        e5App().getObject("Project").projectOpened.connect(self.__projectOpened)
        e5App().getObject("Project").projectClosed.connect(self.__projectClosed)
        e5App().getObject("Project").newProject.connect(self.__projectOpened)
        
        e5App().getObject("Project").projectOpenedHooks.connect(
            self.__object.projectOpenedHooks)
        e5App().getObject("Project").projectClosedHooks.connect(
                     self.__object.projectClosedHooks)
        e5App().getObject("Project").newProjectHooks.connect(
                     self.__object.projectOpenedHooks)
        
        return None, True
    
    def deactivate(self):
        """
        Public method to deactivate this plugin.
        """
        e5App().unregisterPluginObject("ProjectPyramid")
        
        e5App().getObject("Project").projectOpened.disconnect(self.__projectOpened)
        e5App().getObject("Project").projectClosed.disconnect(self.__projectClosed)
        e5App().getObject("Project").newProject.disconnect(self.__projectOpened)
        
        e5App().getObject("Project").projectOpenedHooks.disconnect(
            self.__object.projectOpenedHooks)
        e5App().getObject("Project").projectClosedHooks.disconnect(
            self.__object.projectClosedHooks)
        e5App().getObject("Project").newProjectHooks.disconnect(
            self.__object.projectOpenedHooks)
        
        self.__e5project.unregisterProjectType("Pyramid")
        
        self.__object.projectClosedHooks()
        self.__projectClosed()
        
        self.__initialize()
    
    def __loadTranslator(self):
        """
        Private method to load the translation file.
        """
        if self.__ui is not None:
            loc = self.__ui.getLocale()
            if loc and loc != "C":
                locale_dir = \
                    os.path.join(os.path.dirname(__file__), "ProjectPyramid", "i18n")
                translation = "pyramid_%s" % loc
                translator = QTranslator(None)
                loaded = translator.load(translation, locale_dir)
                if loaded:
                    self.__translator = translator
                    e5App().installTranslator(self.__translator)
                else:
                    print("Warning: translation file '{0}' could not be loaded.".format(
                        translation))
                    print("Using default.")
    
    def __projectOpened(self):
        """
        Private slot to handle the projectOpened signal.
        """
        if self.__e5project.getProjectType() == "Pyramid":
            projectAct = self.__ui.getMenuBarAction("project")
            actions = self.__ui.menuBar().actions()
            insertAct = actions[actions.index(projectAct) + 1]
            self.__mainAct = self.__ui.menuBar().insertMenu(insertAct, self.__mainMenu)
    
    def __projectClosed(self):
        """
        Private slot to handle the projectClosed signal.
        """
        if self.__mainAct is not None:
            self.__ui.menuBar().removeAction(self.__mainAct)
            self.__mainAct = None
            self.__object.projectClosed()
    
    def fileTypesCallback(self):
        """
        Public method get the filetype associations of the Pyramid project type.
        
        @return dictionary with file type associations
        """
        if self.__e5project.getProjectType() == "Pyramid":
            fileTypes = {
                "*.mako": "FORMS",
                "*.mak": "FORMS",
                "*.pt": "FORMS",
                "*.html": "FORMS",
                "*.htm": "FORMS",
                "*.js": "SOURCES",
                "*.pot": "TRANSLATIONS",
                "*.po": "TRANSLATIONS",
                "*.mo": "TRANSLATIONS",
            }
        else:
            fileTypes = {}
        return fileTypes
    
    def lexerAssociationCallback(self, filename):
        """
        Public method to get the lexer association of the Pyramid project type for
        a file.
        
        @param filename name of the file (string)
        @return name of the lexer (string) (Pygments lexers are prefixed with 'Pygments|')
        """
        for pattern, language in self.lexerAssociations.items():
            if fnmatch.fnmatch(filename, pattern):
                return language
        
        return ""
    
    def binaryTranslationsCallback(self, filename):
        """
        Public method to determine the filename of a compiled translation file
        given the translation source file.
        
        @param filename name of the translation source file (string)
        @return name of the binary translation file (string)
        """
        if filename.endswith(".po"):
            filename = filename.replace(".po", ".mo")
        return filename
    
    def getPreferences(self, key):
        """
        Public method to retrieve the various settings.
        
        @param key the key of the value to get
        @param prefClass preferences class used as the storage area
        @return the requested setting
        """
        return Preferences.Prefs.settings.value(self.PreferencesKey + "/" + key,
            self.__defaults[key])
    
    def setPreferences(self, key, value):
        """
        Public method to store the various settings.
        
        @param key the key of the setting to be set (string)
        @param value the value to be set
        @param prefClass preferences class used as the storage area
        """
        Preferences.Prefs.settings.setValue("Pyramid/" + key, value)

eric ide

mercurial