diff -r c6af560e0039 -r 104ee21d765d VirtualEnv/VirtualenvManager.py --- a/VirtualEnv/VirtualenvManager.py Sat Jun 09 17:19:37 2018 +0200 +++ b/VirtualEnv/VirtualenvManager.py Sun Jun 10 16:55:39 2018 +0200 @@ -54,6 +54,8 @@ self.__virtualEnvironments["<default>"] = "" self.__updateSettings() + + self.__virtualenvManagerDialog = None def __updateSettings(self): """ @@ -86,14 +88,15 @@ dia.start(args) dia.exec_() - def addVirtualEnv(self, venvName, venvDirectory): + def addVirtualEnv(self, venvName, venvDirectory, venvInterpreter=""): """ Public method to add a virtual environment. @param venvName logical name for the virtual environment @type str - @param venvDirectory directory of the virtual envoronment + @param venvDirectory directory of the virtual environment @type str + @param venvInterpreter interpreter of the virtual environment """ if venvName in self.__virtualEnvironments: ok = E5MessageBox.yesNo( @@ -106,33 +109,215 @@ if not ok: return - from .VirtualenvInterpreterSelectionDialog import \ - VirtualenvInterpreterSelectionDialog - dlg = VirtualenvInterpreterSelectionDialog(venvName, venvDirectory) - if dlg.exec_() == QDialog.Accepted: - venvExe = dlg.getData() - self.__virtualEnvironmentInterpreters[venvName] = venvExe + if not venvInterpreter: + from .VirtualenvInterpreterSelectionDialog import \ + VirtualenvInterpreterSelectionDialog + dlg = VirtualenvInterpreterSelectionDialog(venvName, venvDirectory) + if dlg.exec_() == QDialog.Accepted: + venvInterpreter = dlg.getData() + + if venvInterpreter: + self.__virtualEnvironmentInterpreters[venvName] = venvInterpreter self.__virtualEnvironments[venvName] = venvDirectory self.__updateSettings() + + if self.__virtualenvManagerDialog: + self.__virtualenvManagerDialog.refresh() - def deleteVirtualEnv(self, venvName): + def setVirtualEnv(self, venvName, venvDirectory, venvInterpreter=""): + """ + Public method to change a virtual environment. + + @param venvName logical name of the virtual environment + @type str + @param venvDirectory directory of the virtual environment + @type str + @param venvInterpreter interpreter of the virtual environment + """ + if venvName not in self.__virtualEnvironments: + E5MessageBox.yesNo( + None, + self.tr("Change Virtual Environment"), + self.tr("""A virtual environment named <b>{0}</b> does not""" + """ exist. Aborting!""") + .format(venvName), + icon=E5MessageBox.Warning) + return + + self.__virtualEnvironmentInterpreters[venvName] = venvInterpreter + self.__virtualEnvironments[venvName] = venvDirectory + + self.__updateSettings() + + if self.__virtualenvManagerDialog: + self.__virtualenvManagerDialog.refresh() + + def renameVirtualEnv(self, oldVenvName, venvName, venvDirectory, + venvInterpreter): + """ + Public method to substitute a virtual environment entry with a new + name. + + @param oldVenvName old name of the virtual environment + @type str + @param venvName logical name for the virtual environment + @type str + @param venvDirectory directory of the virtual environment + @type str + @param venvInterpreter interpreter of the virtual environment + """ + if oldVenvName not in self.__virtualEnvironments: + E5MessageBox.yesNo( + None, + self.tr("Rename Virtual Environment"), + self.tr("""A virtual environment named <b>{0}</b> does not""" + """ exist. Aborting!""") + .format(oldVenvName), + icon=E5MessageBox.Warning) + return + + del self.__virtualEnvironments[oldVenvName] + del self.__virtualEnvironmentInterpreters[oldVenvName] + self.addVirtualEnv(venvName, venvDirectory, venvInterpreter) + + def deleteVirtualEnvs(self, venvNames): + """ + Public method to delete virtual environments from the list and disk. + + @param venvNames list of logical names for the virtual environments + @type list of str """ - Public method to delete a virtual environment from disk. + venvMessages = [] + for venvName in venvNames: + if venvName in self.__virtualEnvironments and \ + bool(self.__virtualEnvironments[venvName]): + venvMessages.append(self.tr("{0} - {1}").format( + venvName, self.__virtualEnvironments[venvName])) + if venvMessages: + from UI.DeleteFilesConfirmationDialog import \ + DeleteFilesConfirmationDialog + dlg = DeleteFilesConfirmationDialog( + None, + self.tr("Delete Virtual Environments"), + self.tr("""Do you really want to delete these virtual""" + """ environments?"""), + venvMessages + ) + if dlg.exec_() == QDialog.Accepted: + if venvName in self.__virtualEnvironments and \ + bool(self.__virtualEnvironments[venvName]): + shutil.rmtree(self.__virtualEnvironments[venvName], True) + del self.__virtualEnvironments[venvName] + del self.__virtualEnvironmentInterpreters[venvName] + + self.__updateSettings() + + if self.__virtualenvManagerDialog: + self.__virtualenvManagerDialog.refresh() + + def removeVirtualEnvs(self, venvNames): + """ + Public method to delete virtuals environment from the list. + + @param venvNames list of logical names for the virtual environments + @type list of str + """ + venvMessages = [] + for venvName in venvNames: + if venvName in self.__virtualEnvironments and \ + bool(self.__virtualEnvironments[venvName]): + venvMessages.append(self.tr("{0} - {1}").format( + venvName, self.__virtualEnvironments[venvName])) + if venvMessages: + from UI.DeleteFilesConfirmationDialog import \ + DeleteFilesConfirmationDialog + dlg = DeleteFilesConfirmationDialog( + None, + self.tr("Remove Virtual Environments"), + self.tr("""Do you really want to remove these virtual""" + """ environments?"""), + venvMessages + ) + if dlg.exec_() == QDialog.Accepted: + if venvName in self.__virtualEnvironments and \ + bool(self.__virtualEnvironments[venvName]): + del self.__virtualEnvironments[venvName] + del self.__virtualEnvironmentInterpreters[venvName] + + self.__updateSettings() + + if self.__virtualenvManagerDialog: + self.__virtualenvManagerDialog.refresh() + + def getEnvironmentEntries(self): + """ + Public method to a dictionary containing the defined virtual + environment entries. + + @return dictionary containing tuples of the environment path and + the associated interpreter + @rtype dict of (str, str) + """ + environments = {} + for venvName in self.__virtualEnvironments: + environments[venvName] = ( + self.__virtualEnvironments[venvName], + self.__virtualEnvironmentInterpreters[venvName], + ) + + return environments + + @pyqtSlot() + def showVirtualenvManagerDialog(self): + """ + Public slot to show the virtual environment manager dialog. + """ + if self.__virtualenvManagerDialog is None: + from .VirtualenvManagerDialog import VirtualenvManagerDialog + self.__virtualenvManagerDialog = VirtualenvManagerDialog( + self, self.__ui) + + self.__virtualenvManagerDialog.show() + + def shutdown(self): + """ + Public method to shutdown the manager. + """ + if self.__virtualenvManagerDialog is not None: + self.__virtualenvManagerDialog.close() + self.__virtualenvManagerDialog = None + + def isUnique(self, venvName): + """ + Public method to check, if the give logical name is unique. @param venvName logical name for the virtual environment @type str + @return flag indicating uniqueness + @rtype bool """ - if venvName in self.__virtualEnvironments: - ok = E5MessageBox.yesNo( - None, - self.tr("Delete Virtual Environment"), - self.tr("""Do you really want to delete the virtual""" - """ environment <b>{0}</b>?<br>Path: {1}""") - .format(venvName, self.__virtualEnvironments[venvName])) - if ok: - shutil.rmtree(self.__virtualEnvironments[venvName], True) - del self.__virtualEnvironments[venvName] - del self.__virtualEnvironmentInterpreters[venvName] - - self.__updateSettings() + return venvName not in self.__virtualEnvironments + + def getVirtualenvInterpreter(self, venvName): + """ + Public method to get the interpreter for a virtual environment. + + @param venvName logical name for the virtual environment + @type str + @return interpreter path + @rtype str + """ + if venvName in self.__virtualEnvironmentInterpreters: + return self.__virtualEnvironmentInterpreters[venvName] + else: + return "" + + def getVirtualenvNames(self): + """ + Public method to get a list of defined virtual environments. + + @return list of defined virtual environments + @rtype list of str + """ + return list(self.__virtualEnvironmentInterpreters.keys())