Utilities/InternalServices.py

branch
BgService
changeset 3173
1fb284abe46e
child 3177
5af61402d74d
equal deleted inserted replaced
3172:c0f78e9d0971 3173:1fb284abe46e
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 a Qt free version of a background client for the various
9 checkers and other python interpreter dependent functions.
10 """
11
12 from __future__ import unicode_literals
13
14 import os
15
16 from PyQt4.QtCore import QObject, pyqtSignal
17 from PyQt4.QtGui import QApplication
18
19 from eric5config import getConfig
20 from Utilities import determinePythonVersion
21
22
23 class InternalServices(QObject):
24 """
25 Implement the standard services (syntax with flakes and the style check).
26 """
27 syntaxChecked = pyqtSignal(str, bool, str, int, int, str, str, list)
28 #styleChecked = pyqtSignal(TBD)
29 #indentChecked = pyqtSignal(TBD)
30
31 def __init__(self, backgroundService):
32 """
33 Contructor of InternalServices.
34
35 @param backgroundService to connect to
36 """
37 super(InternalServices, self).__init__()
38 self.backgroundService = backgroundService
39
40 path = os.path.join(
41 getConfig('ericDir'), 'Plugins', 'CheckerPlugins', 'SyntaxChecker')
42 self.backgroundService.serviceConnect(
43 'syntax', path, 'SyntaxCheck',
44 self.__translateSyntaxCheck,
45 lambda fx, fn, ver, msg: self.syntaxChecked.emit(
46 fn, True, fn, 0, 0, '', msg, []))
47
48 def syntaxCheck(self, filename, source="", checkFlakes=True,
49 ignoreStarImportWarnings=False, pyVer=None, editor=None):
50 """
51 Function to compile one Python source file to Python bytecode
52 and to perform a pyflakes check.
53
54 @param filename source filename (string)
55 @keyparam source string containing the code to check (string)
56 @keyparam checkFlakes flag indicating to do a pyflakes check (boolean)
57 @keyparam ignoreStarImportWarnings flag indicating to
58 ignore 'star import' warnings (boolean)
59 @keyparam pyVer version of the interpreter to use or None for
60 autodetect corresponding interpreter (int or None)
61 @keyparam editor if the file is opened already (Editor object)
62 """
63 if pyVer is None:
64 pyVer = determinePythonVersion(filename, source, editor)
65
66 data = [source, checkFlakes, ignoreStarImportWarnings]
67 self.backgroundService.enqueueRequest('syntax', filename, pyVer, data)
68
69 def __translateSyntaxCheck(
70 self, fn, nok, fname, line, index, code, error, warnings):
71 """
72 Slot to translate the resulting messages.
73
74 If checkFlakes is True, warnings contains a list of strings containing
75 the warnings (marker, file name, line number, message)
76 The values are only valid, if nok is False.
77
78 @param fn filename of the checked file (str)
79 @param nok flag if an error in the source was found (boolean)
80 @param fname filename of the checked file (str) # TODO: remove dubl.
81 @param line number where the error occured (int)
82 @param index the column where the error occured (int)
83 @param code the part of the code where the error occured (str)
84 @param error the name of the error (str)
85 @param warnings a list of strings containing the warnings
86 (marker, file name, line number, message)
87 """
88 for warning in warnings:
89 # Translate messages
90 msg_args = warning.pop()
91 translated = QApplication.translate(
92 'py3Flakes', warning[3]).format(*msg_args)
93 # Avoid leading "u" at Python2 unicode strings
94 if translated.startswith("u'"):
95 translated = translated[1:]
96 warning[3] = translated.replace(" u'", " '")
97
98 self.syntaxChecked.emit(
99 fn, nok, fname, line, index, code, error, warnings)

eric ide

mercurial