CondaInterface/CondaExecDialog.py

branch
conda
changeset 6697
2f5c951bdf14
parent 6685
fbaee9890715
child 6706
d792e054cde2
equal deleted inserted replaced
6696:706185900558 6697:2f5c951bdf14
21 from E5Gui import E5MessageBox 21 from E5Gui import E5MessageBox
22 22
23 from .Ui_CondaExecDialog import Ui_CondaExecDialog 23 from .Ui_CondaExecDialog import Ui_CondaExecDialog
24 24
25 import Preferences 25 import Preferences
26 from Globals import dataString 26 import Globals
27 27
28 28
29 class CondaExecDialog(QDialog, Ui_CondaExecDialog): 29 class CondaExecDialog(QDialog, Ui_CondaExecDialog):
30 """ 30 """
31 Class implementing a dialog to show the output of a conda execution. 31 Class implementing a dialog to show the output of a conda execution.
32 """ 32 """
33 def __init__(self, configuration, venvManager, parent=None): 33 def __init__(self, command, parent=None):
34 """ 34 """
35 Constructor 35 Constructor
36 36
37 @param configuration dictionary containing the configuration parameters 37 @param command conda command executed
38 as returned by the command configuration dialog 38 @type str
39 @type dict
40 @param venvManager reference to the virtual environment manager
41 @type VirtualenvManager
42 @param parent reference to the parent widget 39 @param parent reference to the parent widget
43 @type QWidget 40 @type QWidget
44 """ 41 """
45 super(CondaExecDialog, self).__init__(parent) 42 super(CondaExecDialog, self).__init__(parent)
46 self.setupUi(self) 43 self.setupUi(self)
47 44
48 self.__finishCommandMethods = { 45 self.buttonBox.button(QDialogButtonBox.Close).setEnabled(False)
49 "create": self.__finishCreate, 46 self.buttonBox.button(QDialogButtonBox.Cancel).setDefault(True)
50 } 47
51 48 self.__condaCommand = command
52 self.__venvName = configuration["logicalName"]
53 self.__venvManager = venvManager
54 self.__condaCommand = configuration["command"]
55 49
56 self.__process = None 50 self.__process = None
57 self.__condaExe = Preferences.getConda("CondaExecutable") 51 self.__condaExe = Preferences.getConda("CondaExecutable")
58 if not self.__condaExe: 52 if not self.__condaExe:
59 self.__condaExe = "conda" 53 self.__condaExe = "conda"
89 83
90 self.__bufferedStdout = None 84 self.__bufferedStdout = None
91 self.__json = "--json" in arguments 85 self.__json = "--json" in arguments
92 self.__firstProgress = True 86 self.__firstProgress = True
93 self.__lastFetchFile = "" 87 self.__lastFetchFile = ""
88
89 self.__statusOk = False
90 self.__result = None
94 91
95 self.__process = QProcess() 92 self.__process = QProcess()
96 self.__process.readyReadStandardOutput.connect(self.__readStdout) 93 self.__process.readyReadStandardOutput.connect(self.__readStdout)
97 self.__process.readyReadStandardError.connect(self.__readStderr) 94 self.__process.readyReadStandardError.connect(self.__readStderr)
98 self.__process.finished.connect(self.__finish) 95 self.__process.finished.connect(self.__finish)
114 Private slot called when the process finished. 111 Private slot called when the process finished.
115 112
116 It is called when the process finished or 113 It is called when the process finished or
117 the user pressed the button. 114 the user pressed the button.
118 115
119 @param exitCode exit code of the process (integer) 116 @param exitCode exit code of the process
120 @param exitStatus exit status of the process (QProcess.ExitStatus) 117 @type int
121 @keyparam giveUp flag indicating to not start another attempt (boolean) 118 @param exitStatus exit status of the process
119 @type QProcess.ExitStatus
120 @param giveUp flag indicating to not start another attempt
121 @type bool
122 """ 122 """
123 if self.__process is not None and \ 123 if self.__process is not None and \
124 self.__process.state() != QProcess.NotRunning: 124 self.__process.state() != QProcess.NotRunning:
125 self.__process.terminate() 125 self.__process.terminate()
126 QTimer.singleShot(2000, self.__process.kill) 126 QTimer.singleShot(2000, self.__process.kill)
131 self.buttonBox.button(QDialogButtonBox.Close).setDefault(True) 131 self.buttonBox.button(QDialogButtonBox.Close).setDefault(True)
132 132
133 self.progressLabel.hide() 133 self.progressLabel.hide()
134 self.progressBar.hide() 134 self.progressBar.hide()
135 135
136 self.__statusOk = exitCode == 0
137
136 self.__logOutput(self.tr("Operation finished.\n")) 138 self.__logOutput(self.tr("Operation finished.\n"))
137 if self.__json: 139 if not self.__json and self.__bufferedStdout:
138 if self.__bufferedStdout: 140 self.__logOutput(self.__bufferedStdout)
141
142 if self.__json and self.__bufferedStdout:
139 index = self.__bufferedStdout.find("{") 143 index = self.__bufferedStdout.find("{")
140 rindex = self.__bufferedStdout.rfind("}") 144 rindex = self.__bufferedStdout.rfind("}")
141 self.__bufferedStdout = self.__bufferedStdout[index:rindex + 1] 145 self.__bufferedStdout = self.__bufferedStdout[index:rindex + 1]
142 try: 146 try:
143 jsonDict = json.loads(self.__bufferedStdout) 147 self.__result = json.loads(self.__bufferedStdout)
144 except Exception as error: 148 except Exception as error:
149 self.__result = {}
145 self.__logError(str(error)) 150 self.__logError(str(error))
146 return 151 return
147 152
148 if "error" in jsonDict: 153 if "error" in self.__result:
149 self.__logError(jsonDict["error"]) 154 self.__logError(self.__result["error"])
150 elif "success" in jsonDict and jsonDict["success"]: 155 self.__statusOk = False
151 self.__finishCommandMethods[self.__condaCommand](jsonDict) 156 elif "success" in self.__result and \
152 else: 157 not self.__result["success"]:
153 self.__logError( 158 self.__logError(
154 self.tr("Conda command '{0}' did not return success.") 159 self.tr("Conda command '{0}' did not return success.")
155 .format(self.__condaCommand)) 160 .format(self.__condaCommand))
156 elif self.__bufferedStdout: 161 self.__statusOk = False
157 self.__logOutput(self.__bufferedStdout) 162
158 163 def getResult(self):
159 def __finishCreate(self, resultDict): 164 """
160 """ 165 Public method to the result of the command execution.
161 Private method to finish the 'create' command. 166
162 167 @return tuple containing a flag indicating success and the result data.
163 @param resultDict dictionary containing the 'create' result data 168 @rtype tuple of (bool, dict)
164 @type dict 169 """
165 """ 170 return self.__statusOk, self.__result
166 if "actions" in resultDict and \
167 "PREFIX" in resultDict["actions"]:
168 prefix = resultDict["actions"]["PREFIX"]
169 elif "prefix" in resultDict:
170 prefix = resultDict["prefix"]
171 elif "dst_prefix" in resultDict:
172 prefix = resultDict["dst_prefix"]
173 else:
174 prefix = ""
175 self.__venvManager.addVirtualEnv(self.__venvName,
176 prefix,
177 isConda=True)
178 171
179 def __setProgressValues(self, jsonDict, progressType): 172 def __setProgressValues(self, jsonDict, progressType):
180 """ 173 """
181 Private method to set the value of the progress bar. 174 Private method to set the value of the progress bar.
182 175
198 filesize = parts[1].strip() 191 filesize = parts[1].strip()
199 else: 192 else:
200 self.progressBar.setMaximum(jsonDict["maxval"]) 193 self.progressBar.setMaximum(jsonDict["maxval"])
201 self.progressBar.setValue(jsonDict["progress"]) 194 self.progressBar.setValue(jsonDict["progress"])
202 filename = jsonDict["fetch"].strip() 195 filename = jsonDict["fetch"].strip()
203 filesize = dataString(int(jsonDict["maxval"])) 196 filesize = Globals.dataString(int(jsonDict["maxval"]))
204 197
205 self.progressLabel.setText( 198 self.progressLabel.setText(
206 self.tr("{0} (Size: {1})").format(filename, filesize)) 199 self.tr("{0} (Size: {1})").format(filename, filesize))
207 200
208 if progressType == "fetch": 201 if progressType == "fetch":

eric ide

mercurial