Plugins/WizardPlugins/PyRegExpWizard/PyRegExpWizardDialog.py

changeset 3190
a9a94491c4fd
parent 3189
9a21c547de5f
child 3345
071afe8be2a1
equal deleted inserted replaced
3189:9a21c547de5f 3190:a9a94491c4fd
68 self.redoButton.setIcon(UI.PixmapCache.getIcon("editRedo.png")) 68 self.redoButton.setIcon(UI.PixmapCache.getIcon("editRedo.png"))
69 69
70 self.namedGroups = re.compile(r"""\(?P<([^>]+)>""").findall 70 self.namedGroups = re.compile(r"""\(?P<([^>]+)>""").findall
71 71
72 self.saveButton = self.buttonBox.addButton( 72 self.saveButton = self.buttonBox.addButton(
73 self.trUtf8("Save"), QDialogButtonBox.ActionRole) 73 self.tr("Save"), QDialogButtonBox.ActionRole)
74 self.saveButton.setToolTip( 74 self.saveButton.setToolTip(
75 self.trUtf8("Save the regular expression to a file")) 75 self.tr("Save the regular expression to a file"))
76 self.loadButton = self.buttonBox.addButton( 76 self.loadButton = self.buttonBox.addButton(
77 self.trUtf8("Load"), QDialogButtonBox.ActionRole) 77 self.tr("Load"), QDialogButtonBox.ActionRole)
78 self.loadButton.setToolTip( 78 self.loadButton.setToolTip(
79 self.trUtf8("Load a regular expression from a file")) 79 self.tr("Load a regular expression from a file"))
80 self.validateButton = self.buttonBox.addButton( 80 self.validateButton = self.buttonBox.addButton(
81 self.trUtf8("Validate"), QDialogButtonBox.ActionRole) 81 self.tr("Validate"), QDialogButtonBox.ActionRole)
82 self.validateButton.setToolTip( 82 self.validateButton.setToolTip(
83 self.trUtf8("Validate the regular expression")) 83 self.tr("Validate the regular expression"))
84 self.executeButton = self.buttonBox.addButton( 84 self.executeButton = self.buttonBox.addButton(
85 self.trUtf8("Execute"), QDialogButtonBox.ActionRole) 85 self.tr("Execute"), QDialogButtonBox.ActionRole)
86 self.executeButton.setToolTip( 86 self.executeButton.setToolTip(
87 self.trUtf8("Execute the regular expression")) 87 self.tr("Execute the regular expression"))
88 self.nextButton = self.buttonBox.addButton( 88 self.nextButton = self.buttonBox.addButton(
89 self.trUtf8("Next match"), QDialogButtonBox.ActionRole) 89 self.tr("Next match"), QDialogButtonBox.ActionRole)
90 self.nextButton.setToolTip( 90 self.nextButton.setToolTip(
91 self.trUtf8("Show the next match of the regular expression")) 91 self.tr("Show the next match of the regular expression"))
92 self.nextButton.setEnabled(False) 92 self.nextButton.setEnabled(False)
93 93
94 if fromEric: 94 if fromEric:
95 self.buttonBox.setStandardButtons( 95 self.buttonBox.setStandardButtons(
96 QDialogButtonBox.Cancel | QDialogButtonBox.Ok) 96 QDialogButtonBox.Cancel | QDialogButtonBox.Ok)
97 self.copyButton = None 97 self.copyButton = None
98 else: 98 else:
99 self.copyButton = self.buttonBox.addButton( 99 self.copyButton = self.buttonBox.addButton(
100 self.trUtf8("Copy"), QDialogButtonBox.ActionRole) 100 self.tr("Copy"), QDialogButtonBox.ActionRole)
101 self.copyButton.setToolTip( 101 self.copyButton.setToolTip(
102 self.trUtf8("Copy the regular expression to the clipboard")) 102 self.tr("Copy the regular expression to the clipboard"))
103 self.buttonBox.setStandardButtons(QDialogButtonBox.Close) 103 self.buttonBox.setStandardButtons(QDialogButtonBox.Close)
104 self.variableLabel.hide() 104 self.variableLabel.hide()
105 self.variableLineEdit.hide() 105 self.variableLineEdit.hide()
106 self.variableLine.hide() 106 self.variableLine.hide()
107 self.importCheckBox.hide() 107 self.importCheckBox.hide()
176 regex = self.regexpTextEdit.toPlainText()[:length] 176 regex = self.regexpTextEdit.toPlainText()[:length]
177 names = self.namedGroups(regex) 177 names = self.namedGroups(regex)
178 if not names: 178 if not names:
179 E5MessageBox.information( 179 E5MessageBox.information(
180 self, 180 self,
181 self.trUtf8("Named reference"), 181 self.tr("Named reference"),
182 self.trUtf8("""No named groups have been defined yet.""")) 182 self.tr("""No named groups have been defined yet."""))
183 return 183 return
184 184
185 groupName, ok = QInputDialog.getItem( 185 groupName, ok = QInputDialog.getItem(
186 self, 186 self,
187 self.trUtf8("Named reference"), 187 self.tr("Named reference"),
188 self.trUtf8("Select group name:"), 188 self.tr("Select group name:"),
189 names, 189 names,
190 0, True) 190 0, True)
191 if ok and groupName: 191 if ok and groupName:
192 self.__insertString("(?P={0})".format(groupName)) 192 self.__insertString("(?P={0})".format(groupName))
193 193
313 """ 313 """
314 Private slot to save the regexp to a file. 314 Private slot to save the regexp to a file.
315 """ 315 """
316 fname, selectedFilter = E5FileDialog.getSaveFileNameAndFilter( 316 fname, selectedFilter = E5FileDialog.getSaveFileNameAndFilter(
317 self, 317 self,
318 self.trUtf8("Save regular expression"), 318 self.tr("Save regular expression"),
319 "", 319 "",
320 self.trUtf8("RegExp Files (*.rx);;All Files (*)"), 320 self.tr("RegExp Files (*.rx);;All Files (*)"),
321 None, 321 None,
322 E5FileDialog.Options(E5FileDialog.DontConfirmOverwrite)) 322 E5FileDialog.Options(E5FileDialog.DontConfirmOverwrite))
323 if fname: 323 if fname:
324 ext = QFileInfo(fname).suffix() 324 ext = QFileInfo(fname).suffix()
325 if not ext: 325 if not ext:
327 if ex: 327 if ex:
328 fname += ex 328 fname += ex
329 if QFileInfo(fname).exists(): 329 if QFileInfo(fname).exists():
330 res = E5MessageBox.yesNo( 330 res = E5MessageBox.yesNo(
331 self, 331 self,
332 self.trUtf8("Save regular expression"), 332 self.tr("Save regular expression"),
333 self.trUtf8("<p>The file <b>{0}</b> already exists." 333 self.tr("<p>The file <b>{0}</b> already exists."
334 " Overwrite it?</p>").format(fname), 334 " Overwrite it?</p>").format(fname),
335 icon=E5MessageBox.Warning) 335 icon=E5MessageBox.Warning)
336 if not res: 336 if not res:
337 return 337 return
338 338
339 try: 339 try:
342 f.write(self.regexpTextEdit.toPlainText()) 342 f.write(self.regexpTextEdit.toPlainText())
343 f.close() 343 f.close()
344 except IOError as err: 344 except IOError as err:
345 E5MessageBox.information( 345 E5MessageBox.information(
346 self, 346 self,
347 self.trUtf8("Save regular expression"), 347 self.tr("Save regular expression"),
348 self.trUtf8("""<p>The regular expression could not""" 348 self.tr("""<p>The regular expression could not"""
349 """ be saved.</p><p>Reason: {0}</p>""") 349 """ be saved.</p><p>Reason: {0}</p>""")
350 .format(str(err))) 350 .format(str(err)))
351 351
352 @pyqtSlot() 352 @pyqtSlot()
353 def on_loadButton_clicked(self): 353 def on_loadButton_clicked(self):
354 """ 354 """
355 Private slot to load a regexp from a file. 355 Private slot to load a regexp from a file.
356 """ 356 """
357 fname = E5FileDialog.getOpenFileName( 357 fname = E5FileDialog.getOpenFileName(
358 self, 358 self,
359 self.trUtf8("Load regular expression"), 359 self.tr("Load regular expression"),
360 "", 360 "",
361 self.trUtf8("RegExp Files (*.rx);;All Files (*)")) 361 self.tr("RegExp Files (*.rx);;All Files (*)"))
362 if fname: 362 if fname:
363 try: 363 try:
364 f = open( 364 f = open(
365 Utilities.toNativeSeparators(fname), "r", encoding="utf-8") 365 Utilities.toNativeSeparators(fname), "r", encoding="utf-8")
366 regexp = f.read() 366 regexp = f.read()
367 f.close() 367 f.close()
368 self.regexpTextEdit.setPlainText(regexp) 368 self.regexpTextEdit.setPlainText(regexp)
369 except IOError as err: 369 except IOError as err:
370 E5MessageBox.information( 370 E5MessageBox.information(
371 self, 371 self,
372 self.trUtf8("Save regular expression"), 372 self.tr("Save regular expression"),
373 self.trUtf8("""<p>The regular expression could not""" 373 self.tr("""<p>The regular expression could not"""
374 """ be saved.</p><p>Reason: {0}</p>""") 374 """ be saved.</p><p>Reason: {0}</p>""")
375 .format(str(err))) 375 .format(str(err)))
376 376
377 @pyqtSlot() 377 @pyqtSlot()
378 def on_copyButton_clicked(self): 378 def on_copyButton_clicked(self):
379 """ 379 """
415 if self.unicodeCheckBox.isChecked(): 415 if self.unicodeCheckBox.isChecked():
416 flags |= re.ASCII 416 flags |= re.ASCII
417 re.compile(regex, flags) 417 re.compile(regex, flags)
418 E5MessageBox.information( 418 E5MessageBox.information(
419 self, 419 self,
420 self.trUtf8("Validation"), 420 self.tr("Validation"),
421 self.trUtf8("""The regular expression is valid.""")) 421 self.tr("""The regular expression is valid."""))
422 except re.error as e: 422 except re.error as e:
423 E5MessageBox.critical( 423 E5MessageBox.critical(
424 self, 424 self,
425 self.trUtf8("Error"), 425 self.tr("Error"),
426 self.trUtf8("""Invalid regular expression: {0}""") 426 self.tr("""Invalid regular expression: {0}""")
427 .format(str(e))) 427 .format(str(e)))
428 return 428 return
429 except IndexError: 429 except IndexError:
430 E5MessageBox.critical( 430 E5MessageBox.critical(
431 self, 431 self,
432 self.trUtf8("Error"), 432 self.tr("Error"),
433 self.trUtf8("""Invalid regular expression: missing""" 433 self.tr("""Invalid regular expression: missing"""
434 """ group name""")) 434 """ group name"""))
435 return 435 return
436 else: 436 else:
437 E5MessageBox.critical( 437 E5MessageBox.critical(
438 self, 438 self,
439 self.trUtf8("Error"), 439 self.tr("Error"),
440 self.trUtf8("""A regular expression must be given.""")) 440 self.tr("""A regular expression must be given."""))
441 441
442 @pyqtSlot() 442 @pyqtSlot()
443 def on_executeButton_clicked(self, startpos=0): 443 def on_executeButton_clicked(self, startpos=0):
444 """ 444 """
445 Private slot to execute the entered regexp on the test text. 445 Private slot to execute the entered regexp on the test text.
484 self.resultTable.setColumnCount(0) 484 self.resultTable.setColumnCount(0)
485 self.resultTable.setColumnCount(3) 485 self.resultTable.setColumnCount(3)
486 self.resultTable.setRowCount(0) 486 self.resultTable.setRowCount(0)
487 self.resultTable.setRowCount(OFFSET) 487 self.resultTable.setRowCount(OFFSET)
488 self.resultTable.setItem( 488 self.resultTable.setItem(
489 row, 0, QTableWidgetItem(self.trUtf8("Regexp"))) 489 row, 0, QTableWidgetItem(self.tr("Regexp")))
490 self.resultTable.setItem( 490 self.resultTable.setItem(
491 row, 1, QTableWidgetItem(regex)) 491 row, 1, QTableWidgetItem(regex))
492 492
493 if matchobj is not None: 493 if matchobj is not None:
494 offset = matchobj.start() 494 offset = matchobj.start()
495 self.lastMatchEnd = matchobj.end() 495 self.lastMatchEnd = matchobj.end()
496 self.nextButton.setEnabled(True) 496 self.nextButton.setEnabled(True)
497 row += 1 497 row += 1
498 self.resultTable.setItem( 498 self.resultTable.setItem(
499 row, 0, 499 row, 0,
500 QTableWidgetItem(self.trUtf8("Offset"))) 500 QTableWidgetItem(self.tr("Offset")))
501 self.resultTable.setItem( 501 self.resultTable.setItem(
502 row, 1, 502 row, 1,
503 QTableWidgetItem("{0:d}".format(matchobj.start(0)))) 503 QTableWidgetItem("{0:d}".format(matchobj.start(0))))
504 504
505 row += 1 505 row += 1
506 self.resultTable.setItem( 506 self.resultTable.setItem(
507 row, 0, 507 row, 0,
508 QTableWidgetItem(self.trUtf8("Captures"))) 508 QTableWidgetItem(self.tr("Captures")))
509 self.resultTable.setItem( 509 self.resultTable.setItem(
510 row, 1, 510 row, 1,
511 QTableWidgetItem("{0:d}".format(captures))) 511 QTableWidgetItem("{0:d}".format(captures)))
512 row += 1 512 row += 1
513 self.resultTable.setItem( 513 self.resultTable.setItem(
514 row, 1, 514 row, 1,
515 QTableWidgetItem(self.trUtf8("Text"))) 515 QTableWidgetItem(self.tr("Text")))
516 self.resultTable.setItem( 516 self.resultTable.setItem(
517 row, 2, 517 row, 2,
518 QTableWidgetItem(self.trUtf8("Characters"))) 518 QTableWidgetItem(self.tr("Characters")))
519 519
520 row += 1 520 row += 1
521 self.resultTable.setItem( 521 self.resultTable.setItem(
522 row, 0, 522 row, 0,
523 QTableWidgetItem(self.trUtf8("Match"))) 523 QTableWidgetItem(self.tr("Match")))
524 self.resultTable.setItem( 524 self.resultTable.setItem(
525 row, 1, 525 row, 1,
526 QTableWidgetItem(matchobj.group(0))) 526 QTableWidgetItem(matchobj.group(0)))
527 self.resultTable.setItem( 527 self.resultTable.setItem(
528 row, 2, 528 row, 2,
534 row += 1 534 row += 1
535 self.resultTable.insertRow(row) 535 self.resultTable.insertRow(row)
536 self.resultTable.setItem( 536 self.resultTable.setItem(
537 row, 0, 537 row, 0,
538 QTableWidgetItem( 538 QTableWidgetItem(
539 self.trUtf8("Capture #{0}").format(i))) 539 self.tr("Capture #{0}").format(i)))
540 self.resultTable.setItem( 540 self.resultTable.setItem(
541 row, 1, QTableWidgetItem(matchobj.group(i))) 541 row, 1, QTableWidgetItem(matchobj.group(i)))
542 self.resultTable.setItem( 542 self.resultTable.setItem(
543 row, 2, QTableWidgetItem( 543 row, 2, QTableWidgetItem(
544 "{0:d}".format(len(matchobj.group(i))))) 544 "{0:d}".format(len(matchobj.group(i)))))
553 self.resultTable.setRowCount(2) 553 self.resultTable.setRowCount(2)
554 row += 1 554 row += 1
555 if startpos > 0: 555 if startpos > 0:
556 self.resultTable.setItem( 556 self.resultTable.setItem(
557 row, 0, 557 row, 0,
558 QTableWidgetItem(self.trUtf8("No more matches"))) 558 QTableWidgetItem(self.tr("No more matches")))
559 else: 559 else:
560 self.resultTable.setItem( 560 self.resultTable.setItem(
561 row, 0, 561 row, 0,
562 QTableWidgetItem(self.trUtf8("No matches"))) 562 QTableWidgetItem(self.tr("No matches")))
563 563
564 # remove the highlight 564 # remove the highlight
565 tc = self.textTextEdit.textCursor() 565 tc = self.textTextEdit.textCursor()
566 tc.setPosition(0) 566 tc.setPosition(0)
567 self.textTextEdit.setTextCursor(tc) 567 self.textTextEdit.setTextCursor(tc)
571 self.resultTable.verticalHeader().hide() 571 self.resultTable.verticalHeader().hide()
572 self.resultTable.horizontalHeader().hide() 572 self.resultTable.horizontalHeader().hide()
573 except re.error as e: 573 except re.error as e:
574 E5MessageBox.critical( 574 E5MessageBox.critical(
575 self, 575 self,
576 self.trUtf8("Error"), 576 self.tr("Error"),
577 self.trUtf8("""Invalid regular expression: {0}""") 577 self.tr("""Invalid regular expression: {0}""")
578 .format(str(e))) 578 .format(str(e)))
579 return 579 return
580 except IndexError: 580 except IndexError:
581 E5MessageBox.critical( 581 E5MessageBox.critical(
582 self, 582 self,
583 self.trUtf8("Error"), 583 self.tr("Error"),
584 self.trUtf8("""Invalid regular expression: missing""" 584 self.tr("""Invalid regular expression: missing"""
585 """ group name""")) 585 """ group name"""))
586 return 586 return
587 else: 587 else:
588 E5MessageBox.critical( 588 E5MessageBox.critical(
589 self, 589 self,
590 self.trUtf8("Error"), 590 self.tr("Error"),
591 self.trUtf8("""A regular expression and a text must be""" 591 self.tr("""A regular expression and a text must be"""
592 """ given.""")) 592 """ given."""))
593 593
594 @pyqtSlot() 594 @pyqtSlot()
595 def on_nextButton_clicked(self): 595 def on_nextButton_clicked(self):
596 """ 596 """
597 Private slot to find the next match. 597 Private slot to find the next match.
613 @param checked state of the Python 2 button (boolean) 613 @param checked state of the Python 2 button (boolean)
614 """ 614 """
615 # set the checkboxes 615 # set the checkboxes
616 self.localeCheckBox.setEnabled(checked) 616 self.localeCheckBox.setEnabled(checked)
617 if checked: 617 if checked:
618 self.unicodeCheckBox.setText(self.trUtf8("Unicode")) 618 self.unicodeCheckBox.setText(self.tr("Unicode"))
619 else: 619 else:
620 self.unicodeCheckBox.setText(self.trUtf8("ASCII")) 620 self.unicodeCheckBox.setText(self.tr("ASCII"))
621 self.unicodeCheckBox.setChecked(not self.unicodeCheckBox.isChecked()) 621 self.unicodeCheckBox.setChecked(not self.unicodeCheckBox.isChecked())
622 622
623 # clear the result table 623 # clear the result table
624 self.resultTable.clear() 624 self.resultTable.clear()
625 self.resultTable.setColumnCount(0) 625 self.resultTable.setColumnCount(0)

eric ide

mercurial