6 """ |
6 """ |
7 Module implementing a dialog to compare two files. |
7 Module implementing a dialog to compare two files. |
8 """ |
8 """ |
9 |
9 |
10 import os |
10 import os |
|
11 import pathlib |
11 import time |
12 import time |
12 import contextlib |
13 import contextlib |
13 from difflib import unified_diff, context_diff |
14 from difflib import unified_diff, context_diff |
14 |
15 |
15 from PyQt6.QtCore import QFileInfo, QEvent, pyqtSlot |
16 from PyQt6.QtCore import QEvent, pyqtSlot |
16 from PyQt6.QtGui import QTextCursor |
17 from PyQt6.QtGui import QTextCursor |
17 from PyQt6.QtWidgets import QWidget, QApplication, QDialogButtonBox |
18 from PyQt6.QtWidgets import QWidget, QApplication, QDialogButtonBox |
18 |
19 |
19 from EricWidgets import EricMessageBox, EricFileDialog |
20 from EricWidgets import EricMessageBox, EricFileDialog |
20 from EricWidgets.EricMainWindow import EricMainWindow |
21 from EricWidgets.EricMainWindow import EricMainWindow |
112 EricFileDialog.DontConfirmOverwrite) |
113 EricFileDialog.DontConfirmOverwrite) |
113 |
114 |
114 if not fname: |
115 if not fname: |
115 return |
116 return |
116 |
117 |
117 ext = QFileInfo(fname).suffix() |
118 fpath = pathlib.Path(fname) |
118 if not ext: |
119 if not fpath.suffix: |
119 ex = selectedFilter.split("(*")[1].split(")")[0] |
120 ex = selectedFilter.split("(*")[1].split(")")[0] |
120 if ex: |
121 if ex: |
121 fname += ex |
122 fpath = fpath.with_suffix(ex) |
122 if QFileInfo(fname).exists(): |
123 if fpath.exists(): |
123 res = EricMessageBox.yesNo( |
124 res = EricMessageBox.yesNo( |
124 self, |
125 self, |
125 self.tr("Save Diff"), |
126 self.tr("Save Diff"), |
126 self.tr("<p>The patch file <b>{0}</b> already exists." |
127 self.tr("<p>The patch file <b>{0}</b> already exists." |
127 " Overwrite it?</p>").format(fname), |
128 " Overwrite it?</p>").format(str(fpath)), |
128 icon=EricMessageBox.Warning) |
129 icon=EricMessageBox.Warning) |
129 if not res: |
130 if not res: |
130 return |
131 return |
131 fname = Utilities.toNativeSeparators(fname) |
|
132 |
132 |
133 txt = self.contents.toPlainText() |
133 txt = self.contents.toPlainText() |
134 try: |
134 try: |
135 with open(fname, "w", encoding="utf-8") as f, \ |
135 with fpath.open("w", encoding="utf-8") as f, \ |
136 contextlib.suppress(UnicodeError): |
136 contextlib.suppress(UnicodeError): |
137 f.write(txt) |
137 f.write(txt) |
138 except OSError as why: |
138 except OSError as why: |
139 EricMessageBox.critical( |
139 EricMessageBox.critical( |
140 self, self.tr('Save Diff'), |
140 self, self.tr('Save Diff'), |
141 self.tr( |
141 self.tr( |
142 '<p>The patch file <b>{0}</b> could not be saved.<br />' |
142 '<p>The patch file <b>{0}</b> could not be saved.<br />' |
143 'Reason: {1}</p>').format(fname, str(why))) |
143 'Reason: {1}</p>').format(str(fpath), str(why))) |
144 |
144 |
145 @pyqtSlot() |
145 @pyqtSlot() |
146 def on_diffButton_clicked(self): |
146 def on_diffButton_clicked(self): |
147 """ |
147 """ |
148 Private slot to handle the Compare button press. |
148 Private slot to handle the Compare button press. |