src/eric7/SystemUtilities/PythonUtilities.py

branch
eric7
changeset 10836
dc7f25f2f7e4
parent 10834
6f5cb518cf13
child 11090
f5f5f5803935
equal deleted inserted replaced
10835:9117c08e4707 10836:dc7f25f2f7e4
69 @rtype int 69 @rtype int
70 """ 70 """
71 return sys.hexversion >> 16 71 return sys.hexversion >> 16
72 72
73 73
74 # TODO: change this to a dummy function that always return the int value 3. 74 def isPythonSource(filename, source, editor=None):
75 # TODO: change eric-ide sources to not use this function anymore. 75 """
76 def determinePythonVersion(filename, source, editor=None): 76 Function to check for a Python source code file.
77 """
78 Function to determine the python version of a given file.
79 77
80 @param filename name of the file with extension 78 @param filename name of the file with extension
81 @type str 79 @type str
82 @param source of the file 80 @param source of the file
83 @type str 81 @type str
84 @param editor reference to the editor, if the file is opened already 82 @param editor reference to the editor, if the file is opened already
85 @type Editor 83 (defaults to None)
86 @return Python version if file is Python3 84 @type Editor (optional)
87 @rtype int 85 @return flag indicating Python source code
86 @rtype bool
88 """ 87 """
89 from eric7 import Preferences, Utilities 88 from eric7 import Preferences, Utilities
90 from eric7.EricWidgets.EricApplication import ericApp 89 from eric7.EricWidgets.EricApplication import ericApp
91 90
92 pyAssignment = { 91 pythonEquivalents = ("Cython", "MicroPython", "Python3")
93 "Python3": 3,
94 "MicroPython": 3,
95 "Cython": 3,
96 }
97 92
98 if not editor: 93 if not editor:
99 viewManager = ericApp().getObject("ViewManager") 94 viewManager = ericApp().getObject("ViewManager")
100 editor = viewManager.getOpenEditor(filename) 95 editor = viewManager.getOpenEditor(filename)
101 96
102 # Maybe the user has changed the language 97 # Maybe the user has changed the language
103 if editor and editor.getFileType() in pyAssignment: 98 if editor and editor.getFileType() in pythonEquivalents:
104 return pyAssignment[editor.getFileType()] 99 return True
105 100
106 pyVer = 0
107 if filename: 101 if filename:
108 if not source and os.path.exists(filename): 102 if not source and os.path.exists(filename):
109 source = Utilities.readEncodedFile(filename)[0] 103 source = Utilities.readEncodedFile(filename)[0]
110 flags = Utilities.extractFlags(source) 104 flags = Utilities.extractFlags(source)
111 ext = os.path.splitext(filename)[1] 105 ext = os.path.splitext(filename)[1]
112 py3Ext = Preferences.getPython("Python3Extensions") 106 py3Ext = Preferences.getPython("Python3Extensions")
113 project = ericApp().getObject("Project") 107 project = ericApp().getObject("Project")
114 basename = os.path.basename(filename) 108 basename = os.path.basename(filename)
115 109
116 if "FileType" in flags: 110 if "FileType" in flags and flags["FileType"] in pythonEquivalents:
117 pyVer = pyAssignment.get(flags["FileType"], 0) 111 return True
118 elif project.isOpen() and project.isProjectFile(filename): 112 elif project.isOpen() and project.isProjectFile(filename):
119 language = project.getEditorLexerAssoc(basename) 113 language = project.getEditorLexerAssoc(basename)
120 if not language: 114 if not language:
121 language = Preferences.getEditorLexerAssoc(basename) 115 language = Preferences.getEditorLexerAssoc(basename)
122 if language == "Python3": 116 if language == "Python3":
123 pyVer = pyAssignment[language] 117 return True
124 118
125 if pyVer: 119 if (
126 # Skip the next tests
127 pass
128 elif (
129 Preferences.getProject("DeterminePyFromProject") 120 Preferences.getProject("DeterminePyFromProject")
130 and project.isOpen() 121 and project.isOpen()
131 and project.isProjectFile(filename) 122 and project.isProjectFile(filename)
132 and ext in py3Ext 123 and ext in py3Ext
124 and project.getProjectLanguage() in pythonEquivalents
133 ): 125 ):
134 pyVer = pyAssignment.get(project.getProjectLanguage(), 0) 126 return True
135 elif ext in py3Ext: 127 elif ext in py3Ext:
136 pyVer = 3 128 return True
137 elif source: 129 elif source:
138 if isinstance(source, str): 130 if isinstance(source, str):
139 line0 = source.splitlines()[0] 131 line0 = source.splitlines()[0]
140 else: 132 else:
141 line0 = source[0] 133 line0 = source[0]
142 if line0.startswith("#!") and (("python3" in line0) or ("python" in line0)): 134 if line0.startswith("#!") and (("python3" in line0) or ("python" in line0)):
143 pyVer = 3 135 return True
144 136
145 if pyVer == 0 and ext in py3Ext: 137 return False
146 pyVer = 3
147
148 return pyVer
149 138
150 139
151 def searchInterpreters(environments=None): 140 def searchInterpreters(environments=None):
152 """ 141 """
153 Function to determine a list of all Python interpreters available via the 142 Function to determine a list of all Python interpreters available via the

eric ide

mercurial