Plugins/PluginTabnanny.py

changeset 4237
ff8a3e769fca
parent 4021
195a471c327b
child 4503
d68dcbe1deb3
equal deleted inserted replaced
4236:8d4e498a7af8 4237:ff8a3e769fca
41 41
42 class TabnannyPlugin(QObject): 42 class TabnannyPlugin(QObject):
43 """ 43 """
44 Class implementing the Tabnanny plugin. 44 Class implementing the Tabnanny plugin.
45 45
46 @signal indentChecked(str, bool, str, str) emited when the indent 46 @signal indentChecked(str, bool, str, str) emitted when the indent
47 check was done. 47 check was done.
48 @signal batchFinished() emitted when a style check batch is done
48 """ 49 """
49 indentChecked = pyqtSignal(str, bool, str, str) 50 indentChecked = pyqtSignal(str, bool, str, str)
51 batchFinished = pyqtSignal()
50 52
51 def __init__(self, ui): 53 def __init__(self, ui):
52 """ 54 """
53 Constructor 55 Constructor
54 56
63 path = os.path.join( 65 path = os.path.join(
64 os.path.dirname(__file__), 'CheckerPlugins', 'Tabnanny') 66 os.path.dirname(__file__), 'CheckerPlugins', 'Tabnanny')
65 self.backgroundService.serviceConnect( 67 self.backgroundService.serviceConnect(
66 'indent', 'Python2', path, 'Tabnanny', 68 'indent', 'Python2', path, 'Tabnanny',
67 lambda *args: self.indentChecked.emit(*args), 69 lambda *args: self.indentChecked.emit(*args),
68 onErrorCallback=self.serviceErrorPy2) 70 onErrorCallback=self.serviceErrorPy2,
71 onBatchDone=self.batchJobDone)
69 self.backgroundService.serviceConnect( 72 self.backgroundService.serviceConnect(
70 'indent', 'Python3', path, 'Tabnanny', 73 'indent', 'Python3', path, 'Tabnanny',
71 lambda *args: self.indentChecked.emit(*args), 74 lambda *args: self.indentChecked.emit(*args),
72 onErrorCallback=self.serviceErrorPy3) 75 onErrorCallback=self.serviceErrorPy3,
76 onBatchDone=self.batchJobDone)
77
78 self.queuedBatches = []
79 self.batchesFinished = True
73 80
74 def __serviceError(self, fn, msg): 81 def __serviceError(self, fn, msg):
75 """ 82 """
76 Private slot handling service errors. 83 Private slot handling service errors.
77 84
80 """ 87 """
81 self.indentChecked.emit(fn, True, "1", msg) 88 self.indentChecked.emit(fn, True, "1", msg)
82 89
83 def serviceErrorPy2(self, fx, lang, fn, msg): 90 def serviceErrorPy2(self, fx, lang, fn, msg):
84 """ 91 """
85 Public method handling service errors for Python 2. 92 Public slot handling service errors for Python 2.
86 93
87 @param fx service name (string) 94 @param fx service name (string)
88 @param lang language (string) 95 @param lang language (string)
89 @param fn file name (string) 96 @param fn file name (string)
90 @param msg message text (string) 97 @param msg message text (string)
91 """ 98 """
92 if fx == 'indent' and lang == 'Python2': 99 if fx in ['indent', 'batch_indent'] and lang == 'Python2':
93 self.__serviceError(fn, msg) 100 if fx == 'indent':
101 self.__serviceError(fn, msg)
102 else:
103 self.__serviceError(self.tr("Python 2 batch check"), msg)
104 self.batchJobDone(fx, lang)
94 105
95 def serviceErrorPy3(self, fx, lang, fn, msg): 106 def serviceErrorPy3(self, fx, lang, fn, msg):
96 """ 107 """
97 Public method handling service errors for Python 2. 108 Public slot handling service errors for Python 2.
98 109
99 @param fx service name (string) 110 @param fx service name (string)
100 @param lang language (string) 111 @param lang language (string)
101 @param fn file name (string) 112 @param fn file name (string)
102 @param msg message text (string) 113 @param msg message text (string)
103 """ 114 """
104 if fx == 'indent' and lang == 'Python3': 115 if fx in ['indent', 'batch_indent'] and lang == 'Python3':
105 self.__serviceError(fn, msg) 116 if fx == 'indent':
117 self.__serviceError(fn, msg)
118 else:
119 self.__serviceError(self.tr("Python 3 batch check"), msg)
120 self.batchJobDone(fx, lang)
121
122 def batchJobDone(self, fx, lang):
123 """
124 Public slot handling the completion of a batch job.
125
126 @param fx service name (string)
127 @param lang language (string)
128 """
129 if fx in ['indent', 'batch_indent']:
130 if lang in self.queuedBatches:
131 self.queuedBatches.remove(lang)
132 # prevent sending the signal multiple times
133 if len(self.queuedBatches) == 0 and not self.batchesFinished:
134 self.batchFinished.emit()
135 self.batchesFinished = True
106 136
107 def __initialize(self): 137 def __initialize(self):
108 """ 138 """
109 Private slot to (re)initialize the plugin. 139 Private slot to (re)initialize the plugin.
110 """ 140 """
119 self.__editorAct = None 149 self.__editorAct = None
120 self.__editorTabnannyDialog = None 150 self.__editorTabnannyDialog = None
121 151
122 def indentCheck(self, lang, filename, source): 152 def indentCheck(self, lang, filename, source):
123 """ 153 """
124 Public method to prepare a style check on one Python source file. 154 Public method to prepare an indentation check on one Python source
155 file.
125 156
126 @param lang language of the file or None to determine by internal 157 @param lang language of the file or None to determine by internal
127 algorithm (str or None) 158 algorithm (str or None)
128 @param filename source filename (string) 159 @param filename source filename (string)
129 @param source string containing the code to check (string) 160 @param source string containing the code to check (string)
134 return 165 return
135 166
136 self.backgroundService.enqueueRequest( 167 self.backgroundService.enqueueRequest(
137 'indent', lang, filename, [source]) 168 'indent', lang, filename, [source])
138 169
170 def indentBatchCheck(self, argumentsList):
171 """
172 Public method to prepare an indentation check on multiple Python
173 source files.
174
175 @param argumentsList list of arguments tuples with each tuple
176 containing filename and source (string, string)
177 """
178 data = {
179 "Python2": [],
180 "Python3": [],
181 }
182 for filename, source in argumentsList:
183 lang = 'Python{0}'.format(determinePythonVersion(filename, source))
184 if lang not in ['Python2', 'Python3']:
185 continue
186 else:
187 data[lang].append((filename, source))
188
189 self.queuedBatches = []
190 for lang in ['Python2', 'Python3']:
191 if data[lang]:
192 self.queuedBatches.append(lang)
193 self.backgroundService.enqueueRequest('batch_indent', lang, "",
194 data[lang])
195 self.batchesFinished = False
196
197 def cancelIndentBatchCheck(self):
198 """
199 Public method to cancel all batch jobs.
200 """
201 for lang in ['Python2', 'Python3']:
202 self.backgroundService.requestCancel('batch_style', lang)
203
139 def activate(self): 204 def activate(self):
140 """ 205 """
141 Public method to activate this plugin. 206 Public method to activate this plugin.
142 207
143 @return tuple of None and activation status (boolean) 208 @return tuple of None and activation status (boolean)

eric ide

mercurial