|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2004 - 2019 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the Python re wizard dialog. |
|
8 """ |
|
9 |
|
10 from __future__ import unicode_literals |
|
11 |
|
12 import os |
|
13 import re |
|
14 |
|
15 from PyQt5.QtCore import QFileInfo, pyqtSlot |
|
16 from PyQt5.QtGui import QClipboard, QTextCursor |
|
17 from PyQt5.QtWidgets import QWidget, QDialog, QInputDialog, QApplication, \ |
|
18 QDialogButtonBox, QVBoxLayout, QTableWidgetItem |
|
19 |
|
20 from E5Gui import E5MessageBox, E5FileDialog |
|
21 from E5Gui.E5MainWindow import E5MainWindow |
|
22 |
|
23 from .Ui_PyRegExpWizardDialog import Ui_PyRegExpWizardDialog |
|
24 |
|
25 import UI.PixmapCache |
|
26 |
|
27 import Utilities |
|
28 import Preferences |
|
29 |
|
30 |
|
31 class PyRegExpWizardWidget(QWidget, Ui_PyRegExpWizardDialog): |
|
32 """ |
|
33 Class implementing the Python re wizard dialog. |
|
34 """ |
|
35 def __init__(self, parent=None, fromEric=True): |
|
36 """ |
|
37 Constructor |
|
38 |
|
39 @param parent parent widget (QWidget) |
|
40 @param fromEric flag indicating a call from within eric6 |
|
41 """ |
|
42 super(PyRegExpWizardWidget, self).__init__(parent) |
|
43 self.setupUi(self) |
|
44 |
|
45 # initialize icons of the tool buttons |
|
46 self.commentButton.setIcon(UI.PixmapCache.getIcon("comment.png")) |
|
47 self.charButton.setIcon(UI.PixmapCache.getIcon("characters.png")) |
|
48 self.anycharButton.setIcon(UI.PixmapCache.getIcon("anychar.png")) |
|
49 self.repeatButton.setIcon(UI.PixmapCache.getIcon("repeat.png")) |
|
50 self.nonGroupButton.setIcon(UI.PixmapCache.getIcon("nongroup.png")) |
|
51 self.groupButton.setIcon(UI.PixmapCache.getIcon("group.png")) |
|
52 self.namedGroupButton.setIcon(UI.PixmapCache.getIcon("namedgroup.png")) |
|
53 self.namedReferenceButton.setIcon( |
|
54 UI.PixmapCache.getIcon("namedreference.png")) |
|
55 self.altnButton.setIcon(UI.PixmapCache.getIcon("altn.png")) |
|
56 self.beglineButton.setIcon(UI.PixmapCache.getIcon("begline.png")) |
|
57 self.endlineButton.setIcon(UI.PixmapCache.getIcon("endline.png")) |
|
58 self.wordboundButton.setIcon( |
|
59 UI.PixmapCache.getIcon("wordboundary.png")) |
|
60 self.nonwordboundButton.setIcon( |
|
61 UI.PixmapCache.getIcon("nonwordboundary.png")) |
|
62 self.poslookaheadButton.setIcon( |
|
63 UI.PixmapCache.getIcon("poslookahead.png")) |
|
64 self.neglookaheadButton.setIcon( |
|
65 UI.PixmapCache.getIcon("neglookahead.png")) |
|
66 self.poslookbehindButton.setIcon( |
|
67 UI.PixmapCache.getIcon("poslookbehind.png")) |
|
68 self.neglookbehindButton.setIcon( |
|
69 UI.PixmapCache.getIcon("neglookbehind.png")) |
|
70 self.undoButton.setIcon(UI.PixmapCache.getIcon("editUndo.png")) |
|
71 self.redoButton.setIcon(UI.PixmapCache.getIcon("editRedo.png")) |
|
72 |
|
73 self.namedGroups = re.compile(r"""\(?P<([^>]+)>""").findall |
|
74 |
|
75 self.saveButton = self.buttonBox.addButton( |
|
76 self.tr("Save"), QDialogButtonBox.ActionRole) |
|
77 self.saveButton.setToolTip( |
|
78 self.tr("Save the regular expression to a file")) |
|
79 self.loadButton = self.buttonBox.addButton( |
|
80 self.tr("Load"), QDialogButtonBox.ActionRole) |
|
81 self.loadButton.setToolTip( |
|
82 self.tr("Load a regular expression from a file")) |
|
83 self.validateButton = self.buttonBox.addButton( |
|
84 self.tr("Validate"), QDialogButtonBox.ActionRole) |
|
85 self.validateButton.setToolTip( |
|
86 self.tr("Validate the regular expression")) |
|
87 self.executeButton = self.buttonBox.addButton( |
|
88 self.tr("Execute"), QDialogButtonBox.ActionRole) |
|
89 self.executeButton.setToolTip( |
|
90 self.tr("Execute the regular expression")) |
|
91 self.nextButton = self.buttonBox.addButton( |
|
92 self.tr("Next match"), QDialogButtonBox.ActionRole) |
|
93 self.nextButton.setToolTip( |
|
94 self.tr("Show the next match of the regular expression")) |
|
95 self.nextButton.setEnabled(False) |
|
96 |
|
97 if fromEric: |
|
98 self.buttonBox.setStandardButtons( |
|
99 QDialogButtonBox.Cancel | QDialogButtonBox.Ok) |
|
100 self.copyButton = None |
|
101 else: |
|
102 self.copyButton = self.buttonBox.addButton( |
|
103 self.tr("Copy"), QDialogButtonBox.ActionRole) |
|
104 self.copyButton.setToolTip( |
|
105 self.tr("Copy the regular expression to the clipboard")) |
|
106 self.buttonBox.setStandardButtons(QDialogButtonBox.Close) |
|
107 self.variableLabel.hide() |
|
108 self.variableLineEdit.hide() |
|
109 self.variableLine.hide() |
|
110 self.importCheckBox.hide() |
|
111 self.regexpTextEdit.setFocus() |
|
112 |
|
113 def __insertString(self, s, steps=0): |
|
114 """ |
|
115 Private method to insert a string into line edit and move cursor. |
|
116 |
|
117 @param s string to be inserted into the regexp line edit |
|
118 (string) |
|
119 @param steps number of characters to move the cursor (integer). |
|
120 Negative steps moves cursor back, positives forward. |
|
121 """ |
|
122 self.regexpTextEdit.insertPlainText(s) |
|
123 tc = self.regexpTextEdit.textCursor() |
|
124 if steps != 0: |
|
125 if steps < 0: |
|
126 act = QTextCursor.Left |
|
127 steps = abs(steps) |
|
128 else: |
|
129 act = QTextCursor.Right |
|
130 for _ in range(steps): |
|
131 tc.movePosition(act) |
|
132 self.regexpTextEdit.setTextCursor(tc) |
|
133 |
|
134 @pyqtSlot() |
|
135 def on_commentButton_clicked(self): |
|
136 """ |
|
137 Private slot to handle the comment toolbutton. |
|
138 """ |
|
139 self.__insertString("(?#)", -1) |
|
140 |
|
141 @pyqtSlot() |
|
142 def on_anycharButton_clicked(self): |
|
143 """ |
|
144 Private slot to handle the any character toolbutton. |
|
145 """ |
|
146 self.__insertString(".") |
|
147 |
|
148 @pyqtSlot() |
|
149 def on_nonGroupButton_clicked(self): |
|
150 """ |
|
151 Private slot to handle the non group toolbutton. |
|
152 """ |
|
153 self.__insertString("(?:)", -1) |
|
154 |
|
155 @pyqtSlot() |
|
156 def on_groupButton_clicked(self): |
|
157 """ |
|
158 Private slot to handle the group toolbutton. |
|
159 """ |
|
160 self.__insertString("()", -1) |
|
161 |
|
162 @pyqtSlot() |
|
163 def on_namedGroupButton_clicked(self): |
|
164 """ |
|
165 Private slot to handle the named group toolbutton. |
|
166 """ |
|
167 self.__insertString("(?P<>)", -2) |
|
168 |
|
169 @pyqtSlot() |
|
170 def on_namedReferenceButton_clicked(self): |
|
171 """ |
|
172 Private slot to handle the named reference toolbutton. |
|
173 """ |
|
174 # determine cursor position as length into text |
|
175 length = self.regexpTextEdit.textCursor().position() |
|
176 |
|
177 # only present group names that occur before the |
|
178 # current cursor position |
|
179 regex = self.regexpTextEdit.toPlainText()[:length] |
|
180 names = self.namedGroups(regex) |
|
181 if not names: |
|
182 E5MessageBox.information( |
|
183 self, |
|
184 self.tr("Named reference"), |
|
185 self.tr("""No named groups have been defined yet.""")) |
|
186 return |
|
187 |
|
188 groupName, ok = QInputDialog.getItem( |
|
189 self, |
|
190 self.tr("Named reference"), |
|
191 self.tr("Select group name:"), |
|
192 names, |
|
193 0, True) |
|
194 if ok and groupName: |
|
195 self.__insertString("(?P={0})".format(groupName)) |
|
196 |
|
197 @pyqtSlot() |
|
198 def on_altnButton_clicked(self): |
|
199 """ |
|
200 Private slot to handle the alternatives toolbutton. |
|
201 """ |
|
202 self.__insertString("(|)", -2) |
|
203 |
|
204 @pyqtSlot() |
|
205 def on_beglineButton_clicked(self): |
|
206 """ |
|
207 Private slot to handle the begin line toolbutton. |
|
208 """ |
|
209 self.__insertString("^") |
|
210 |
|
211 @pyqtSlot() |
|
212 def on_endlineButton_clicked(self): |
|
213 """ |
|
214 Private slot to handle the end line toolbutton. |
|
215 """ |
|
216 self.__insertString("$") |
|
217 |
|
218 @pyqtSlot() |
|
219 def on_wordboundButton_clicked(self): |
|
220 """ |
|
221 Private slot to handle the word boundary toolbutton. |
|
222 """ |
|
223 self.__insertString("\\b") |
|
224 |
|
225 @pyqtSlot() |
|
226 def on_nonwordboundButton_clicked(self): |
|
227 """ |
|
228 Private slot to handle the non word boundary toolbutton. |
|
229 """ |
|
230 self.__insertString("\\B") |
|
231 |
|
232 @pyqtSlot() |
|
233 def on_poslookaheadButton_clicked(self): |
|
234 """ |
|
235 Private slot to handle the positive lookahead toolbutton. |
|
236 """ |
|
237 self.__insertString("(?=)", -1) |
|
238 |
|
239 @pyqtSlot() |
|
240 def on_neglookaheadButton_clicked(self): |
|
241 """ |
|
242 Private slot to handle the negative lookahead toolbutton. |
|
243 """ |
|
244 self.__insertString("(?!)", -1) |
|
245 |
|
246 @pyqtSlot() |
|
247 def on_poslookbehindButton_clicked(self): |
|
248 """ |
|
249 Private slot to handle the positive lookbehind toolbutton. |
|
250 """ |
|
251 self.__insertString("(?<=)", -1) |
|
252 |
|
253 @pyqtSlot() |
|
254 def on_neglookbehindButton_clicked(self): |
|
255 """ |
|
256 Private slot to handle the negative lookbehind toolbutton. |
|
257 """ |
|
258 self.__insertString("(?<!)", -1) |
|
259 |
|
260 @pyqtSlot() |
|
261 def on_repeatButton_clicked(self): |
|
262 """ |
|
263 Private slot to handle the repeat toolbutton. |
|
264 """ |
|
265 from .PyRegExpWizardRepeatDialog import PyRegExpWizardRepeatDialog |
|
266 dlg = PyRegExpWizardRepeatDialog(self) |
|
267 if dlg.exec_() == QDialog.Accepted: |
|
268 self.__insertString(dlg.getRepeat()) |
|
269 |
|
270 @pyqtSlot() |
|
271 def on_charButton_clicked(self): |
|
272 """ |
|
273 Private slot to handle the characters toolbutton. |
|
274 """ |
|
275 from .PyRegExpWizardCharactersDialog import \ |
|
276 PyRegExpWizardCharactersDialog |
|
277 dlg = PyRegExpWizardCharactersDialog(self) |
|
278 if dlg.exec_() == QDialog.Accepted: |
|
279 self.__insertString(dlg.getCharacters()) |
|
280 |
|
281 @pyqtSlot() |
|
282 def on_undoButton_clicked(self): |
|
283 """ |
|
284 Private slot to handle the undo action. |
|
285 """ |
|
286 self.regexpTextEdit.document().undo() |
|
287 |
|
288 @pyqtSlot() |
|
289 def on_redoButton_clicked(self): |
|
290 """ |
|
291 Private slot to handle the redo action. |
|
292 """ |
|
293 self.regexpTextEdit.document().redo() |
|
294 |
|
295 def on_buttonBox_clicked(self, button): |
|
296 """ |
|
297 Private slot called by a button of the button box clicked. |
|
298 |
|
299 @param button button that was clicked (QAbstractButton) |
|
300 """ |
|
301 if button == self.validateButton: |
|
302 self.on_validateButton_clicked() |
|
303 elif button == self.executeButton: |
|
304 self.on_executeButton_clicked() |
|
305 elif button == self.saveButton: |
|
306 self.on_saveButton_clicked() |
|
307 elif button == self.loadButton: |
|
308 self.on_loadButton_clicked() |
|
309 elif button == self.nextButton: |
|
310 self.on_nextButton_clicked() |
|
311 elif self.copyButton and button == self.copyButton: |
|
312 self.on_copyButton_clicked() |
|
313 |
|
314 @pyqtSlot() |
|
315 def on_saveButton_clicked(self): |
|
316 """ |
|
317 Private slot to save the regexp to a file. |
|
318 """ |
|
319 fname, selectedFilter = E5FileDialog.getSaveFileNameAndFilter( |
|
320 self, |
|
321 self.tr("Save regular expression"), |
|
322 "", |
|
323 self.tr("RegExp Files (*.rx);;All Files (*)"), |
|
324 None, |
|
325 E5FileDialog.Options(E5FileDialog.DontConfirmOverwrite)) |
|
326 if fname: |
|
327 ext = QFileInfo(fname).suffix() |
|
328 if not ext: |
|
329 ex = selectedFilter.split("(*")[1].split(")")[0] |
|
330 if ex: |
|
331 fname += ex |
|
332 if QFileInfo(fname).exists(): |
|
333 res = E5MessageBox.yesNo( |
|
334 self, |
|
335 self.tr("Save regular expression"), |
|
336 self.tr("<p>The file <b>{0}</b> already exists." |
|
337 " Overwrite it?</p>").format(fname), |
|
338 icon=E5MessageBox.Warning) |
|
339 if not res: |
|
340 return |
|
341 |
|
342 try: |
|
343 f = open( |
|
344 Utilities.toNativeSeparators(fname), "w", encoding="utf-8") |
|
345 f.write(self.regexpTextEdit.toPlainText()) |
|
346 f.close() |
|
347 except IOError as err: |
|
348 E5MessageBox.information( |
|
349 self, |
|
350 self.tr("Save regular expression"), |
|
351 self.tr("""<p>The regular expression could not""" |
|
352 """ be saved.</p><p>Reason: {0}</p>""") |
|
353 .format(str(err))) |
|
354 |
|
355 @pyqtSlot() |
|
356 def on_loadButton_clicked(self): |
|
357 """ |
|
358 Private slot to load a regexp from a file. |
|
359 """ |
|
360 fname = E5FileDialog.getOpenFileName( |
|
361 self, |
|
362 self.tr("Load regular expression"), |
|
363 "", |
|
364 self.tr("RegExp Files (*.rx);;All Files (*)")) |
|
365 if fname: |
|
366 try: |
|
367 f = open( |
|
368 Utilities.toNativeSeparators(fname), "r", encoding="utf-8") |
|
369 regexp = f.read() |
|
370 f.close() |
|
371 self.regexpTextEdit.setPlainText(regexp) |
|
372 except IOError as err: |
|
373 E5MessageBox.information( |
|
374 self, |
|
375 self.tr("Save regular expression"), |
|
376 self.tr("""<p>The regular expression could not""" |
|
377 """ be saved.</p><p>Reason: {0}</p>""") |
|
378 .format(str(err))) |
|
379 |
|
380 @pyqtSlot() |
|
381 def on_copyButton_clicked(self): |
|
382 """ |
|
383 Private slot to copy the regexp string into the clipboard. |
|
384 |
|
385 This slot is only available, if not called from within eric6. |
|
386 """ |
|
387 escaped = self.regexpTextEdit.toPlainText() |
|
388 if escaped: |
|
389 escaped = escaped.replace("\\", "\\\\") |
|
390 cb = QApplication.clipboard() |
|
391 cb.setText(escaped, QClipboard.Clipboard) |
|
392 if cb.supportsSelection(): |
|
393 cb.setText(escaped, QClipboard.Selection) |
|
394 |
|
395 @pyqtSlot() |
|
396 def on_validateButton_clicked(self): |
|
397 """ |
|
398 Private slot to validate the entered regexp. |
|
399 """ |
|
400 regex = self.regexpTextEdit.toPlainText() |
|
401 if regex: |
|
402 try: |
|
403 flags = 0 |
|
404 if not self.caseSensitiveCheckBox.isChecked(): |
|
405 flags |= re.IGNORECASE |
|
406 if self.multilineCheckBox.isChecked(): |
|
407 flags |= re.MULTILINE |
|
408 if self.dotallCheckBox.isChecked(): |
|
409 flags |= re.DOTALL |
|
410 if self.verboseCheckBox.isChecked(): |
|
411 flags |= re.VERBOSE |
|
412 if self.py2Button.isChecked(): |
|
413 if self.localeCheckBox.isChecked(): |
|
414 flags |= re.LOCALE |
|
415 if self.unicodeCheckBox.isChecked(): |
|
416 flags |= re.UNICODE |
|
417 else: |
|
418 if self.unicodeCheckBox.isChecked(): |
|
419 flags |= re.ASCII |
|
420 re.compile(regex, flags) |
|
421 E5MessageBox.information( |
|
422 self, |
|
423 self.tr("Validation"), |
|
424 self.tr("""The regular expression is valid.""")) |
|
425 except re.error as e: |
|
426 E5MessageBox.critical( |
|
427 self, |
|
428 self.tr("Error"), |
|
429 self.tr("""Invalid regular expression: {0}""") |
|
430 .format(str(e))) |
|
431 return |
|
432 except IndexError: |
|
433 E5MessageBox.critical( |
|
434 self, |
|
435 self.tr("Error"), |
|
436 self.tr("""Invalid regular expression: missing""" |
|
437 """ group name""")) |
|
438 return |
|
439 else: |
|
440 E5MessageBox.critical( |
|
441 self, |
|
442 self.tr("Error"), |
|
443 self.tr("""A regular expression must be given.""")) |
|
444 |
|
445 @pyqtSlot() |
|
446 def on_executeButton_clicked(self, startpos=0): |
|
447 """ |
|
448 Private slot to execute the entered regexp on the test text. |
|
449 |
|
450 This slot will execute the entered regexp on the entered test |
|
451 data and will display the result in the table part of the dialog. |
|
452 |
|
453 @param startpos starting position for the regexp matching |
|
454 """ |
|
455 regex = self.regexpTextEdit.toPlainText() |
|
456 text = self.textTextEdit.toPlainText() |
|
457 if regex and text: |
|
458 try: |
|
459 flags = 0 |
|
460 if not self.caseSensitiveCheckBox.isChecked(): |
|
461 flags |= re.IGNORECASE |
|
462 if self.multilineCheckBox.isChecked(): |
|
463 flags |= re.MULTILINE |
|
464 if self.dotallCheckBox.isChecked(): |
|
465 flags |= re.DOTALL |
|
466 if self.verboseCheckBox.isChecked(): |
|
467 flags |= re.VERBOSE |
|
468 if self.py2Button.isChecked(): |
|
469 if self.localeCheckBox.isChecked(): |
|
470 flags |= re.LOCALE |
|
471 if self.unicodeCheckBox.isChecked(): |
|
472 flags |= re.UNICODE |
|
473 else: |
|
474 if self.unicodeCheckBox.isChecked(): |
|
475 flags |= re.ASCII |
|
476 regobj = re.compile(regex, flags) |
|
477 matchobj = regobj.search(text, startpos) |
|
478 if matchobj is not None: |
|
479 captures = len(matchobj.groups()) |
|
480 if captures is None: |
|
481 captures = 0 |
|
482 else: |
|
483 captures = 0 |
|
484 row = 0 |
|
485 OFFSET = 5 |
|
486 |
|
487 self.resultTable.setColumnCount(0) |
|
488 self.resultTable.setColumnCount(3) |
|
489 self.resultTable.setRowCount(0) |
|
490 self.resultTable.setRowCount(OFFSET) |
|
491 self.resultTable.setItem( |
|
492 row, 0, QTableWidgetItem(self.tr("Regexp"))) |
|
493 self.resultTable.setItem( |
|
494 row, 1, QTableWidgetItem(regex)) |
|
495 |
|
496 if matchobj is not None: |
|
497 offset = matchobj.start() |
|
498 self.lastMatchEnd = matchobj.end() |
|
499 self.nextButton.setEnabled(True) |
|
500 row += 1 |
|
501 self.resultTable.setItem( |
|
502 row, 0, |
|
503 QTableWidgetItem(self.tr("Offset"))) |
|
504 self.resultTable.setItem( |
|
505 row, 1, |
|
506 QTableWidgetItem("{0:d}".format(matchobj.start(0)))) |
|
507 |
|
508 row += 1 |
|
509 self.resultTable.setItem( |
|
510 row, 0, |
|
511 QTableWidgetItem(self.tr("Captures"))) |
|
512 self.resultTable.setItem( |
|
513 row, 1, |
|
514 QTableWidgetItem("{0:d}".format(captures))) |
|
515 row += 1 |
|
516 self.resultTable.setItem( |
|
517 row, 1, |
|
518 QTableWidgetItem(self.tr("Text"))) |
|
519 self.resultTable.setItem( |
|
520 row, 2, |
|
521 QTableWidgetItem(self.tr("Characters"))) |
|
522 |
|
523 row += 1 |
|
524 self.resultTable.setItem( |
|
525 row, 0, |
|
526 QTableWidgetItem(self.tr("Match"))) |
|
527 self.resultTable.setItem( |
|
528 row, 1, |
|
529 QTableWidgetItem(matchobj.group(0))) |
|
530 self.resultTable.setItem( |
|
531 row, 2, |
|
532 QTableWidgetItem( |
|
533 "{0:d}".format(len(matchobj.group(0))))) |
|
534 |
|
535 for i in range(1, captures + 1): |
|
536 if matchobj.group(i) is not None: |
|
537 row += 1 |
|
538 self.resultTable.insertRow(row) |
|
539 self.resultTable.setItem( |
|
540 row, 0, |
|
541 QTableWidgetItem( |
|
542 self.tr("Capture #{0}").format(i))) |
|
543 self.resultTable.setItem( |
|
544 row, 1, QTableWidgetItem(matchobj.group(i))) |
|
545 self.resultTable.setItem( |
|
546 row, 2, QTableWidgetItem( |
|
547 "{0:d}".format(len(matchobj.group(i))))) |
|
548 |
|
549 # highlight the matched text |
|
550 tc = self.textTextEdit.textCursor() |
|
551 tc.setPosition(offset) |
|
552 tc.setPosition(self.lastMatchEnd, QTextCursor.KeepAnchor) |
|
553 self.textTextEdit.setTextCursor(tc) |
|
554 else: |
|
555 self.nextButton.setEnabled(False) |
|
556 self.resultTable.setRowCount(2) |
|
557 row += 1 |
|
558 if startpos > 0: |
|
559 self.resultTable.setItem( |
|
560 row, 0, |
|
561 QTableWidgetItem(self.tr("No more matches"))) |
|
562 else: |
|
563 self.resultTable.setItem( |
|
564 row, 0, |
|
565 QTableWidgetItem(self.tr("No matches"))) |
|
566 |
|
567 # remove the highlight |
|
568 tc = self.textTextEdit.textCursor() |
|
569 tc.setPosition(0) |
|
570 self.textTextEdit.setTextCursor(tc) |
|
571 |
|
572 self.resultTable.resizeColumnsToContents() |
|
573 self.resultTable.resizeRowsToContents() |
|
574 self.resultTable.verticalHeader().hide() |
|
575 self.resultTable.horizontalHeader().hide() |
|
576 except re.error as e: |
|
577 E5MessageBox.critical( |
|
578 self, |
|
579 self.tr("Error"), |
|
580 self.tr("""Invalid regular expression: {0}""") |
|
581 .format(str(e))) |
|
582 return |
|
583 except IndexError: |
|
584 E5MessageBox.critical( |
|
585 self, |
|
586 self.tr("Error"), |
|
587 self.tr("""Invalid regular expression: missing""" |
|
588 """ group name""")) |
|
589 return |
|
590 else: |
|
591 E5MessageBox.critical( |
|
592 self, |
|
593 self.tr("Error"), |
|
594 self.tr("""A regular expression and a text must be""" |
|
595 """ given.""")) |
|
596 |
|
597 @pyqtSlot() |
|
598 def on_nextButton_clicked(self): |
|
599 """ |
|
600 Private slot to find the next match. |
|
601 """ |
|
602 self.on_executeButton_clicked(self.lastMatchEnd) |
|
603 |
|
604 @pyqtSlot() |
|
605 def on_regexpTextEdit_textChanged(self): |
|
606 """ |
|
607 Private slot called when the regexp changes. |
|
608 """ |
|
609 self.nextButton.setEnabled(False) |
|
610 |
|
611 @pyqtSlot(bool) |
|
612 def on_py2Button_toggled(self, checked): |
|
613 """ |
|
614 Private slot called when the Python version was selected. |
|
615 |
|
616 @param checked state of the Python 2 button (boolean) |
|
617 """ |
|
618 # set the checkboxes |
|
619 self.localeCheckBox.setEnabled(checked) |
|
620 if checked: |
|
621 self.unicodeCheckBox.setText(self.tr("Unicode")) |
|
622 else: |
|
623 self.unicodeCheckBox.setText(self.tr("ASCII")) |
|
624 self.unicodeCheckBox.setChecked(not self.unicodeCheckBox.isChecked()) |
|
625 |
|
626 # clear the result table |
|
627 self.resultTable.clear() |
|
628 self.resultTable.setColumnCount(0) |
|
629 self.resultTable.setRowCount(0) |
|
630 |
|
631 # remove the highlight |
|
632 tc = self.textTextEdit.textCursor() |
|
633 tc.setPosition(0) |
|
634 self.textTextEdit.setTextCursor(tc) |
|
635 |
|
636 def getCode(self, indLevel, indString): |
|
637 """ |
|
638 Public method to get the source code. |
|
639 |
|
640 @param indLevel indentation level (int) |
|
641 @param indString string used for indentation (space or tab) (string) |
|
642 @return generated code (string) |
|
643 """ |
|
644 # calculate the indentation string |
|
645 istring = indLevel * indString |
|
646 i1string = (indLevel + 1) * indString |
|
647 estring = os.linesep + indLevel * indString |
|
648 |
|
649 # now generate the code |
|
650 reVar = self.variableLineEdit.text() |
|
651 if not reVar: |
|
652 reVar = "regexp" |
|
653 |
|
654 regexp = self.regexpTextEdit.toPlainText() |
|
655 |
|
656 flags = [] |
|
657 if not self.caseSensitiveCheckBox.isChecked(): |
|
658 flags.append('re.IGNORECASE') |
|
659 if self.multilineCheckBox.isChecked(): |
|
660 flags.append('re.MULTILINE') |
|
661 if self.dotallCheckBox.isChecked(): |
|
662 flags.append('re.DOTALL') |
|
663 if self.verboseCheckBox.isChecked(): |
|
664 flags.append('re.VERBOSE') |
|
665 if self.localeCheckBox.isChecked() and \ |
|
666 self.py2Button.isChecked(): |
|
667 flags.append('re.LOCALE') |
|
668 if self.unicodeCheckBox.isChecked(): |
|
669 if self.py2Button.isChecked(): |
|
670 flags.append('re.UNICODE') |
|
671 else: |
|
672 flags.append('re.ASCII') |
|
673 flags = " | ".join(flags) |
|
674 |
|
675 code = '' |
|
676 if self.importCheckBox.isChecked(): |
|
677 code += 'import re{0}{1}'.format(os.linesep, istring) |
|
678 code += '{0} = re.compile('.format(reVar) |
|
679 code += '{0}{1}r"""{2}"""'.format( |
|
680 os.linesep, i1string, regexp.replace('"', '\\"')) |
|
681 if flags: |
|
682 code += ',{0}{1}{2}'.format(os.linesep, i1string, flags) |
|
683 code += '){0}'.format(estring) |
|
684 return code |
|
685 |
|
686 |
|
687 class PyRegExpWizardDialog(QDialog): |
|
688 """ |
|
689 Class for the dialog variant. |
|
690 """ |
|
691 def __init__(self, parent=None, fromEric=True): |
|
692 """ |
|
693 Constructor |
|
694 |
|
695 @param parent parent widget (QWidget) |
|
696 @param fromEric flag indicating a call from within eric6 |
|
697 """ |
|
698 super(PyRegExpWizardDialog, self).__init__(parent) |
|
699 self.setModal(fromEric) |
|
700 self.setSizeGripEnabled(True) |
|
701 |
|
702 self.__layout = QVBoxLayout(self) |
|
703 self.__layout.setContentsMargins(0, 0, 0, 0) |
|
704 self.setLayout(self.__layout) |
|
705 |
|
706 self.cw = PyRegExpWizardWidget(self, fromEric) |
|
707 size = self.cw.size() |
|
708 self.__layout.addWidget(self.cw) |
|
709 self.resize(size) |
|
710 self.setWindowTitle(self.cw.windowTitle()) |
|
711 |
|
712 self.cw.buttonBox.accepted.connect(self.accept) |
|
713 self.cw.buttonBox.rejected.connect(self.reject) |
|
714 |
|
715 def getCode(self, indLevel, indString): |
|
716 """ |
|
717 Public method to get the source code. |
|
718 |
|
719 @param indLevel indentation level (int) |
|
720 @param indString string used for indentation (space or tab) (string) |
|
721 @return generated code (string) |
|
722 """ |
|
723 return self.cw.getCode(indLevel, indString) |
|
724 |
|
725 |
|
726 class PyRegExpWizardWindow(E5MainWindow): |
|
727 """ |
|
728 Main window class for the standalone dialog. |
|
729 """ |
|
730 def __init__(self, parent=None): |
|
731 """ |
|
732 Constructor |
|
733 |
|
734 @param parent reference to the parent widget (QWidget) |
|
735 """ |
|
736 super(PyRegExpWizardWindow, self).__init__(parent) |
|
737 self.cw = PyRegExpWizardWidget(self, fromEric=False) |
|
738 size = self.cw.size() |
|
739 self.setCentralWidget(self.cw) |
|
740 self.resize(size) |
|
741 self.setWindowTitle(self.cw.windowTitle()) |
|
742 |
|
743 self.setStyle( |
|
744 Preferences.getUI("Style"), Preferences.getUI("StyleSheet")) |
|
745 |
|
746 self.cw.buttonBox.accepted.connect(self.close) |
|
747 self.cw.buttonBox.rejected.connect(self.close) |