VirtualEnv/VirtualenvManager.py

changeset 6338
104ee21d765d
parent 6337
c6af560e0039
child 6341
a00e63f6d766
equal deleted inserted replaced
6337:c6af560e0039 6338:104ee21d765d
52 if defaultPy not in self.__virtualEnvironmentInterpreters.values(): 52 if defaultPy not in self.__virtualEnvironmentInterpreters.values():
53 self.__virtualEnvironmentInterpreters["<default>"] = defaultPy 53 self.__virtualEnvironmentInterpreters["<default>"] = defaultPy
54 self.__virtualEnvironments["<default>"] = "" 54 self.__virtualEnvironments["<default>"] = ""
55 55
56 self.__updateSettings() 56 self.__updateSettings()
57
58 self.__virtualenvManagerDialog = None
57 59
58 def __updateSettings(self): 60 def __updateSettings(self):
59 """ 61 """
60 Private slot to save the virtual environments. 62 Private slot to save the virtual environments.
61 """ 63 """
84 self) 86 self)
85 dia.show() 87 dia.show()
86 dia.start(args) 88 dia.start(args)
87 dia.exec_() 89 dia.exec_()
88 90
89 def addVirtualEnv(self, venvName, venvDirectory): 91 def addVirtualEnv(self, venvName, venvDirectory, venvInterpreter=""):
90 """ 92 """
91 Public method to add a virtual environment. 93 Public method to add a virtual environment.
92 94
93 @param venvName logical name for the virtual environment 95 @param venvName logical name for the virtual environment
94 @type str 96 @type str
95 @param venvDirectory directory of the virtual envoronment 97 @param venvDirectory directory of the virtual environment
96 @type str 98 @type str
99 @param venvInterpreter interpreter of the virtual environment
97 """ 100 """
98 if venvName in self.__virtualEnvironments: 101 if venvName in self.__virtualEnvironments:
99 ok = E5MessageBox.yesNo( 102 ok = E5MessageBox.yesNo(
100 None, 103 None,
101 self.tr("Add Virtual Environment"), 104 self.tr("Add Virtual Environment"),
104 .format(venvName), 107 .format(venvName),
105 icon=E5MessageBox.Warning) 108 icon=E5MessageBox.Warning)
106 if not ok: 109 if not ok:
107 return 110 return
108 111
109 from .VirtualenvInterpreterSelectionDialog import \ 112 if not venvInterpreter:
110 VirtualenvInterpreterSelectionDialog 113 from .VirtualenvInterpreterSelectionDialog import \
111 dlg = VirtualenvInterpreterSelectionDialog(venvName, venvDirectory) 114 VirtualenvInterpreterSelectionDialog
112 if dlg.exec_() == QDialog.Accepted: 115 dlg = VirtualenvInterpreterSelectionDialog(venvName, venvDirectory)
113 venvExe = dlg.getData() 116 if dlg.exec_() == QDialog.Accepted:
114 self.__virtualEnvironmentInterpreters[venvName] = venvExe 117 venvInterpreter = dlg.getData()
118
119 if venvInterpreter:
120 self.__virtualEnvironmentInterpreters[venvName] = venvInterpreter
115 self.__virtualEnvironments[venvName] = venvDirectory 121 self.__virtualEnvironments[venvName] = venvDirectory
116 122
117 self.__updateSettings() 123 self.__updateSettings()
118 124
119 def deleteVirtualEnv(self, venvName): 125 if self.__virtualenvManagerDialog:
120 """ 126 self.__virtualenvManagerDialog.refresh()
121 Public method to delete a virtual environment from disk. 127
122 128 def setVirtualEnv(self, venvName, venvDirectory, venvInterpreter=""):
129 """
130 Public method to change a virtual environment.
131
132 @param venvName logical name of the virtual environment
133 @type str
134 @param venvDirectory directory of the virtual environment
135 @type str
136 @param venvInterpreter interpreter of the virtual environment
137 """
138 if venvName not in self.__virtualEnvironments:
139 E5MessageBox.yesNo(
140 None,
141 self.tr("Change Virtual Environment"),
142 self.tr("""A virtual environment named <b>{0}</b> does not"""
143 """ exist. Aborting!""")
144 .format(venvName),
145 icon=E5MessageBox.Warning)
146 return
147
148 self.__virtualEnvironmentInterpreters[venvName] = venvInterpreter
149 self.__virtualEnvironments[venvName] = venvDirectory
150
151 self.__updateSettings()
152
153 if self.__virtualenvManagerDialog:
154 self.__virtualenvManagerDialog.refresh()
155
156 def renameVirtualEnv(self, oldVenvName, venvName, venvDirectory,
157 venvInterpreter):
158 """
159 Public method to substitute a virtual environment entry with a new
160 name.
161
162 @param oldVenvName old name of the virtual environment
163 @type str
123 @param venvName logical name for the virtual environment 164 @param venvName logical name for the virtual environment
124 @type str 165 @type str
125 """ 166 @param venvDirectory directory of the virtual environment
126 if venvName in self.__virtualEnvironments: 167 @type str
127 ok = E5MessageBox.yesNo( 168 @param venvInterpreter interpreter of the virtual environment
128 None, 169 """
129 self.tr("Delete Virtual Environment"), 170 if oldVenvName not in self.__virtualEnvironments:
130 self.tr("""Do you really want to delete the virtual""" 171 E5MessageBox.yesNo(
131 """ environment <b>{0}</b>?<br>Path: {1}""") 172 None,
132 .format(venvName, self.__virtualEnvironments[venvName])) 173 self.tr("Rename Virtual Environment"),
133 if ok: 174 self.tr("""A virtual environment named <b>{0}</b> does not"""
134 shutil.rmtree(self.__virtualEnvironments[venvName], True) 175 """ exist. Aborting!""")
135 del self.__virtualEnvironments[venvName] 176 .format(oldVenvName),
136 del self.__virtualEnvironmentInterpreters[venvName] 177 icon=E5MessageBox.Warning)
178 return
179
180 del self.__virtualEnvironments[oldVenvName]
181 del self.__virtualEnvironmentInterpreters[oldVenvName]
182 self.addVirtualEnv(venvName, venvDirectory, venvInterpreter)
183
184 def deleteVirtualEnvs(self, venvNames):
185 """
186 Public method to delete virtual environments from the list and disk.
187
188 @param venvNames list of logical names for the virtual environments
189 @type list of str
190 """
191 venvMessages = []
192 for venvName in venvNames:
193 if venvName in self.__virtualEnvironments and \
194 bool(self.__virtualEnvironments[venvName]):
195 venvMessages.append(self.tr("{0} - {1}").format(
196 venvName, self.__virtualEnvironments[venvName]))
197 if venvMessages:
198 from UI.DeleteFilesConfirmationDialog import \
199 DeleteFilesConfirmationDialog
200 dlg = DeleteFilesConfirmationDialog(
201 None,
202 self.tr("Delete Virtual Environments"),
203 self.tr("""Do you really want to delete these virtual"""
204 """ environments?"""),
205 venvMessages
206 )
207 if dlg.exec_() == QDialog.Accepted:
208 if venvName in self.__virtualEnvironments and \
209 bool(self.__virtualEnvironments[venvName]):
210 shutil.rmtree(self.__virtualEnvironments[venvName], True)
211 del self.__virtualEnvironments[venvName]
212 del self.__virtualEnvironmentInterpreters[venvName]
137 213
138 self.__updateSettings() 214 self.__updateSettings()
215
216 if self.__virtualenvManagerDialog:
217 self.__virtualenvManagerDialog.refresh()
218
219 def removeVirtualEnvs(self, venvNames):
220 """
221 Public method to delete virtuals environment from the list.
222
223 @param venvNames list of logical names for the virtual environments
224 @type list of str
225 """
226 venvMessages = []
227 for venvName in venvNames:
228 if venvName in self.__virtualEnvironments and \
229 bool(self.__virtualEnvironments[venvName]):
230 venvMessages.append(self.tr("{0} - {1}").format(
231 venvName, self.__virtualEnvironments[venvName]))
232 if venvMessages:
233 from UI.DeleteFilesConfirmationDialog import \
234 DeleteFilesConfirmationDialog
235 dlg = DeleteFilesConfirmationDialog(
236 None,
237 self.tr("Remove Virtual Environments"),
238 self.tr("""Do you really want to remove these virtual"""
239 """ environments?"""),
240 venvMessages
241 )
242 if dlg.exec_() == QDialog.Accepted:
243 if venvName in self.__virtualEnvironments and \
244 bool(self.__virtualEnvironments[venvName]):
245 del self.__virtualEnvironments[venvName]
246 del self.__virtualEnvironmentInterpreters[venvName]
247
248 self.__updateSettings()
249
250 if self.__virtualenvManagerDialog:
251 self.__virtualenvManagerDialog.refresh()
252
253 def getEnvironmentEntries(self):
254 """
255 Public method to a dictionary containing the defined virtual
256 environment entries.
257
258 @return dictionary containing tuples of the environment path and
259 the associated interpreter
260 @rtype dict of (str, str)
261 """
262 environments = {}
263 for venvName in self.__virtualEnvironments:
264 environments[venvName] = (
265 self.__virtualEnvironments[venvName],
266 self.__virtualEnvironmentInterpreters[venvName],
267 )
268
269 return environments
270
271 @pyqtSlot()
272 def showVirtualenvManagerDialog(self):
273 """
274 Public slot to show the virtual environment manager dialog.
275 """
276 if self.__virtualenvManagerDialog is None:
277 from .VirtualenvManagerDialog import VirtualenvManagerDialog
278 self.__virtualenvManagerDialog = VirtualenvManagerDialog(
279 self, self.__ui)
280
281 self.__virtualenvManagerDialog.show()
282
283 def shutdown(self):
284 """
285 Public method to shutdown the manager.
286 """
287 if self.__virtualenvManagerDialog is not None:
288 self.__virtualenvManagerDialog.close()
289 self.__virtualenvManagerDialog = None
290
291 def isUnique(self, venvName):
292 """
293 Public method to check, if the give logical name is unique.
294
295 @param venvName logical name for the virtual environment
296 @type str
297 @return flag indicating uniqueness
298 @rtype bool
299 """
300 return venvName not in self.__virtualEnvironments
301
302 def getVirtualenvInterpreter(self, venvName):
303 """
304 Public method to get the interpreter for a virtual environment.
305
306 @param venvName logical name for the virtual environment
307 @type str
308 @return interpreter path
309 @rtype str
310 """
311 if venvName in self.__virtualEnvironmentInterpreters:
312 return self.__virtualEnvironmentInterpreters[venvName]
313 else:
314 return ""
315
316 def getVirtualenvNames(self):
317 """
318 Public method to get a list of defined virtual environments.
319
320 @return list of defined virtual environments
321 @rtype list of str
322 """
323 return list(self.__virtualEnvironmentInterpreters.keys())

eric ide

mercurial