Plugins/CheckerPlugins/SyntaxChecker/SyntaxCheckService.py

branch
Py2 comp.
changeset 3456
96232974dcdb
parent 3418
27ab90e0f25e
child 3525
66f4b8646622
equal deleted inserted replaced
3178:f25fc1364c88 3456:96232974dcdb
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 @signal syntaxChecked(str, dict) emited when the syntax check was done.
29 """
30 syntaxChecked = pyqtSignal(str, dict)
31
32 def __init__(self):
33 """
34 Contructor of SyntaxCheckService.
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 @param filename of the sourcefile (str)
45 @param source code of the file (str)
46 @return language of the file or None if not found (str or None)
47 """
48 pyVer = determinePythonVersion(filename, source)
49 if pyVer:
50 return 'Python{0}'.format(pyVer)
51
52 for lang, (getArgs, getExt) in self.__supportedLanguages.items():
53 if filename.endswith(tuple(getExt())):
54 return lang
55
56 return None
57
58 def addLanguage(
59 self, lang, path, module, getArgs, getExt, callback, onError):
60 """
61 Register the new language to the supported languages.
62
63 @param lang new language to check syntax (str)
64 @param path full path to the module (str)
65 @param module name to import (str)
66 @param getArgs function to collect the required arguments to call the
67 syntax checker on client side (function)
68 @param getExt function that returns the supported file extensions of
69 the syntax checker (function)
70 @param callback function on service response (function)
71 @param onError callback function if client or service isn't available
72 (function)
73 """
74 self.__supportedLanguages[lang] = getArgs, getExt
75 # Connect to the background service
76 self.backgroundService.serviceConnect(
77 'syntax', lang, path, module, callback, onError)
78
79 def getLanguages(self):
80 """
81 Return the supported language names.
82
83 @return list of languanges supported (list of str)
84 """
85 return list(self.__supportedLanguages.keys())
86
87 def removeLanguage(self, lang):
88 """
89 Remove the language from syntax check.
90
91 @param lang language to remove (str)
92 """
93 self.__supportedLanguages.pop(lang, None)
94 self.backgroundService.serviceDisconnect('syntax', lang)
95
96 def getExtensions(self):
97 """
98 Return all supported file extensions for the syntax checker dialog.
99
100 @return set of all supported file extensions (set of str)
101 """
102 extensions = set()
103 for getArgs, getExt in self.__supportedLanguages.values():
104 for ext in getExt():
105 extensions.add(ext)
106 return extensions
107
108 def syntaxCheck(self, lang, filename, source):
109 """
110 Method to prepare to compile one Python source file to Python bytecode
111 and to perform a pyflakes check in another task.
112
113 @param lang language of the file or None to determine by internal
114 algorithm (str or None)
115 @param filename source filename (string)
116 @param source string containing the code to check (string)
117 """
118 if not lang:
119 lang = self.__determineLanguage(filename, source)
120 if lang not in self.getLanguages():
121 return
122 data = [source]
123 # Call the getArgs function to get the required arguments
124 args = self.__supportedLanguages[lang][0]()
125 data.extend(args)
126 self.backgroundService.enqueueRequest('syntax', lang, filename, data)

eric ide

mercurial