--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/VirtualEnv/VirtualenvManager.py Sat Jun 09 17:19:37 2018 +0200 @@ -0,0 +1,138 @@ +# -*- coding: utf-8 -*- + +# Copyright (c) 2018 Detlev Offenbach <detlev@die-offenbachs.de> +# + +""" +Module implementing a class to manage Python virtual environments. +""" + +from __future__ import unicode_literals + +import os +import sys +import shutil + +from PyQt5.QtCore import pyqtSlot, QObject +from PyQt5.QtWidgets import QDialog + +from E5Gui import E5MessageBox + +import Preferences + + +class VirtualenvManager(QObject): + """ + Class implementing an object to manage Python virtual environments. + """ + def __init__(self, parent=None): + """ + Constructor + + @param parent reference to the parent object + @type QWidget + """ + super(VirtualenvManager, self).__init__(parent) + + self.__ui = parent + + self.__virtualEnvironments = {} + self.__virtualEnvironmentInterpreters = {} + environments = Preferences.toDict(Preferences.Prefs.settings.value( + "PyVenv/Environments", {})) + interpreters = Preferences.toDict(Preferences.Prefs.settings.value( + "PyVenv/Interpreters", {})) + for venvName, venvExe in interpreters.items(): + # remove all environments, that don't exist anymore + if os.access(venvExe, os.X_OK): + self.__virtualEnvironmentInterpreters[venvName] = venvExe + self.__virtualEnvironments[venvName] = environments[venvName] + + defaultPy = sys.executable.replace("w.exe", ".exe") + if defaultPy not in self.__virtualEnvironmentInterpreters.values(): + self.__virtualEnvironmentInterpreters["<default>"] = defaultPy + self.__virtualEnvironments["<default>"] = "" + + self.__updateSettings() + + def __updateSettings(self): + """ + Private slot to save the virtual environments. + """ + Preferences.Prefs.settings.setValue( + "PyVenv/Environments", self.__virtualEnvironments) + Preferences.Prefs.settings.setValue( + "PyVenv/Interpreters", self.__virtualEnvironmentInterpreters) + + @pyqtSlot() + def createVirtualEnv(self): + """ + Public slot to create a new virtual environment. + """ + from .VirtualenvConfigurationDialog import \ + VirtualenvConfigurationDialog + + dlg = VirtualenvConfigurationDialog() + if dlg.exec_() == QDialog.Accepted: + (pyvenv, args, name, openTarget, createLog, createScript, + targetDir, interpreter) = dlg.getData() + + # now do the call + from .VirtualenvExecDialog import VirtualenvExecDialog + dia = VirtualenvExecDialog(pyvenv, targetDir, name, openTarget, + createLog, createScript, interpreter, + self) + dia.show() + dia.start(args) + dia.exec_() + + def addVirtualEnv(self, venvName, venvDirectory): + """ + Public method to add a virtual environment. + + @param venvName logical name for the virtual environment + @type str + @param venvDirectory directory of the virtual envoronment + @type str + """ + if venvName in self.__virtualEnvironments: + ok = E5MessageBox.yesNo( + None, + self.tr("Add Virtual Environment"), + self.tr("""A virtual environment named <b>{0}</b> exists""" + """ already. Shall it be replaced?""") + .format(venvName), + icon=E5MessageBox.Warning) + if not ok: + return + + from .VirtualenvInterpreterSelectionDialog import \ + VirtualenvInterpreterSelectionDialog + dlg = VirtualenvInterpreterSelectionDialog(venvName, venvDirectory) + if dlg.exec_() == QDialog.Accepted: + venvExe = dlg.getData() + self.__virtualEnvironmentInterpreters[venvName] = venvExe + self.__virtualEnvironments[venvName] = venvDirectory + + self.__updateSettings() + + def deleteVirtualEnv(self, venvName): + """ + Public method to delete a virtual environment from disk. + + @param venvName logical name for the virtual environment + @type str + """ + 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()