src/eric7/CondaInterface/CondaExportDialog.py

branch
eric7
changeset 9221
bf71ee032bb4
parent 9209
b99e7fd55fd3
child 9413
80c06d472826
equal deleted inserted replaced
9220:e9e7eca7efee 9221:bf71ee032bb4
8 """ 8 """
9 9
10 import os 10 import os
11 11
12 from PyQt6.QtCore import pyqtSlot, Qt 12 from PyQt6.QtCore import pyqtSlot, Qt
13 from PyQt6.QtWidgets import ( 13 from PyQt6.QtWidgets import QDialog, QDialogButtonBox, QAbstractButton, QApplication
14 QDialog, QDialogButtonBox, QAbstractButton, QApplication
15 )
16 14
17 from EricWidgets import EricMessageBox, EricFileDialog 15 from EricWidgets import EricMessageBox, EricFileDialog
18 from EricWidgets.EricPathPicker import EricPathPickerModes 16 from EricWidgets.EricPathPicker import EricPathPickerModes
19 from EricWidgets.EricApplication import ericApp 17 from EricWidgets.EricApplication import ericApp
20 from EricGui.EricOverrideCursor import EricOverrideCursor 18 from EricGui.EricOverrideCursor import EricOverrideCursor
24 22
25 class CondaExportDialog(QDialog, Ui_CondaExportDialog): 23 class CondaExportDialog(QDialog, Ui_CondaExportDialog):
26 """ 24 """
27 Class implementing a dialog to generate a requirements file for conda. 25 Class implementing a dialog to generate a requirements file for conda.
28 """ 26 """
27
29 def __init__(self, conda, envName, envPrefix, parent=None): 28 def __init__(self, conda, envName, envPrefix, parent=None):
30 """ 29 """
31 Constructor 30 Constructor
32 31
33 @param conda reference to the master object 32 @param conda reference to the master object
34 @type Conda 33 @type Conda
35 @param envName name of the environment to create the requirements 34 @param envName name of the environment to create the requirements
36 file from 35 file from
37 @type str 36 @type str
42 @type QWidget 41 @type QWidget
43 """ 42 """
44 super().__init__(parent) 43 super().__init__(parent)
45 self.setupUi(self) 44 self.setupUi(self)
46 self.setWindowFlags(Qt.WindowType.Window) 45 self.setWindowFlags(Qt.WindowType.Window)
47 46
48 self.__refreshButton = self.buttonBox.addButton( 47 self.__refreshButton = self.buttonBox.addButton(
49 self.tr("&Refresh"), QDialogButtonBox.ButtonRole.ActionRole) 48 self.tr("&Refresh"), QDialogButtonBox.ButtonRole.ActionRole
50 49 )
50
51 self.requirementsFilePicker.setMode(EricPathPickerModes.SAVE_FILE_MODE) 51 self.requirementsFilePicker.setMode(EricPathPickerModes.SAVE_FILE_MODE)
52 self.requirementsFilePicker.setFilters( 52 self.requirementsFilePicker.setFilters(
53 self.tr("Text Files (*.txt);;All Files (*)")) 53 self.tr("Text Files (*.txt);;All Files (*)")
54 54 )
55
55 self.__conda = conda 56 self.__conda = conda
56 self.__prefix = envPrefix 57 self.__prefix = envPrefix
57 58
58 self.environmentLabel.setText("<b>{0}</b>".format(envName)) 59 self.environmentLabel.setText("<b>{0}</b>".format(envName))
59 60
60 self.__requirementsEdited = False 61 self.__requirementsEdited = False
61 self.__requirementsAvailable = False 62 self.__requirementsAvailable = False
62 63
63 self.__updateButtons() 64 self.__updateButtons()
64 65
65 def closeEvent(self, e): 66 def closeEvent(self, e):
66 """ 67 """
67 Protected slot implementing a close event handler. 68 Protected slot implementing a close event handler.
68 69
69 @param e close event 70 @param e close event
70 @type QCloseEvent 71 @type QCloseEvent
71 """ 72 """
72 e.accept() 73 e.accept()
73 74
74 @pyqtSlot(str) 75 @pyqtSlot(str)
75 def on_requirementsFilePicker_textChanged(self, txt): 76 def on_requirementsFilePicker_textChanged(self, txt):
76 """ 77 """
77 Private slot handling a change of the requirements file name. 78 Private slot handling a change of the requirements file name.
78 79
79 @param txt name of the requirements file 80 @param txt name of the requirements file
80 @type str 81 @type str
81 """ 82 """
82 self.__updateButtons() 83 self.__updateButtons()
83 84
84 @pyqtSlot() 85 @pyqtSlot()
85 def on_requirementsEdit_textChanged(self): 86 def on_requirementsEdit_textChanged(self):
86 """ 87 """
87 Private slot handling changes of the requirements text. 88 Private slot handling changes of the requirements text.
88 """ 89 """
89 self.__requirementsEdited = True 90 self.__requirementsEdited = True
90 91
91 @pyqtSlot(QAbstractButton) 92 @pyqtSlot(QAbstractButton)
92 def on_buttonBox_clicked(self, button): 93 def on_buttonBox_clicked(self, button):
93 """ 94 """
94 Private slot called by a button of the button box clicked. 95 Private slot called by a button of the button box clicked.
95 96
96 @param button button that was clicked 97 @param button button that was clicked
97 @type QAbstractButton 98 @type QAbstractButton
98 """ 99 """
99 if button == self.buttonBox.button( 100 if button == self.buttonBox.button(QDialogButtonBox.StandardButton.Close):
100 QDialogButtonBox.StandardButton.Close
101 ):
102 self.close() 101 self.close()
103 elif button == self.__refreshButton: 102 elif button == self.__refreshButton:
104 self.__refresh() 103 self.__refresh()
105 104
106 def __refresh(self): 105 def __refresh(self):
107 """ 106 """
108 Private slot to refresh the displayed list. 107 Private slot to refresh the displayed list.
109 """ 108 """
110 ok = ( 109 ok = (
111 EricMessageBox.yesNo( 110 EricMessageBox.yesNo(
112 self, 111 self,
113 self.tr("Generate Requirements"), 112 self.tr("Generate Requirements"),
114 self.tr("""The requirements were changed. Do you want""" 113 self.tr(
115 """ to overwrite these changes?""")) 114 """The requirements were changed. Do you want"""
116 if self.__requirementsEdited else 115 """ to overwrite these changes?"""
117 True 116 ),
117 )
118 if self.__requirementsEdited
119 else True
118 ) 120 )
119 if ok: 121 if ok:
120 self.start() 122 self.start()
121 123
122 def start(self): 124 def start(self):
123 """ 125 """
124 Public method to start the command. 126 Public method to start the command.
125 """ 127 """
126 self.requirementsEdit.clear() 128 self.requirementsEdit.clear()
127 self.__requirementsAvailable = False 129 self.__requirementsAvailable = False
128 130
129 args = [ 131 args = [
130 "list", 132 "list",
131 "--export", 133 "--export",
132 "--prefix", 134 "--prefix",
133 self.__prefix, 135 self.__prefix,
134 ] 136 ]
135 137
136 with EricOverrideCursor(): 138 with EricOverrideCursor():
137 success, output = self.__conda.runProcess(args) 139 success, output = self.__conda.runProcess(args)
138 140
139 if success: 141 if success:
140 self.requirementsEdit.setPlainText(output) 142 self.requirementsEdit.setPlainText(output)
141 self.__requirementsAvailable = True 143 self.__requirementsAvailable = True
142 else: 144 else:
143 self.requirementsEdit.setPlainText( 145 self.requirementsEdit.setPlainText(
144 self.tr("No output generated by conda.")) 146 self.tr("No output generated by conda.")
145 147 )
148
146 self.__updateButtons() 149 self.__updateButtons()
147 150
148 self.__requirementsEdited = False 151 self.__requirementsEdited = False
149 152
150 def __updateButtons(self): 153 def __updateButtons(self):
151 """ 154 """
152 Private method to set the state of the various buttons. 155 Private method to set the state of the various buttons.
153 """ 156 """
154 self.saveButton.setEnabled( 157 self.saveButton.setEnabled(
155 self.__requirementsAvailable and 158 self.__requirementsAvailable and bool(self.requirementsFilePicker.text())
156 bool(self.requirementsFilePicker.text())
157 ) 159 )
158 self.saveToButton.setEnabled(self.__requirementsAvailable) 160 self.saveToButton.setEnabled(self.__requirementsAvailable)
159 self.copyButton.setEnabled(self.__requirementsAvailable) 161 self.copyButton.setEnabled(self.__requirementsAvailable)
160 162
161 aw = ericApp().getObject("ViewManager").activeWindow() 163 aw = ericApp().getObject("ViewManager").activeWindow()
162 if aw and self.__requirementsAvailable: 164 if aw and self.__requirementsAvailable:
163 self.insertButton.setEnabled(True) 165 self.insertButton.setEnabled(True)
164 self.replaceAllButton.setEnabled(True) 166 self.replaceAllButton.setEnabled(True)
165 self.replaceSelectionButton.setEnabled( 167 self.replaceSelectionButton.setEnabled(aw.hasSelectedText())
166 aw.hasSelectedText())
167 else: 168 else:
168 self.insertButton.setEnabled(False) 169 self.insertButton.setEnabled(False)
169 self.replaceAllButton.setEnabled(False) 170 self.replaceAllButton.setEnabled(False)
170 self.replaceSelectionButton.setEnabled(False) 171 self.replaceSelectionButton.setEnabled(False)
171 172
172 def __writeToFile(self, fileName): 173 def __writeToFile(self, fileName):
173 """ 174 """
174 Private method to write the requirements text to a file. 175 Private method to write the requirements text to a file.
175 176
176 @param fileName name of the file to write to 177 @param fileName name of the file to write to
177 @type str 178 @type str
178 """ 179 """
179 if os.path.exists(fileName): 180 if os.path.exists(fileName):
180 ok = EricMessageBox.warning( 181 ok = EricMessageBox.warning(
181 self, 182 self,
182 self.tr("Generate Requirements"), 183 self.tr("Generate Requirements"),
183 self.tr("""The file <b>{0}</b> already exists. Do you want""" 184 self.tr(
184 """ to overwrite it?""").format(fileName)) 185 """The file <b>{0}</b> already exists. Do you want"""
186 """ to overwrite it?"""
187 ).format(fileName),
188 )
185 if not ok: 189 if not ok:
186 return 190 return
187 191
188 try: 192 try:
189 with open(fileName, "w") as f: 193 with open(fileName, "w") as f:
190 f.write(self.requirementsEdit.toPlainText()) 194 f.write(self.requirementsEdit.toPlainText())
191 except OSError as err: 195 except OSError as err:
192 EricMessageBox.critical( 196 EricMessageBox.critical(
193 self, 197 self,
194 self.tr("Generate Requirements"), 198 self.tr("Generate Requirements"),
195 self.tr("""<p>The requirements could not be written""" 199 self.tr(
196 """ to <b>{0}</b>.</p><p>Reason: {1}</p>""") 200 """<p>The requirements could not be written"""
197 .format(fileName, str(err))) 201 """ to <b>{0}</b>.</p><p>Reason: {1}</p>"""
198 202 ).format(fileName, str(err)),
203 )
204
199 @pyqtSlot() 205 @pyqtSlot()
200 def on_saveButton_clicked(self): 206 def on_saveButton_clicked(self):
201 """ 207 """
202 Private slot to save the requirements text to the requirements file. 208 Private slot to save the requirements text to the requirements file.
203 """ 209 """
204 fileName = self.requirementsFilePicker.text() 210 fileName = self.requirementsFilePicker.text()
205 self.__writeToFile(fileName) 211 self.__writeToFile(fileName)
206 212
207 @pyqtSlot() 213 @pyqtSlot()
208 def on_saveToButton_clicked(self): 214 def on_saveToButton_clicked(self):
209 """ 215 """
210 Private slot to write the requirements text to a new file. 216 Private slot to write the requirements text to a new file.
211 """ 217 """
213 self, 219 self,
214 self.tr("Generate Requirements"), 220 self.tr("Generate Requirements"),
215 os.path.expanduser("~"), 221 os.path.expanduser("~"),
216 self.tr("Text Files (*.txt);;All Files (*)"), 222 self.tr("Text Files (*.txt);;All Files (*)"),
217 None, 223 None,
218 EricFileDialog.DontConfirmOverwrite 224 EricFileDialog.DontConfirmOverwrite,
219 ) 225 )
220 if fileName: 226 if fileName:
221 ext = os.path.splitext(fileName)[1] 227 ext = os.path.splitext(fileName)[1]
222 if not ext: 228 if not ext:
223 ex = selectedFilter.split("(*")[1].split(")")[0] 229 ex = selectedFilter.split("(*")[1].split(")")[0]
224 if ex: 230 if ex:
225 fileName += ex 231 fileName += ex
226 self.__writeToFile(fileName) 232 self.__writeToFile(fileName)
227 233
228 @pyqtSlot() 234 @pyqtSlot()
229 def on_copyButton_clicked(self): 235 def on_copyButton_clicked(self):
230 """ 236 """
231 Private slot to copy the requirements text to the clipboard. 237 Private slot to copy the requirements text to the clipboard.
232 """ 238 """
233 txt = self.requirementsEdit.toPlainText() 239 txt = self.requirementsEdit.toPlainText()
234 cb = QApplication.clipboard() 240 cb = QApplication.clipboard()
235 cb.setText(txt) 241 cb.setText(txt)
236 242
237 @pyqtSlot() 243 @pyqtSlot()
238 def on_insertButton_clicked(self): 244 def on_insertButton_clicked(self):
239 """ 245 """
240 Private slot to insert the requirements text at the cursor position 246 Private slot to insert the requirements text at the cursor position
241 of the current editor. 247 of the current editor.
244 if aw: 250 if aw:
245 aw.beginUndoAction() 251 aw.beginUndoAction()
246 cline, cindex = aw.getCursorPosition() 252 cline, cindex = aw.getCursorPosition()
247 aw.insertAt(self.requirementsEdit.toPlainText(), cline, cindex) 253 aw.insertAt(self.requirementsEdit.toPlainText(), cline, cindex)
248 aw.endUndoAction() 254 aw.endUndoAction()
249 255
250 @pyqtSlot() 256 @pyqtSlot()
251 def on_replaceSelectionButton_clicked(self): 257 def on_replaceSelectionButton_clicked(self):
252 """ 258 """
253 Private slot to replace the selected text of the current editor 259 Private slot to replace the selected text of the current editor
254 with the requirements text. 260 with the requirements text.
256 aw = ericApp().getObject("ViewManager").activeWindow() 262 aw = ericApp().getObject("ViewManager").activeWindow()
257 if aw: 263 if aw:
258 aw.beginUndoAction() 264 aw.beginUndoAction()
259 aw.replaceSelectedText(self.requirementsEdit.toPlainText()) 265 aw.replaceSelectedText(self.requirementsEdit.toPlainText())
260 aw.endUndoAction() 266 aw.endUndoAction()
261 267
262 @pyqtSlot() 268 @pyqtSlot()
263 def on_replaceAllButton_clicked(self): 269 def on_replaceAllButton_clicked(self):
264 """ 270 """
265 Private slot to replace the text of the current editor with the 271 Private slot to replace the text of the current editor with the
266 requirements text. 272 requirements text.

eric ide

mercurial