ProjectFlask/FlaskVirtualenvConfigurationDialog.py

changeset 52
caddf9f36f9f
child 60
02243723ac17
equal deleted inserted replaced
51:428c2bec2e8f 52:caddf9f36f9f
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2020 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing a dialog to configure a project specific virtual
8 environment.
9 """
10
11 import sys
12 import os
13
14 from PyQt5.QtCore import pyqtSlot
15 from PyQt5.QtWidgets import QDialog, QDialogButtonBox
16
17 from E5Gui.E5PathPicker import E5PathPickerModes
18
19 from .Ui_FlaskVirtualenvConfigurationDialog import (
20 Ui_FlaskVirtualenvConfigurationDialog
21 )
22
23 import Utilities
24
25
26 class FlaskVirtualenvConfigurationDialog(
27 QDialog, Ui_FlaskVirtualenvConfigurationDialog
28 ):
29 """
30 Class implementing a dialog to configure a project specific virtual
31 environment.
32
33 Note: This dialog is a simplified variant of the one found in the eric
34 package.
35 """
36 def __init__(self, projectPath, projectName, parent=None):
37 """
38 Constructor
39
40 @param projectPath directory path of the project
41 @type str
42 @param projectName name of the project
43 @type str
44 @param parent reference to the parent widget
45 @type QWidget
46 """
47 super(FlaskVirtualenvConfigurationDialog, self).__init__(parent)
48 self.setupUi(self)
49
50 self.targetDirectoryPicker.setMode(E5PathPickerModes.DirectoryMode)
51 self.targetDirectoryPicker.setWindowTitle(
52 self.tr("Virtualenv Target Directory"))
53 self.targetDirectoryPicker.setDefaultDirectory(projectPath)
54
55 self.pythonExecPicker.setMode(E5PathPickerModes.OpenFileMode)
56 self.pythonExecPicker.setWindowTitle(
57 self.tr("Python Interpreter"))
58 self.pythonExecPicker.setDefaultDirectory(
59 sys.executable.replace("w.exe", ".exe"))
60
61 mandatoryStyleSheet = "QLineEdit {border: 2px solid;}"
62 self.targetDirectoryPicker.setStyleSheet(mandatoryStyleSheet)
63 self.nameEdit.setStyleSheet(mandatoryStyleSheet)
64
65 # pre-populate some fields
66 self.nameEdit.setText("Project {0}".format(projectName))
67 self.targetDirectoryPicker.setText(os.path.join(projectPath, "venv"))
68
69 msh = self.minimumSizeHint()
70 self.resize(max(self.width(), msh.width()), msh.height())
71
72 def __updateOK(self):
73 """
74 Private method to update the enabled status of the OK button.
75 """
76 self.buttonBox.button(QDialogButtonBox.Ok).setEnabled(
77 bool(self.targetDirectoryPicker.text()) and
78 bool(self.nameEdit.text())
79 )
80
81 @pyqtSlot(str)
82 def on_nameEdit_textChanged(self, txt):
83 """
84 Private slot handling a change of the virtual environment name.
85
86 @param txt name of the virtual environment
87 @type str
88 """
89 self.__updateOK()
90
91 @pyqtSlot(str)
92 def on_targetDirectoryPicker_textChanged(self, txt):
93 """
94 Private slot handling a change of the target directory.
95
96 @param txt target directory
97 @type str
98 """
99 self.__updateOK()
100
101 def __generateTargetDir(self):
102 """
103 Private method to generate a valid target directory path.
104
105 @return target directory path
106 @rtype str
107 """
108 targetDirectory = Utilities.toNativeSeparators(
109 self.targetDirectoryPicker.text())
110 if not os.path.isabs(targetDirectory):
111 targetDirectory = os.path.join(os.path.expanduser("~"),
112 targetDirectory)
113 return targetDirectory
114
115 def getData(self):
116 """
117 Public method to retrieve the dialog data.
118
119 Note: This method returns a data structure compatible with the one
120 returned by the eric virtual environment configuration dialog.
121
122 @return dictionary containing the data for the environment to be
123 created. The keys for both variants are 'arguments' containing the
124 command line arguments, 'logicalName' containing the environment
125 name to be used with the virtual environment manager and 'envType'
126 containing the environment type (always pyvenv). The pyvenv
127 specific keys are 'openTarget' containg a flag to open the target
128 directory after creation (always False), 'createLog' containing a
129 flag to write a log file (always False), 'createScript' containing
130 a flag to write a script (always False), 'targetDirectory'
131 containing the target directory and 'pythonExe' containing the
132 Python interpreter to be used.
133 @rtype dict
134 """
135 resultDict = {
136 "arguments": [self.__generateTargetDir()],
137 "logicalName": self.nameEdit.text(),
138 "envType": "pyvenv",
139 "openTarget": False,
140 "createLog": False,
141 "createScript": False,
142 "targetDirectory": self.__generateTargetDir(),
143 "pythonExe": Utilities.toNativeSeparators(
144 self.pythonExecPicker.text()),
145 }
146
147 return resultDict

eric ide

mercurial