src/eric7/SystemUtilities/PythonUtilities.py

branch
eric7
changeset 9624
b47dfa7a137d
child 9653
e67609152c5e
equal deleted inserted replaced
9623:9c1f429cb56b 9624:b47dfa7a137d
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2022 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing Python related utility functions.
8 """
9
10 import os
11 import sys
12 import sysconfig
13
14
15 def getPythonExecutable():
16 """
17 Function to determine the path of the (non-windowed) Python executable.
18
19 @return path of the Python executable
20 @rtype str
21 """
22 if sys.platform.startswith("linux"):
23 return sys.executable
24 elif sys.platform == "darwin":
25 return sys.executable.replace("pythonw", "python")
26 else:
27 return sys.executable.replace("pythonw.exe", "python.exe")
28
29
30 def getPythonLibraryDirectory():
31 """
32 Function to determine the path to Python's library directory.
33
34 @return path to the Python library directory
35 @rtype str
36 """
37 return sysconfig.get_path("platlib")
38
39
40 def getPythonScriptsDirectory():
41 """
42 Function to determine the path to Python's scripts directory.
43
44 @return path to the Python scripts directory
45 @rtype str
46 """
47 return sysconfig.get_path("scripts")
48
49
50 def getPythonLibPath():
51 """
52 Function to determine the path to Python's library.
53
54 @return path to the Python library (string)
55 """
56 return sysconfig.get_path("platstdlib")
57
58
59 def getPythonVersion():
60 """
61 Function to get the Python version (major, minor) as an integer value.
62
63 @return An integer representing major and minor version number (integer)
64 """
65 return sys.hexversion >> 16
66
67
68 def determinePythonVersion(filename, source, editor=None):
69 """
70 Function to determine the python version of a given file.
71
72 @param filename name of the file with extension (str)
73 @param source of the file (str)
74 @param editor reference to the editor, if the file is opened
75 already (Editor object)
76 @return Python version if file is Python3 (int)
77 """
78 from eric7 import Preferences, Utilities
79 from eric7.EricWidgets.EricApplication import ericApp
80
81 pyAssignment = {
82 "Python3": 3,
83 "MicroPython": 3,
84 "Cython": 3,
85 }
86
87 if not editor:
88 viewManager = ericApp().getObject("ViewManager")
89 editor = viewManager.getOpenEditor(filename)
90
91 # Maybe the user has changed the language
92 if editor and editor.getFileType() in pyAssignment:
93 return pyAssignment[editor.getFileType()]
94
95 pyVer = 0
96 if filename:
97 if not source:
98 source = Utilities.readEncodedFile(filename)[0]
99 flags = Utilities.extractFlags(source)
100 ext = os.path.splitext(filename)[1]
101 py3Ext = Preferences.getPython("Python3Extensions")
102 project = ericApp().getObject("Project")
103 basename = os.path.basename(filename)
104
105 if "FileType" in flags:
106 pyVer = pyAssignment.get(flags["FileType"], 0)
107 elif project.isOpen() and project.isProjectFile(filename):
108 language = project.getEditorLexerAssoc(basename)
109 if not language:
110 language = Preferences.getEditorLexerAssoc(basename)
111 if language == "Python3":
112 pyVer = pyAssignment[language]
113
114 if pyVer:
115 # Skip the next tests
116 pass
117 elif (
118 Preferences.getProject("DeterminePyFromProject")
119 and project.isOpen()
120 and project.isProjectFile(filename)
121 and ext in py3Ext
122 ):
123 pyVer = pyAssignment.get(project.getProjectLanguage(), 0)
124 elif ext in py3Ext:
125 pyVer = 3
126 elif source:
127 if isinstance(source, str):
128 line0 = source.splitlines()[0]
129 else:
130 line0 = source[0]
131 if line0.startswith("#!") and (("python3" in line0) or ("python" in line0)):
132 pyVer = 3
133
134 if pyVer == 0 and ext in py3Ext:
135 pyVer = 3
136
137 return pyVer

eric ide

mercurial