|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2013 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the Kivy project plug-in. |
|
8 """ |
|
9 |
|
10 import fnmatch |
|
11 |
|
12 from PyQt4.QtCore import QObject |
|
13 |
|
14 from E5Gui.E5Application import e5App |
|
15 |
|
16 import Preferences |
|
17 |
|
18 from pygments.lexers._mapping import LEXERS |
|
19 |
|
20 # Start-of-Header |
|
21 name = "Kivy Project Plugin" |
|
22 author = "Detlev Offenbach <detlev@die-offenbachs.de>" |
|
23 autoactivate = True |
|
24 deactivateable = True |
|
25 version = "1.0.0" |
|
26 className = "ProjectKivyPlugin" |
|
27 packageName = "ProjectKivy" |
|
28 shortDescription = "Project support for Kivy projects." |
|
29 longDescription = """This plugin implements project support for Kivy projects.""" |
|
30 needsRestart = False |
|
31 pyqtApi = 2 |
|
32 # End-of-Header |
|
33 |
|
34 |
|
35 class ProjectKivyPlugin(QObject): |
|
36 """ |
|
37 Class implementing the Kivy project plugin. |
|
38 """ |
|
39 lexerAssociations = { |
|
40 "*.kv": "Pygments|Kivy", |
|
41 "*.kivy": "Pygments|Kivy", |
|
42 } |
|
43 |
|
44 KivyLexerKey = 'KivyLexer' |
|
45 KivyLexerEntry = ( |
|
46 'ProjectKivy.KivyLexer', |
|
47 'Kivy', |
|
48 ('kivy', 'kv'), |
|
49 ('*.kv', '*.kivy'), |
|
50 ('application/x-kivy',) |
|
51 ) |
|
52 |
|
53 def __init__(self, ui): |
|
54 """ |
|
55 Constructor |
|
56 |
|
57 @param ui reference to the user interface object (UI.UserInterface) |
|
58 """ |
|
59 QObject.__init__(self, ui) |
|
60 self.__ui = ui |
|
61 self.__initialize() |
|
62 |
|
63 def __initialize(self): |
|
64 """ |
|
65 Private slot to (re)initialize the plugin. |
|
66 """ |
|
67 self.__e5project = e5App().getObject("Project") |
|
68 |
|
69 def activate(self): |
|
70 """ |
|
71 Public method to activate this plugin. |
|
72 |
|
73 @return tuple of None and activation status (boolean) |
|
74 """ |
|
75 self.__e5project.registerProjectType("Kivy", self.trUtf8("Kivy"), |
|
76 self.fileTypesCallback, |
|
77 lexerAssociationCallback=self.lexerAssociationCallback) |
|
78 |
|
79 from Project.ProjectBrowser import SourcesBrowserFlag, FormsBrowserFlag, \ |
|
80 TranslationsBrowserFlag, OthersBrowserFlag |
|
81 Preferences.setProjectBrowserFlagsDefault("Kivy", |
|
82 SourcesBrowserFlag | \ |
|
83 FormsBrowserFlag | \ |
|
84 TranslationsBrowserFlag | \ |
|
85 OthersBrowserFlag, |
|
86 ) |
|
87 |
|
88 LEXERS[self.KivyLexerKey] = self.KivyLexerEntry |
|
89 import QScintilla.Lexers |
|
90 QScintilla.Lexers.registerLexer( |
|
91 "Kivy", |
|
92 self.trUtf8("Kivy"), |
|
93 "dummy.kv", |
|
94 self.getLexer, |
|
95 [self.trUtf8('Kivy Files (*.kv)')], |
|
96 [self.trUtf8('Kivy Files (*.kv)')], |
|
97 ['*.kv', '*.kivy'] |
|
98 ) |
|
99 |
|
100 return None, True |
|
101 |
|
102 def deactivate(self): |
|
103 """ |
|
104 Public method to deactivate this plugin. |
|
105 """ |
|
106 self.__e5project.unregisterProjectType("Kivy") |
|
107 |
|
108 import QScintilla.Lexers |
|
109 QScintilla.Lexers.unregisterLexer("Kivy") |
|
110 if self.KivyLexerKey in LEXERS: |
|
111 del LEXERS[self.KivyLexerKey] |
|
112 |
|
113 self.__initialize() |
|
114 |
|
115 def fileTypesCallback(self): |
|
116 """ |
|
117 Public method get the filetype associations of the Kivy project type. |
|
118 |
|
119 @return dictionary with file type associations |
|
120 """ |
|
121 if self.__e5project.getProjectType() == "Kivy": |
|
122 fileTypes = { |
|
123 "*.kv": "SOURCES", |
|
124 "*.kivy": "SOURCES", |
|
125 "*.py": "SOURCES", |
|
126 } |
|
127 else: |
|
128 fileTypes = {} |
|
129 return fileTypes |
|
130 |
|
131 def lexerAssociationCallback(self, filename): |
|
132 """ |
|
133 Public method to get the lexer association of the Kivy project type for |
|
134 a file. |
|
135 |
|
136 @param filename name of the file (string) |
|
137 @return name of the lexer (string) (Pygments lexers are prefixed with 'Pygments|') |
|
138 """ |
|
139 for pattern, language in self.lexerAssociations.items(): |
|
140 if fnmatch.fnmatch(filename, pattern): |
|
141 return language |
|
142 |
|
143 return "" |
|
144 |
|
145 def getLexer(self, parent=None): |
|
146 """ |
|
147 Public method to instantiate a Pygments Kivy lexer object. |
|
148 |
|
149 @param parent reference to the parent object (QObject) |
|
150 @return reference to the instanciated lexer object (QsciLexer) |
|
151 """ |
|
152 from QScintilla.Lexers.LexerPygments import LexerPygments |
|
153 lexer = LexerPygments(parent, name="Kivy") |
|
154 if lexer.canStyle(): |
|
155 return lexer |
|
156 else: |
|
157 return None |