5 |
5 |
6 """ |
6 """ |
7 Module implementing a dialog to generate a requirements file. |
7 Module implementing a dialog to generate a requirements file. |
8 """ |
8 """ |
9 |
9 |
|
10 import enum |
10 import os |
11 import os |
11 |
12 |
12 from PyQt6.QtCore import pyqtSlot, Qt |
13 from PyQt6.QtCore import pyqtSlot, Qt |
13 from PyQt6.QtWidgets import QDialog, QDialogButtonBox, QAbstractButton, QApplication |
14 from PyQt6.QtWidgets import QDialog, QDialogButtonBox, QAbstractButton, QApplication |
14 |
15 |
20 from .Ui_PipFreezeDialog import Ui_PipFreezeDialog |
21 from .Ui_PipFreezeDialog import Ui_PipFreezeDialog |
21 |
22 |
22 import Utilities |
23 import Utilities |
23 |
24 |
24 |
25 |
|
26 class PipFreezeDialogModes(enum.Enum): |
|
27 """ |
|
28 Class defining the various dialog modes. |
|
29 """ |
|
30 |
|
31 Constraints = 1 |
|
32 Requirements = 2 |
|
33 |
|
34 |
25 class PipFreezeDialog(QDialog, Ui_PipFreezeDialog): |
35 class PipFreezeDialog(QDialog, Ui_PipFreezeDialog): |
26 """ |
36 """ |
27 Class implementing a dialog to generate a requirements file. |
37 Class implementing a dialog to generate a requirements file. |
28 """ |
38 """ |
29 |
39 |
30 def __init__(self, pip, parent=None): |
40 def __init__(self, pip, mode=PipFreezeDialogModes.Requirements, parent=None): |
31 """ |
41 """ |
32 Constructor |
42 Constructor |
33 |
43 |
34 @param pip reference to the master object |
44 @param pip reference to the master object |
35 @type Pip |
45 @type Pip |
36 @param parent reference to the parent widget |
46 @param mode dialog mod (defaults to PipFreezeDialogModes.Requirements) |
37 @type QWidget |
47 @type PipFreezeDialogModes (optional) |
|
48 @param parent reference to the parent widget (defaults to None |
|
49 @type QWidget (optional) |
38 """ |
50 """ |
39 super().__init__(parent) |
51 super().__init__(parent) |
40 self.setupUi(self) |
52 self.setupUi(self) |
41 self.setWindowFlags(Qt.WindowType.Window) |
53 self.setWindowFlags(Qt.WindowType.Window) |
|
54 |
|
55 self.__dialogMode = mode |
|
56 if mode is PipFreezeDialogModes.Constraints: |
|
57 self.constraintsCheckBox.setChecked(False) |
|
58 self.constraintsCheckBox.setEnabled(False) |
|
59 |
|
60 self.setWindowTitle(self.tr("Generate Constraints")) |
|
61 |
|
62 elif mode is PipFreezeDialogModes.Requirements: |
|
63 self.setWindowTitle(self.tr("Generate Requirements")) |
42 |
64 |
43 self.__refreshButton = self.buttonBox.addButton( |
65 self.__refreshButton = self.buttonBox.addButton( |
44 self.tr("&Refresh"), QDialogButtonBox.ButtonRole.ActionRole |
66 self.tr("&Refresh"), QDialogButtonBox.ButtonRole.ActionRole |
45 ) |
67 ) |
46 |
68 |
139 """ |
161 """ |
140 self.requirementsEdit.clear() |
162 self.requirementsEdit.clear() |
141 self.__requirementsAvailable = False |
163 self.__requirementsAvailable = False |
142 self.__environmentName = venvName |
164 self.__environmentName = venvName |
143 |
165 |
144 fileName = ( |
166 if not bool(self.requirementsFilePicker.text()): |
145 Utilities.toNativeSeparators(self.requirementsFilePicker.text()) |
167 self.requirementsFilePicker.setText( |
146 if self.requirementsFilePicker.text() |
168 "constraints.txt" |
147 else "" |
169 if self.__dialogMode is PipFreezeDialogModes.Constraints |
148 ) |
170 else "requirements.txt" |
|
171 ) |
|
172 |
|
173 fileName = Utilities.toNativeSeparators(self.requirementsFilePicker.text()) |
|
174 if fileName and not os.path.isabs(fileName): |
|
175 fileName = "" |
149 |
176 |
150 with EricOverrideCursor(): |
177 with EricOverrideCursor(): |
151 specifiers = self.__pip.getFrozenPackages( |
178 specifiers = self.__pip.getFrozenPackages( |
152 venvName, |
179 venvName, |
153 localPackages=self.localCheckBox.isChecked(), |
180 localPackages=self.localCheckBox.isChecked(), |
170 def __updateButtons(self): |
197 def __updateButtons(self): |
171 """ |
198 """ |
172 Private method to set the state of the various buttons. |
199 Private method to set the state of the various buttons. |
173 """ |
200 """ |
174 self.saveButton.setEnabled( |
201 self.saveButton.setEnabled( |
175 self.__requirementsAvailable and bool(self.requirementsFilePicker.text()) |
202 self.__requirementsAvailable |
|
203 and bool(self.requirementsFilePicker.text()) |
|
204 and os.path.isabs(self.requirementsFilePicker.text()) |
176 ) |
205 ) |
177 self.saveToButton.setEnabled(self.__requirementsAvailable) |
206 self.saveToButton.setEnabled(self.__requirementsAvailable) |
178 self.copyButton.setEnabled(self.__requirementsAvailable) |
207 self.copyButton.setEnabled(self.__requirementsAvailable) |
179 |
208 |
180 aw = ericApp().getObject("ViewManager").activeWindow() |
209 aw = ericApp().getObject("ViewManager").activeWindow() |
204 ).format(fileName), |
233 ).format(fileName), |
205 ) |
234 ) |
206 if not ok: |
235 if not ok: |
207 return |
236 return |
208 |
237 |
|
238 txt = self.requirementsEdit.toPlainText() |
|
239 if self.constraintsCheckBox.isChecked(): |
|
240 txt = f"--constraint constraints.txt\n{txt}" |
209 try: |
241 try: |
210 with open(fileName, "w") as f: |
242 with open(fileName, "w") as f: |
211 f.write(self.requirementsEdit.toPlainText()) |
243 f.write(txt) |
212 except OSError as err: |
244 except OSError as err: |
213 EricMessageBox.critical( |
245 EricMessageBox.critical( |
214 self, |
246 self, |
215 self.tr("Generate Requirements"), |
247 self.tr("Generate Requirements"), |
216 self.tr( |
248 self.tr( |
252 def on_copyButton_clicked(self): |
284 def on_copyButton_clicked(self): |
253 """ |
285 """ |
254 Private slot to copy the requirements text to the clipboard. |
286 Private slot to copy the requirements text to the clipboard. |
255 """ |
287 """ |
256 txt = self.requirementsEdit.toPlainText() |
288 txt = self.requirementsEdit.toPlainText() |
|
289 if self.constraintsCheckBox.isChecked(): |
|
290 txt = f"--constraint constraints.txt\n{txt}" |
257 cb = QApplication.clipboard() |
291 cb = QApplication.clipboard() |
258 cb.setText(txt) |
292 cb.setText(txt) |
259 |
293 |
260 @pyqtSlot() |
294 @pyqtSlot() |
261 def on_insertButton_clicked(self): |
295 def on_insertButton_clicked(self): |