Commit of first functions of the Kivy project support plug-in.

Wed, 12 Jun 2013 18:50:51 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Wed, 12 Jun 2013 18:50:51 +0200
changeset 1
371cfb479eb6
parent 0
35ccc96239a0
child 2
373da5610be1

Commit of first functions of the Kivy project support plug-in.

.hgignore file | annotate | diff | comparison | revisions
PluginKivy.e4p file | annotate | diff | comparison | revisions
PluginProjectKivy file | annotate | diff | comparison | revisions
PluginProjectKivy.py file | annotate | diff | comparison | revisions
ProjectKivy/KivyLexer.py file | annotate | diff | comparison | revisions
ProjectKivy/__init__.py file | annotate | diff | comparison | revisions
--- a/.hgignore	Tue Jun 11 07:54:11 2013 +0200
+++ b/.hgignore	Wed Jun 12 18:50:51 2013 +0200
@@ -15,3 +15,4 @@
 glob:tmp
 glob:__pycache__
 glob:**.DS_Store
+glob:**Ui_*.py
--- a/PluginKivy.e4p	Tue Jun 11 07:54:11 2013 +0200
+++ b/PluginKivy.e4p	Wed Jun 12 18:50:51 2013 +0200
@@ -10,9 +10,12 @@
   <Version>0.1</Version>
   <Author>Detlev Offenbach</Author>
   <Email>detlev@die-offenbachs.de</Email>
+  <Eol index="1"/>
   <Sources>
     <Source>__init__.py</Source>
-    <Source>PluginProjectKivy</Source>
+    <Source>PluginProjectKivy.py</Source>
+    <Source>ProjectKivy/__init__.py</Source>
+    <Source>ProjectKivy/KivyLexer.py</Source>
   </Sources>
   <Forms/>
   <Translations/>
@@ -21,7 +24,7 @@
   <Others>
     <Other>.hgignore</Other>
   </Others>
-  <MainScript>PluginProjectKivy</MainScript>
+  <MainScript>PluginProjectKivy.py</MainScript>
   <Vcs>
     <VcsType>Mercurial</VcsType>
     <VcsOptions>
@@ -139,5 +142,6 @@
     <FiletypeAssociation pattern="*.ts" type="TRANSLATIONS"/>
     <FiletypeAssociation pattern="*.ui" type="FORMS"/>
     <FiletypeAssociation pattern="*.ui.h" type="FORMS"/>
+    <FiletypeAssociation pattern="Ui_*.py" type="__IGNORE__"/>
   </FiletypeAssociations>
 </Project>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PluginProjectKivy.py	Wed Jun 12 18:50:51 2013 +0200
