|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2018 - 2022 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a dialog to enter the interpreter for a virtual |
|
8 environment. |
|
9 """ |
|
10 |
|
11 import glob |
|
12 import os |
|
13 |
|
14 from PyQt6.QtCore import pyqtSlot |
|
15 from PyQt6.QtWidgets import QDialog, QDialogButtonBox |
|
16 |
|
17 from EricWidgets.EricPathPicker import EricPathPickerModes |
|
18 |
|
19 from .Ui_VirtualenvInterpreterSelectionDialog import ( |
|
20 Ui_VirtualenvInterpreterSelectionDialog |
|
21 ) |
|
22 |
|
23 import Globals |
|
24 |
|
25 |
|
26 class VirtualenvInterpreterSelectionDialog( |
|
27 QDialog, Ui_VirtualenvInterpreterSelectionDialog): |
|
28 """ |
|
29 Class implementing a dialog to enter the interpreter for a virtual |
|
30 environment. |
|
31 """ |
|
32 def __init__(self, venvName, venvDirectory, parent=None): |
|
33 """ |
|
34 Constructor |
|
35 |
|
36 @param venvName name for the virtual environment |
|
37 @type str |
|
38 @param venvDirectory directory of the virtual environment |
|
39 @type str |
|
40 @param parent reference to the parent widget |
|
41 @type QWidget |
|
42 """ |
|
43 super().__init__(parent) |
|
44 self.setupUi(self) |
|
45 |
|
46 self.pythonExecPicker.setMode(EricPathPickerModes.OPEN_FILE_MODE) |
|
47 self.pythonExecPicker.setWindowTitle( |
|
48 self.tr("Python Interpreter")) |
|
49 |
|
50 self.nameEdit.setText(venvName) |
|
51 |
|
52 if venvDirectory: |
|
53 # try to determine a Python interpreter name |
|
54 if Globals.isWindowsPlatform(): |
|
55 candidates = glob.glob( |
|
56 os.path.join(venvDirectory, "Scripts", "python*.exe") |
|
57 ) + glob.glob(os.path.join(venvDirectory, "python*.exe")) |
|
58 else: |
|
59 candidates = glob.glob( |
|
60 os.path.join(venvDirectory, "bin", "python*") |
|
61 ) |
|
62 self.pythonExecPicker.addItems(sorted(candidates)) |
|
63 self.pythonExecPicker.setText("") |
|
64 else: |
|
65 self.pythonExecPicker.setText(venvDirectory) |
|
66 |
|
67 def __updateOK(self): |
|
68 """ |
|
69 Private method to update the enabled status of the OK button. |
|
70 """ |
|
71 interpreterPath = self.pythonExecPicker.text() |
|
72 self.buttonBox.button(QDialogButtonBox.StandardButton.Ok).setEnabled( |
|
73 bool(interpreterPath) and |
|
74 os.path.isfile(interpreterPath) and |
|
75 os.access(interpreterPath, os.X_OK) |
|
76 ) |
|
77 |
|
78 @pyqtSlot(str) |
|
79 def on_pythonExecPicker_textChanged(self, txt): |
|
80 """ |
|
81 Private slot to handle changes of the entered Python interpreter path. |
|
82 |
|
83 @param txt entered Python interpreter path |
|
84 @type str |
|
85 """ |
|
86 self.__updateOK() |
|
87 |
|
88 def getData(self): |
|
89 """ |
|
90 Public method to get the entered data. |
|
91 |
|
92 @return path of the selected Python interpreter |
|
93 @rtype str |
|
94 """ |
|
95 return self.pythonExecPicker.text() |