eric7/Plugins/WizardPlugins/QRegularExpressionWizard/QRegularExpressionWizardDialog.py

branch
eric7
changeset 9152
8a68afaf1ba2
parent 9016
6f079c524e99
child 9153
506e35e424d5
equal deleted inserted replaced
9151:8c5296fe3056 9152:8a68afaf1ba2
6 """ 6 """
7 Module implementing the QRegularExpression wizard dialog. 7 Module implementing the QRegularExpression wizard dialog.
8 """ 8 """
9 9
10 import os 10 import os
11 import pathlib
11 import re 12 import re
12 import json 13 import json
13 14
14 from PyQt6.QtCore import QFileInfo, pyqtSlot, QProcess, QByteArray 15 from PyQt6.QtCore import pyqtSlot, QProcess, QByteArray
15 from PyQt6.QtGui import QClipboard, QTextCursor 16 from PyQt6.QtGui import QClipboard, QTextCursor
16 from PyQt6.QtWidgets import ( 17 from PyQt6.QtWidgets import (
17 QWidget, QDialog, QInputDialog, QApplication, QDialogButtonBox, 18 QWidget, QDialog, QInputDialog, QApplication, QDialogButtonBox,
18 QVBoxLayout, QTableWidgetItem 19 QVBoxLayout, QTableWidgetItem
19 ) 20 )
408 "", 409 "",
409 self.tr("RegExp Files (*.rx);;All Files (*)"), 410 self.tr("RegExp Files (*.rx);;All Files (*)"),
410 None, 411 None,
411 EricFileDialog.DontConfirmOverwrite) 412 EricFileDialog.DontConfirmOverwrite)
412 if fname: 413 if fname:
413 ext = QFileInfo(fname).suffix() 414 fpath = pathlib.Path(fname)
414 if not ext: 415 if not fpath.suffix:
415 ex = selectedFilter.split("(*")[1].split(")")[0] 416 ex = selectedFilter.split("(*")[1].split(")")[0]
416 if ex: 417 if ex:
417 fname += ex 418 fpath = fpath.with_suffix(ex)
418 if QFileInfo(fname).exists(): 419 if fpath.exists():
419 res = EricMessageBox.yesNo( 420 res = EricMessageBox.yesNo(
420 self, 421 self,
421 self.tr("Save regular expression"), 422 self.tr("Save regular expression"),
422 self.tr("<p>The file <b>{0}</b> already exists." 423 self.tr("<p>The file <b>{0}</b> already exists."
423 " Overwrite it?</p>").format(fname), 424 " Overwrite it?</p>").format(str(fpath)),
424 icon=EricMessageBox.Warning) 425 icon=EricMessageBox.Warning)
425 if not res: 426 if not res:
426 return 427 return
427 428
428 fname = Utilities.toNativeSeparators(fname)
429 try: 429 try:
430 with open(fname, "w", encoding="utf-8") as f: 430 with fpath.open("w", encoding="utf-8") as f:
431 f.write(self.regexpTextEdit.toPlainText()) 431 f.write(self.regexpTextEdit.toPlainText())
432 except OSError as err: 432 except OSError as err:
433 EricMessageBox.information( 433 EricMessageBox.information(
434 self, 434 self,
435 self.tr("Save regular expression"), 435 self.tr("Save regular expression"),

eric ide

mercurial