Plugins/UiExtensionPlugins/PipInterface/PipFreezeDialog.py

changeset 6342
c79ecba9cde7
parent 6048
82ad8ec9548c
child 6619
1d34365c082c
equal deleted inserted replaced
6341:a00e63f6d766 6342:c79ecba9cde7
18 from PyQt5.QtCore import pyqtSlot, Qt 18 from PyQt5.QtCore import pyqtSlot, Qt
19 from PyQt5.QtWidgets import QDialog, QDialogButtonBox, QAbstractButton, \ 19 from PyQt5.QtWidgets import QDialog, QDialogButtonBox, QAbstractButton, \
20 QApplication 20 QApplication
21 21
22 from E5Gui import E5MessageBox, E5FileDialog 22 from E5Gui import E5MessageBox, E5FileDialog
23 from E5Gui.E5PathPicker import E5PathPickerModes
23 from E5Gui.E5Application import e5App 24 from E5Gui.E5Application import e5App
24 25
25 from .Ui_PipFreezeDialog import Ui_PipFreezeDialog 26 from .Ui_PipFreezeDialog import Ui_PipFreezeDialog
26 27
27 import Utilities 28 import Utilities
28 import UI.PixmapCache
29 29
30 30
31 class PipFreezeDialog(QDialog, Ui_PipFreezeDialog): 31 class PipFreezeDialog(QDialog, Ui_PipFreezeDialog):
32 """ 32 """
33 Class implementing a dialog to generate a requirements file. 33 Class implementing a dialog to generate a requirements file.
34 """ 34 """
35 def __init__(self, pip, plugin, parent=None): 35 def __init__(self, pip, parent=None):
36 """ 36 """
37 Constructor 37 Constructor
38 38
39 @param pip reference to the master object (Pip) 39 @param pip reference to the master object
40 @param plugin reference to the plugin object (ToolPipPlugin) 40 @type Pip
41 @param parent reference to the parent widget (QWidget) 41 @param parent reference to the parent widget
42 @type QWidget
42 """ 43 """
43 super(PipFreezeDialog, self).__init__(parent) 44 super(PipFreezeDialog, self).__init__(parent)
44 self.setupUi(self) 45 self.setupUi(self)
45 self.setWindowFlags(Qt.Window) 46 self.setWindowFlags(Qt.Window)
46 47
47 self.__refreshButton = self.buttonBox.addButton( 48 self.__refreshButton = self.buttonBox.addButton(
48 self.tr("&Refresh"), QDialogButtonBox.ActionRole) 49 self.tr("&Refresh"), QDialogButtonBox.ActionRole)
49 50
50 self.fileButton.setIcon(UI.PixmapCache.getIcon("open.png")) 51 self.requirementsFilePicker.setMode(E5PathPickerModes.OpenFileMode)
52 self.requirementsFilePicker.setFilters(
53 self.tr("Text Files (*.txt);;All Files (*)"))
51 54
52 self.__pip = pip 55 self.__pip = pip
53 56
54 self.__default = self.tr("<Default>") 57 self.venvComboBox.addItem(pip.getDefaultEnvironmentString())
55 pipExecutables = sorted(plugin.getPreferences("PipExecutables")) 58 self.venvComboBox.addItems(pip.getVirtualenvNames())
56 self.pipComboBox.addItem(self.__default)
57 self.pipComboBox.addItems(pipExecutables)
58 59
59 self.__requirementsEdited = False 60 self.__requirementsEdited = False
60 self.__requirementsAvailable = False 61 self.__requirementsAvailable = False
61 62
62 self.__updateButtons() 63 self.__updateButtons()
63 64
64 def closeEvent(self, e): 65 def closeEvent(self, e):
65 """ 66 """
66 Protected slot implementing a close event handler. 67 Protected slot implementing a close event handler.
67 68
68 @param e close event (QCloseEvent) 69 @param e close event
70 @type QCloseEvent
69 """ 71 """
70 QApplication.restoreOverrideCursor() 72 QApplication.restoreOverrideCursor()
71 e.accept() 73 e.accept()
72 74
73 @pyqtSlot(str) 75 @pyqtSlot(str)
74 def on_pipComboBox_activated(self, txt): 76 def on_venvComboBox_activated(self, txt):
75 """ 77 """
76 Private slot handling the selection of a pip executable. 78 Private slot handling the selection of a virtual environment.
77 79
78 @param txt path of the pip executable (string) 80 @param txt virtual environment
81 @type str
79 """ 82 """
80 self.__refresh() 83 self.__refresh()
81 84
82 @pyqtSlot(bool) 85 @pyqtSlot(bool)
83 def on_localCheckBox_clicked(self, checked): 86 def on_localCheckBox_clicked(self, checked):
84 """ 87 """
85 Private slot handling the switching of the local mode. 88 Private slot handling the switching of the local mode.
86 89
87 @param checked state of the local check box (boolean) 90 @param checked state of the local check box
91 @type bool
88 """ 92 """
89 self.__refresh() 93 self.__refresh()
90 94
91 @pyqtSlot(str) 95 @pyqtSlot(str)
92 def on_requirementsFileEdit_textChanged(self, txt): 96 def on_requirementsFilePicker_textChanged(self, txt):
93 """ 97 """
94 Private slot handling a change of the requirements file name. 98 Private slot handling a change of the requirements file name.
95 99
96 @param txt name of the requirements file (string) 100 @param txt name of the requirements file
101 @type str
97 """ 102 """
98 self.__updateButtons() 103 self.__updateButtons()
99
100 @pyqtSlot()
101 def on_fileButton_clicked(self):
102 """
103 Private slot to enter the requirements file via a file selection
104 dialog.
105 """
106 fileName = E5FileDialog.getOpenFileName(
107 self,
108 self.tr("Select the requirements file"),
109 self.requirementsFileEdit.text() or os.path.expanduser("~"),
110 self.tr("Text Files (*.txt);;All Files (*)")
111 )
112 if fileName:
113 self.requirementsFileEdit.setText(
114 Utilities.toNativeSeparators(fileName))
115 104
116 @pyqtSlot() 105 @pyqtSlot()
117 def on_requirementsEdit_textChanged(self): 106 def on_requirementsEdit_textChanged(self):
118 """ 107 """
119 Private slot handling changes of the requirements text. 108 Private slot handling changes of the requirements text.
123 @pyqtSlot(QAbstractButton) 112 @pyqtSlot(QAbstractButton)
124 def on_buttonBox_clicked(self, button): 113 def on_buttonBox_clicked(self, button):
125 """ 114 """
126 Private slot called by a button of the button box clicked. 115 Private slot called by a button of the button box clicked.
127 116
128 @param button button that was clicked (QAbstractButton) 117 @param button button that was clicked
118 @type QAbstractButton
129 """ 119 """
130 if button == self.buttonBox.button(QDialogButtonBox.Close): 120 if button == self.buttonBox.button(QDialogButtonBox.Close):
131 self.close() 121 self.close()
132 elif button == self.__refreshButton: 122 elif button == self.__refreshButton:
133 self.__refresh() 123 self.__refresh()
152 Public method to start the command. 142 Public method to start the command.
153 """ 143 """
154 self.requirementsEdit.clear() 144 self.requirementsEdit.clear()
155 self.__requirementsAvailable = False 145 self.__requirementsAvailable = False
156 146
157 command = self.pipComboBox.currentText() 147 venvName = self.venvComboBox.currentText()
158 if command == self.__default: 148 interpreter = self.__pip.getVirtualenvInterpreter(venvName)
159 command = "" 149 if not interpreter:
160 150 return
161 args = ["freeze"] 151
152 args = ["-m", "pip", "freeze"]
162 if self.localCheckBox.isChecked(): 153 if self.localCheckBox.isChecked():
163 args.append("--local") 154 args.append("--local")
164 if self.requirementsFileEdit.text(): 155 if self.requirementsFilePicker.text():
165 fileName = Utilities.toNativeSeparators( 156 fileName = Utilities.toNativeSeparators(
166 self.requirementsFileEdit.text()) 157 self.requirementsFilePicker.text())
167 if os.path.exists(fileName): 158 if os.path.exists(fileName):
168 args.append("--requirement") 159 args.append("--requirement")
169 args.append(fileName) 160 args.append(fileName)
170 161
171 QApplication.setOverrideCursor(Qt.WaitCursor) 162 QApplication.setOverrideCursor(Qt.WaitCursor)
172 success, output = self.__pip.runProcess( 163 success, output = self.__pip.runProcess(args, interpreter)
173 args, cmd=command)
174 164
175 if success: 165 if success:
176 self.requirementsEdit.setPlainText(output) 166 self.requirementsEdit.setPlainText(output)
177 self.__requirementsAvailable = True 167 self.__requirementsAvailable = True
178 else: 168 else:
188 """ 178 """
189 Private method to set the state of the various buttons. 179 Private method to set the state of the various buttons.
190 """ 180 """
191 self.saveButton.setEnabled( 181 self.saveButton.setEnabled(
192 self.__requirementsAvailable and 182 self.__requirementsAvailable and
193 bool(self.requirementsFileEdit.text()) 183 bool(self.requirementsFilePicker.text())
194 ) 184 )
195 self.saveToButton.setEnabled(self.__requirementsAvailable) 185 self.saveToButton.setEnabled(self.__requirementsAvailable)
196 self.copyButton.setEnabled(self.__requirementsAvailable) 186 self.copyButton.setEnabled(self.__requirementsAvailable)
197 187
198 aw = e5App().getObject("ViewManager").activeWindow() 188 aw = e5App().getObject("ViewManager").activeWindow()
208 198
209 def __writeToFile(self, fileName): 199 def __writeToFile(self, fileName):
210 """ 200 """
211 Private method to write the requirements text to a file. 201 Private method to write the requirements text to a file.
212 202
213 @param fileName name of the file to write to (string) 203 @param fileName name of the file to write to
204 @type str
214 """ 205 """
215 if os.path.exists(fileName): 206 if os.path.exists(fileName):
216 ok = E5MessageBox.warning( 207 ok = E5MessageBox.warning(
217 self, 208 self,
218 self.tr("Generate Requirements"), 209 self.tr("Generate Requirements"),
236 @pyqtSlot() 227 @pyqtSlot()
237 def on_saveButton_clicked(self): 228 def on_saveButton_clicked(self):
238 """ 229 """
239 Private slot to save the requirements text to the requirements file. 230 Private slot to save the requirements text to the requirements file.
240 """ 231 """
241 fileName = self.requirementsFileEdit.text() 232 fileName = self.requirementsFilePicker.text()
242 self.__writeToFile(fileName) 233 self.__writeToFile(fileName)
243 234
244 @pyqtSlot() 235 @pyqtSlot()
245 def on_saveToButton_clicked(self): 236 def on_saveToButton_clicked(self):
246 """ 237 """

eric ide

mercurial