src/eric7/SystemUtilities/PythonUtilities.py

branch
eric7
changeset 10836
dc7f25f2f7e4
parent 10834
6f5cb518cf13
child 11090
f5f5f5803935
--- a/src/eric7/SystemUtilities/PythonUtilities.py	Tue Jul 09 14:09:40 2024 +0200
+++ b/src/eric7/SystemUtilities/PythonUtilities.py	Tue Jul 09 17:20:18 2024 +0200
@@ -71,39 +71,33 @@
     return sys.hexversion >> 16
 
 
-# TODO: change this to a dummy function that always return the int value 3.
-# TODO: change eric-ide sources to not use this function anymore.
-def determinePythonVersion(filename, source, editor=None):
+def isPythonSource(filename, source, editor=None):
     """
-    Function to determine the python version of a given file.
+    Function to check for a Python source code file.
 
     @param filename name of the file with extension
     @type str
     @param source of the file
     @type str
     @param editor reference to the editor, if the file is opened already
-    @type Editor
-    @return Python version if file is Python3
-    @rtype int
+        (defaults to None)
+    @type Editor (optional)
+    @return flag indicating Python source code
+    @rtype bool
     """
     from eric7 import Preferences, Utilities
     from eric7.EricWidgets.EricApplication import ericApp
 
-    pyAssignment = {
-        "Python3": 3,
-        "MicroPython": 3,
-        "Cython": 3,
-    }
+    pythonEquivalents = ("Cython", "MicroPython", "Python3")
 
     if not editor:
         viewManager = ericApp().getObject("ViewManager")
         editor = viewManager.getOpenEditor(filename)
 
     # Maybe the user has changed the language
-    if editor and editor.getFileType() in pyAssignment:
-        return pyAssignment[editor.getFileType()]
+    if editor and editor.getFileType() in pythonEquivalents:
+        return True
 
-    pyVer = 0
     if filename:
         if not source and os.path.exists(filename):
             source = Utilities.readEncodedFile(filename)[0]
@@ -113,39 +107,34 @@
         project = ericApp().getObject("Project")
         basename = os.path.basename(filename)
 
-        if "FileType" in flags:
-            pyVer = pyAssignment.get(flags["FileType"], 0)
+        if "FileType" in flags and flags["FileType"] in pythonEquivalents:
+            return True
         elif project.isOpen() and project.isProjectFile(filename):
             language = project.getEditorLexerAssoc(basename)
             if not language:
                 language = Preferences.getEditorLexerAssoc(basename)
             if language == "Python3":
-                pyVer = pyAssignment[language]
+                return True
 
-        if pyVer:
-            # Skip the next tests
-            pass
-        elif (
+        if (
             Preferences.getProject("DeterminePyFromProject")
             and project.isOpen()
             and project.isProjectFile(filename)
             and ext in py3Ext
+            and project.getProjectLanguage() in pythonEquivalents
         ):
-            pyVer = pyAssignment.get(project.getProjectLanguage(), 0)
+            return True
         elif ext in py3Ext:
-            pyVer = 3
+            return True
         elif source:
             if isinstance(source, str):
                 line0 = source.splitlines()[0]
             else:
                 line0 = source[0]
             if line0.startswith("#!") and (("python3" in line0) or ("python" in line0)):
-                pyVer = 3
+                return True
 
-        if pyVer == 0 and ext in py3Ext:
-            pyVer = 3
-
-    return pyVer
+    return False
 
 
 def searchInterpreters(environments=None):

eric ide

mercurial