--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ProjectFlask/FlaskVirtualenvConfigurationDialog.py Sat Dec 05 17:18:17 2020 +0100 @@ -0,0 +1,147 @@ +# -*- coding: utf-8 -*- + +# Copyright (c) 2020 Detlev Offenbach <detlev@die-offenbachs.de> +# + +""" +Module implementing a dialog to configure a project specific virtual +environment. +""" + +import sys +import os + +from PyQt5.QtCore import pyqtSlot +from PyQt5.QtWidgets import QDialog, QDialogButtonBox + +from E5Gui.E5PathPicker import E5PathPickerModes + +from .Ui_FlaskVirtualenvConfigurationDialog import ( + Ui_FlaskVirtualenvConfigurationDialog +) + +import Utilities + + +class FlaskVirtualenvConfigurationDialog( + QDialog, Ui_FlaskVirtualenvConfigurationDialog +): + """ + Class implementing a dialog to configure a project specific virtual + environment. + + Note: This dialog is a simplified variant of the one found in the eric + package. + """ + def __init__(self, projectPath, projectName, parent=None): + """ + Constructor + + @param projectPath directory path of the project + @type str + @param projectName name of the project + @type str + @param parent reference to the parent widget + @type QWidget + """ + super(FlaskVirtualenvConfigurationDialog, self).__init__(parent) + self.setupUi(self) + + self.targetDirectoryPicker.setMode(E5PathPickerModes.DirectoryMode) + self.targetDirectoryPicker.setWindowTitle( + self.tr("Virtualenv Target Directory")) + self.targetDirectoryPicker.setDefaultDirectory(projectPath) + + self.pythonExecPicker.setMode(E5PathPickerModes.OpenFileMode) + self.pythonExecPicker.setWindowTitle( + self.tr("Python Interpreter")) + self.pythonExecPicker.setDefaultDirectory( + sys.executable.replace("w.exe", ".exe")) + + mandatoryStyleSheet = "QLineEdit {border: 2px solid;}" + self.targetDirectoryPicker.setStyleSheet(mandatoryStyleSheet) + self.nameEdit.setStyleSheet(mandatoryStyleSheet) + + # pre-populate some fields + self.nameEdit.setText("Project {0}".format(projectName)) + self.targetDirectoryPicker.setText(os.path.join(projectPath, "venv")) + + msh = self.minimumSizeHint() + self.resize(max(self.width(), msh.width()), msh.height()) + + def __updateOK(self): + """ + Private method to update the enabled status of the OK button. + """ + self.buttonBox.button(QDialogButtonBox.Ok).setEnabled( + bool(self.targetDirectoryPicker.text()) and + bool(self.nameEdit.text()) + ) + + @pyqtSlot(str) + def on_nameEdit_textChanged(self, txt): + """ + Private slot handling a change of the virtual environment name. + + @param txt name of the virtual environment + @type str + """ + self.__updateOK() + + @pyqtSlot(str) + def on_targetDirectoryPicker_textChanged(self, txt): + """ + Private slot handling a change of the target directory. + + @param txt target directory + @type str + """ + self.__updateOK() + + def __generateTargetDir(self): + """ + Private method to generate a valid target directory path. + + @return target directory path + @rtype str + """ + targetDirectory = Utilities.toNativeSeparators( + self.targetDirectoryPicker.text()) + if not os.path.isabs(targetDirectory): + targetDirectory = os.path.join(os.path.expanduser("~"), + targetDirectory) + return targetDirectory + + def getData(self): + """ + Public method to retrieve the dialog data. + + Note: This method returns a data structure compatible with the one + returned by the eric virtual environment configuration dialog. + + @return dictionary containing the data for the environment to be + created. The keys for both variants are 'arguments' containing the + command line arguments, 'logicalName' containing the environment + name to be used with the virtual environment manager and 'envType' + containing the environment type (always pyvenv). The pyvenv + specific keys are 'openTarget' containg a flag to open the target + directory after creation (always False), 'createLog' containing a + flag to write a log file (always False), 'createScript' containing + a flag to write a script (always False), 'targetDirectory' + containing the target directory and 'pythonExe' containing the + Python interpreter to be used. + @rtype dict + """ + resultDict = { + "arguments": [self.__generateTargetDir()], + "logicalName": self.nameEdit.text(), + "envType": "pyvenv", + "openTarget": False, + "createLog": False, + "createScript": False, + "targetDirectory": self.__generateTargetDir(), + "pythonExe": Utilities.toNativeSeparators( + self.pythonExecPicker.text()), + } + + return resultDict