ProjectFlask/FlaskVirtualenvConfigurationDialog.py

Sat, 31 Dec 2022 16:27:49 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sat, 31 Dec 2022 16:27:49 +0100
branch
eric7
changeset 82
bb14c648099b
parent 80
662e5eb1ba8b
child 83
d8788dc3442f
permissions
-rw-r--r--

Updated copyright for 2023.

# -*- coding: utf-8 -*-

# Copyright (c) 2020 - 2023 Detlev Offenbach <detlev@die-offenbachs.de>
#

"""
Module implementing a dialog to configure a project specific virtual
environment.
"""

import os
import sys

from PyQt6.QtCore import pyqtSlot
from PyQt6.QtWidgets import QDialog, QDialogButtonBox

from eric7.EricWidgets.EricPathPicker import EricPathPickerModes

try:
    from eric7.SystemUtilities.FileSystemUtilities import toNativeSeparators
except ImportError:
    # imports for eric < 23.1
    from eric7.Utilities import toNativeSeparators

from .Ui_FlaskVirtualenvConfigurationDialog import Ui_FlaskVirtualenvConfigurationDialog


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().__init__(parent)
        self.setupUi(self)

        self.targetDirectoryPicker.setMode(EricPathPickerModes.DIRECTORY_MODE)
        self.targetDirectoryPicker.setWindowTitle(
            self.tr("Virtualenv Target Directory")
        )
        self.targetDirectoryPicker.setDefaultDirectory(projectPath)

        self.pythonExecPicker.setMode(EricPathPickerModes.OPEN_FILE_MODE)
        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.StandardButton.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 = 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": toNativeSeparators(self.pythonExecPicker.text()),
        }

        return resultDict

eric ide

mercurial