@@ -0,0 +1,157 @@
+# -*- coding: utf-8 -*-
+
+# Copyright (c) 2013 Detlev Offenbach <detlev@die-offenbachs.de>
+#
+
+"""
+Module implementing the Kivy project plug-in.
+"""
+
+import fnmatch
+
+from PyQt4.QtCore import QObject
+
+from E5Gui.E5Application import e5App
+
+import Preferences
+
+from pygments.lexers._mapping import LEXERS
+
+# Start-of-Header
+name = "Kivy Project Plugin"
+author = "Detlev Offenbach <detlev@die-offenbachs.de>"
+autoactivate = True
+deactivateable = True
+version = "1.0.0"
+className = "ProjectKivyPlugin"
+packageName = "ProjectKivy"
+shortDescription = "Project support for Kivy projects."
+longDescription = """This plugin implements project support for Kivy projects."""
+needsRestart = False
+pyqtApi = 2
+# End-of-Header
+
+
+class ProjectKivyPlugin(QObject):
+    """
+    Class implementing the Kivy project plugin.
+    """
+    lexerAssociations = {
+        "*.kv": "Pygments|Kivy",
+        "*.kivy": "Pygments|Kivy",
+    }
+    
+    KivyLexerKey = 'KivyLexer'
+    KivyLexerEntry = (
+        'ProjectKivy.KivyLexer',
+        'Kivy',
+        ('kivy', 'kv'),
+        ('*.kv', '*.kivy'),
+        ('application/x-kivy',)
+    )
+    
+    def __init__(self, ui):
+        """
+        Constructor
+        
+        @param ui reference to the user interface object (UI.UserInterface)
+        """
+        QObject.__init__(self, ui)
+        self.__ui = ui
+        self.__initialize()
+    
+    def __initialize(self):
+        """
+        Private slot to (re)initialize the plugin.
+        """
+        self.__e5project = e5App().getObject("Project")
+    
+    def activate(self):
+        """
+        Public method to activate this plugin.
+        
+        @return tuple of None and activation status (boolean)
+        """
+        self.__e5project.registerProjectType("Kivy", self.trUtf8("Kivy"),
+            self.fileTypesCallback,
+            lexerAssociationCallback=self.lexerAssociationCallback)
+        
+        from Project.ProjectBrowser import SourcesBrowserFlag, FormsBrowserFlag, \
+            TranslationsBrowserFlag, OthersBrowserFlag
+        Preferences.setProjectBrowserFlagsDefault("Kivy",
+            SourcesBrowserFlag | \
+            FormsBrowserFlag | \
+            TranslationsBrowserFlag | \
+            OthersBrowserFlag,
+        )
+        
+        LEXERS[self.KivyLexerKey] = self.KivyLexerEntry
+        import QScintilla.Lexers
+        QScintilla.Lexers.registerLexer(
+            "Kivy", 
+            self.trUtf8("Kivy"), 
+            "dummy.kv", 
+            self.getLexer, 
+            [self.trUtf8('Kivy Files (*.kv)')],
+            [self.trUtf8('Kivy Files (*.kv)')], 
+            ['*.kv', '*.kivy']
+        )
+        
+        return None, True
+    
+    def deactivate(self):
+        """
+        Public method to deactivate this plugin.
+        """
+        self.__e5project.unregisterProjectType("Kivy")
+        
+        import QScintilla.Lexers
+        QScintilla.Lexers.unregisterLexer("Kivy")
+        if self.KivyLexerKey in LEXERS:
+            del LEXERS[self.KivyLexerKey]
+        
+        self.__initialize()
+    
+    def fileTypesCallback(self):
+        """
+        Public method get the filetype associations of the Kivy project type.
+        
+        @return dictionary with file type associations
+        """
+        if self.__e5project.getProjectType() == "Kivy":
+            fileTypes = {
+                "*.kv": "SOURCES",
+                "*.kivy": "SOURCES",
+                "*.py": "SOURCES",
+            }
+        else:
+            fileTypes = {}
+        return fileTypes
+    
+    def lexerAssociationCallback(self, filename):
+        """
+        Public method to get the lexer association of the Kivy 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 getLexer(self, parent=None):
+        """
+        Public method to instantiate a Pygments Kivy lexer object.
+        
+        @param parent reference to the parent object (QObject)
+        @return reference to the instanciated lexer object (QsciLexer)
+        """
+        from QScintilla.Lexers.LexerPygments import LexerPygments
+        lexer = LexerPygments(parent, name="Kivy")
+        if lexer.canStyle():
+            return lexer
+        else:
+            return None
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ProjectKivy/KivyLexer.py	Wed Jun 12 18:50:51 2013 +0200
@@ -0,0 +1,43 @@
+# -*- coding: utf-8 -*-
+
+# Copyright (c) 2013 Detlev Offenbach <detlev@die-offenbachs.de>
+#
+
+"""""
+Pygments lexer for the Kivy  language
+"""
+
+from pygments.lexer import RegexLexer, bygroups, using
+from pygments.lexers.agile import PythonLexer
+from pygments.token import *
+
+__all__ = ['KivyLexer']
+
+class KivyLexer(RegexLexer):
+    name = 'Kivy'
+    aliases = ['kivy', 'kv']
+    filenames = ['*.kv', '*.kivy']
+    mimetypes = ['application/x-kivy']
+    tokens = {
+        'root': [
+            (r'#:.*?$', Comment.Preproc),
+            (r'#.*?$', using(PythonLexer)),
+            (r'\s+', Text),
+            (r'<.+>', Name.Namespace),
+            (r'(\[)(\s*)(.*?)(\s*)(@)',
+                bygroups(Punctuation, Text, Name.Class, Text, Operator),
+                'classList'),
+            (r'[A-Za-z][A-Za-z0-9]*$', Name.Attribute),
+            (r'(.*?)(\s*)(:)(\s*)$',
+                bygroups(Name.Class, Text, Punctuation, Text)),
+            (r'(.*?)(\s*)(:)(\s*)(.*?)$',
+                bygroups(Name.Attribute, Text, Punctuation, Text,
+                using(PythonLexer)))],
+        'classList': [
+            (r'(,)(\s*)([A-Z][A-Za-z0-9]*)',
+                bygroups(Punctuation, Text, Name.Class)),
+            (r'(\+)(\s*)([A-Z][A-Za-z0-9]*)',
+                bygroups(Operator, Text, Name.Class)),
+            (r'\s+', Text),
+            (r'[A-Z][A-Za-z0-9]*', Name.Class),
+            (r'\]', Punctuation, '#pop')]}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ProjectKivy/__init__.py	Wed Jun 12 18:50:51 2013 +0200
@@ -0,0 +1,8 @@
+# -*- coding: utf-8 -*-
+
+# Copyright (c) 2013 Detlev Offenbach <detlev@die-offenbachs.de>
+#
+
+"""
+Package implementing project support for eric5 Kivy projects.
+"""

eric ide

mercurial