|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2013 - 2014 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 # pylint: disable=C0103 |
|
6 |
|
7 """ |
|
8 Module implementing an interface to add different languages to do a syntax |
|
9 check. |
|
10 """ |
|
11 |
|
12 from __future__ import unicode_literals |
|
13 |
|
14 from PyQt4.QtCore import QObject, pyqtSignal |
|
15 |
|
16 from E5Gui.E5Application import e5App |
|
17 from Utilities import determinePythonVersion |
|
18 |
|
19 |
|
20 class SyntaxCheckService(QObject): |
|
21 """ |
|
22 Implement the syntax check service. |
|
23 |
|
24 Plugins can add other languages to the syntax check by calling addLanguage |
|
25 and support of an extra checker module on the client side which has to |
|
26 connect directly to the background service. |
|
27 """ |
|
28 syntaxChecked = pyqtSignal(str, bool, int, int, str, str, list) |
|
29 |
|
30 def __init__(self): |
|
31 """ |
|
32 Contructor of SyntaxCheckService. |
|
33 |
|
34 @param backgroundService to connect to (BackgroundService class) |
|
35 """ |
|
36 super(SyntaxCheckService, self).__init__() |
|
37 self.backgroundService = e5App().getObject("BackgroundService") |
|
38 self.__supportedLanguages = {} |
|
39 |
|
40 def __determineLanguage(self, filename, source): |
|
41 """ |
|
42 Private methode to determine the language of the file. |
|
43 |
|
44 @return language of the file or None if not found (str or None) |
|
45 """ |
|
46 pyVer = determinePythonVersion(filename, source) |
|
47 if pyVer: |
|
48 return 'Python{0}'.format(pyVer) |
|
49 |
|
50 for lang, (getArgs, getExt) in self.__supportedLanguages.items(): |
|
51 if filename.endswith(getExt()): |
|
52 return lang |
|
53 |
|
54 return None |
|
55 |
|
56 def addLanguage( |
|
57 self, lang, path, module, getArgs, getExt, callback, onError): |
|
58 """ |
|
59 Register the new language to the supported languages. |
|
60 |
|
61 @param lang new language to check syntax (str) |
|
62 @param path full path to the module (str) |
|
63 @param module name to import (str) |
|
64 @param getArgs function to collect the required arguments to call the |
|
65 syntax checker on client side (function) |
|
66 @param getExt function that returns the supported file extensions of |
|
67 the syntax checker (function) |
|
68 @param callback function on service response (function) |
|
69 @param onError callback function if client or service isn't available |
|
70 (function) |
|
71 """ |
|
72 self.__supportedLanguages[lang] = getArgs, getExt |
|
73 # Connect to the background service |
|
74 self.backgroundService.serviceConnect( |
|
75 'syntax', lang, path, module, callback, onError) |
|
76 |
|
77 def getLanguages(self): |
|
78 """ |
|
79 Return the supported language names. |
|
80 |
|
81 @return list of languanges supported (list of str) |
|
82 """ |
|
83 return list(self.__supportedLanguages.keys()) |
|
84 |
|
85 def removeLanguage(self, lang): |
|
86 """ |
|
87 Remove the language from syntax check. |
|
88 |
|
89 @param lang language to remove (str) |
|
90 """ |
|
91 self.__supportedLanguages.pop(lang, None) |
|
92 self.backgroundService.serviceDisconnect('syntax', lang) |
|
93 |
|
94 def getExtensions(self): |
|
95 """ |
|
96 Return all supported file extensions for the syntax checker dialog. |
|
97 |
|
98 @return set of all supported file extensions (set of str) |
|
99 """ |
|
100 extensions = set() |
|
101 for getArgs, getExt in self.__supportedLanguages.values(): |
|
102 for ext in getExt(): |
|
103 extensions.add(ext) |
|
104 return extensions |
|
105 |
|
106 def syntaxCheck(self, lang, filename, source=""): |
|
107 """ |
|
108 Method to prepare to compile one Python source file to Python bytecode |
|
109 and to perform a pyflakes check in another task. |
|
110 |
|
111 @param lang language of the file or None to determine by internal |
|
112 algorithm (str or None) |
|
113 @param filename source filename (string) |
|
114 @keyparam source string containing the code to check (string) |
|
115 """ |
|
116 if not lang: |
|
117 lang = self.__determineLanguage(filename, source) |
|
118 if lang not in self.getLanguages(): |
|
119 return |
|
120 data = [source] |
|
121 # Call the getArgs function to get the required arguments |
|
122 args = self.__supportedLanguages[lang][0]() |
|
123 data.extend(args) |
|
124 self.backgroundService.enqueueRequest('syntax', lang, filename, data) |