extensionservice.py

来自「Wxpython Implemented on Windows CE, Sou」· Python 代码 · 共 445 行 · 第 1/2 页

PY
445
字号
#----------------------------------------------------------------------------
# Name:         ExtensionService.py
# Purpose:      Extension Service for IDE
#
# Author:       Peter Yared
#
# Created:      5/23/05
# CVS-ID:       $ID:$
# Copyright:    (c) 2005-2006 ActiveGrid, Inc.
# License:      wxWindows License
#----------------------------------------------------------------------------

import wx
import wx.lib.pydocview
import MessageService
import ProjectEditor
import os
import os.path
import activegrid.util.xmlutils as xmlutils
_ = wx.GetTranslation


#----------------------------------------------------------------------------
# Constants
#----------------------------------------------------------------------------
SPACE = 10
HALF_SPACE = 5


#----------------------------------------------------------------------------
# Classes
#----------------------------------------------------------------------------

class Extension:


    def __init__(self, menuItemName=None):
        self.menuItemName = menuItemName
        self.id = 0
        self.menuItemDesc = ''
        self.command = ''
        self.commandPreArgs = ''
        self.commandPostArgs = ''
        self.fileExt = None
        self.opOnSelectedFile = True


class ExtensionService(wx.lib.pydocview.DocService):

    EXTENSIONS_KEY = "/AG_Extensions"

    def __init__(self):
        self.LoadExtensions()


    def __getExtensionKeyName(extensionName):
        return "%s/%s" % (ExtensionService.EXTENSIONS_KEY, extensionName)


    __getExtensionKeyName = staticmethod(__getExtensionKeyName)


    def LoadExtensions(self):
        self._extensions = []

        extensionNames = []
        config = wx.ConfigBase_Get()
        path = config.GetPath()
        try:
            config.SetPath(ExtensionService.EXTENSIONS_KEY)
            cont, value, index = config.GetFirstEntry()
            while cont:
                extensionNames.append(value)
                cont, value, index = config.GetNextEntry(index)
        finally:
            config.SetPath(path)

        for extensionName in extensionNames:
            extensionData = config.Read(self.__getExtensionKeyName(extensionName))
            if extensionData:
                extension = xmlutils.unmarshal(extensionData.encode('utf-8'))
                self._extensions.append(extension)


    def SaveExtensions(self):
        config = wx.ConfigBase_Get()
        config.DeleteGroup(ExtensionService.EXTENSIONS_KEY)
        for extension in self._extensions:
            config.Write(self.__getExtensionKeyName(extension.menuItemName), xmlutils.marshal(extension))


    def GetExtensions(self):
        return self._extensions


    def SetExtensions(self, extensions):
        self._extensions = extensions


    def CheckSumExtensions(self):
        return xmlutils.marshal(self._extensions)


    def InstallControls(self, frame, menuBar = None, toolBar = None, statusBar = None, document = None):
        toolsMenuIndex = menuBar.FindMenu(_("&Tools"))
        if toolsMenuIndex > -1:
            toolsMenu = menuBar.GetMenu(toolsMenuIndex)
        else:
            toolsMenu = wx.Menu()

        if self._extensions:
            if toolsMenu.GetMenuItems():
                toolsMenu.AppendSeparator()
            for ext in self._extensions:
                # Append a tool menu item for each extension
                ext.id = wx.NewId()
                toolsMenu.Append(ext.id, ext.menuItemName)
                wx.EVT_MENU(frame, ext.id, frame.ProcessEvent)
                wx.EVT_UPDATE_UI(frame, ext.id, frame.ProcessUpdateUIEvent)

        if toolsMenuIndex == -1:
            index = menuBar.FindMenu(_("&Run"))
            if index == -1:
                index = menuBar.FindMenu(_("&Project"))
            if index == -1:
                index = menuBar.FindMenu(_("&Format"))
            if index == -1:
                index = menuBar.FindMenu(_("&View"))
            menuBar.Insert(index + 1, toolsMenu, _("&Tools"))


    def ProcessEvent(self, event):
        id = event.GetId()
        for extension in self._extensions:
            if id == extension.id:
                self.OnExecuteExtension(extension)
                return True
        return False


    def ProcessUpdateUIEvent(self, event):
        id = event.GetId()
        for extension in self._extensions:
            if id == extension.id:
                if extension.fileExt:
                    doc = wx.GetApp().GetDocumentManager().GetCurrentDocument()
                    if doc and '*' in extension.fileExt:
                        event.Enable(True)
                        return True
                    if doc:
                        for fileExt in extension.fileExt:
                            if fileExt in doc.GetDocumentTemplate().GetFileFilter():
                                event.Enable(True)
                                return True
                        if extension.opOnSelectedFile and isinstance(doc, ProjectEditor.ProjectDocument):
                            filename = doc.GetFirstView().GetSelectedFile()
                            if filename:
                                template = wx.GetApp().GetDocumentManager().FindTemplateForPath(filename)
                                for fileExt in extension.fileExt:
                                    if fileExt in template.GetFileFilter():
                                        event.Enable(True)
                                        return True
                    event.Enable(False)
                    return False
        return False


    def OnExecuteExtension(self, extension):
        if extension.fileExt:
            doc = wx.GetApp().GetDocumentManager().GetCurrentDocument()
            if not doc:
                return
            if extension.opOnSelectedFile and isinstance(doc, ProjectEditor.ProjectDocument):
                filename = doc.GetFirstView().GetSelectedFile()
                if not filename:
                    filename = doc.GetFilename()
            else:
                filename = doc.GetFilename()
            ext = os.path.splitext(filename)[1]
            if not '*' in extension.fileExt:
                if not ext or ext[1:] not in extension.fileExt:
                    return
            cmds = [extension.command]
            if extension.commandPreArgs:
                cmds.append(extension.commandPreArgs)
            cmds.append(filename)
            if extension.commandPostArgs:
                cmds.append(extension.commandPostArgs)
            os.spawnv(os.P_NOWAIT, extension.command, cmds)

        else:
            cmd = extension.command
            if extension.commandPreArgs:
                cmd = cmd + ' ' + extension.commandPreArgs
            if extension.commandPostArgs:
                cmd = cmd + ' ' + extension.commandPostArgs
            f = os.popen(cmd)
            messageService = wx.GetApp().GetService(MessageService.MessageService)
            messageService.ShowWindow()
            view = messageService.GetView()
            for line in f.readlines():
                view.AddLines(line)
            view.GetControl().EnsureCaretVisible()
            f.close()


class ExtensionOptionsPanel(wx.Panel):


    def __init__(self, parent, id):
        wx.Panel.__init__(self, parent, id)

        extOptionsPanelBorderSizer = wx.BoxSizer(wx.VERTICAL)

        extOptionsPanelSizer = wx.BoxSizer(wx.HORIZONTAL)

        extCtrlSizer = wx.BoxSizer(wx.VERTICAL)
        extCtrlSizer.Add(wx.StaticText(self, -1, _("External Tools:")), 0, wx.BOTTOM, HALF_SPACE)
        self._extListBox = wx.ListBox(self, -1, style=wx.LB_SINGLE)
        self.Bind(wx.EVT_LISTBOX, self.OnListBoxSelect, self._extListBox)
        extCtrlSizer.Add(self._extListBox, 1, wx.BOTTOM | wx.EXPAND, SPACE)
        buttonSizer = wx.GridSizer(cols=2, vgap=HALF_SPACE, hgap=HALF_SPACE)
        self._moveUpButton = wx.Button(self, -1, _("Move Up"))

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?