Refactored some code. eric7

Tue, 09 Jul 2024 17:20:18 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Tue, 09 Jul 2024 17:20:18 +0200
branch
eric7
changeset 10836
dc7f25f2f7e4
parent 10835
9117c08e4707
child 10837
40cdaa4a847d

Refactored some code.

src/eric7/APIs/Python3/eric7.api file | annotate | diff | comparison | revisions
src/eric7/Documentation/Help/source.qch file | annotate | diff | comparison | revisions
src/eric7/Documentation/Help/source.qhp file | annotate | diff | comparison | revisions
src/eric7/Documentation/Source/eric7.QScintilla.Editor.html file | annotate | diff | comparison | revisions
src/eric7/Documentation/Source/eric7.SystemUtilities.PythonUtilities.html file | annotate | diff | comparison | revisions
src/eric7/Plugins/CheckerPlugins/SyntaxChecker/SyntaxCheckService.py file | annotate | diff | comparison | revisions
src/eric7/Plugins/PluginCodeStyleChecker.py file | annotate | diff | comparison | revisions
src/eric7/QScintilla/Editor.py file | annotate | diff | comparison | revisions
src/eric7/SystemUtilities/PythonUtilities.py file | annotate | diff | comparison | revisions
--- a/src/eric7/APIs/Python3/eric7.api	Tue Jul 09 14:09:40 2024 +0200
+++ b/src/eric7/APIs/Python3/eric7.api	Tue Jul 09 17:20:18 2024 +0200
@@ -10135,13 +10135,13 @@
 eric7.SystemUtilities.OSUtilities.win32_getRealName?4()
 eric7.SystemUtilities.PySideImporter.pySideVariant?7
 eric7.SystemUtilities.PySideImporter.ret?7
-eric7.SystemUtilities.PythonUtilities.determinePythonVersion?4(filename, source, editor=None)
 eric7.SystemUtilities.PythonUtilities.getExePath?4(branch, access, versionStr)
 eric7.SystemUtilities.PythonUtilities.getPythonExecutable?4()
 eric7.SystemUtilities.PythonUtilities.getPythonLibPath?4()
 eric7.SystemUtilities.PythonUtilities.getPythonLibraryDirectory?4()
 eric7.SystemUtilities.PythonUtilities.getPythonScriptsDirectory?4()
 eric7.SystemUtilities.PythonUtilities.getPythonVersion?4()
+eric7.SystemUtilities.PythonUtilities.isPythonSource?4(filename, source, editor=None)
 eric7.SystemUtilities.PythonUtilities.searchInterpreters?4(environments=None)
 eric7.SystemUtilities.QtUtilities.checkPyside?4(variant=2)
 eric7.SystemUtilities.QtUtilities.generatePyQtToolPath?4(toolname, alternatives=None)
Binary file src/eric7/Documentation/Help/source.qch has changed
--- a/src/eric7/Documentation/Help/source.qhp	Tue Jul 09 14:09:40 2024 +0200
+++ b/src/eric7/Documentation/Help/source.qhp	Tue Jul 09 17:20:18 2024 +0200
@@ -4671,7 +4671,6 @@
       <keyword name="Editor.__getEditorConfig" id="Editor.__getEditorConfig" ref="eric7.QScintilla.Editor.html#Editor.__getEditorConfig" />
       <keyword name="Editor.__getMacroName" id="Editor.__getMacroName" ref="eric7.QScintilla.Editor.html#Editor.__getMacroName" />
       <keyword name="Editor.__getOverrideValue" id="Editor.__getOverrideValue" ref="eric7.QScintilla.Editor.html#Editor.__getOverrideValue" />
-      <keyword name="Editor.__getPyVersion" id="Editor.__getPyVersion" ref="eric7.QScintilla.Editor.html#Editor.__getPyVersion" />
       <keyword name="Editor.__getSaveFileName" id="Editor.__getSaveFileName" ref="eric7.QScintilla.Editor.html#Editor.__getSaveFileName" />
       <keyword name="Editor.__ignoreSpellingAlways" id="Editor.__ignoreSpellingAlways" ref="eric7.QScintilla.Editor.html#Editor.__ignoreSpellingAlways" />
       <keyword name="Editor.__indentLine" id="Editor.__indentLine" ref="eric7.QScintilla.Editor.html#Editor.__indentLine" />
@@ -20346,7 +20345,6 @@
       <keyword name="decryptData" id="decryptData" ref="eric7.Utilities.crypto.py3AES.html#decryptData" />
       <keyword name="deepgetattr" id="deepgetattr" ref="eric7.Plugins.CheckerPlugins.CodeStyleChecker.Security.SecurityUtils.html#deepgetattr" />
       <keyword name="desktopName" id="desktopName" ref="eric7.SystemUtilities.DesktopUtilities.html#desktopName" />
-      <keyword name="determinePythonVersion" id="determinePythonVersion" ref="eric7.SystemUtilities.PythonUtilities.html#determinePythonVersion" />
       <keyword name="determineSourceType" id="determineSourceType" ref="eric7.Utilities.ClassBrowsers.__init__.html#determineSourceType" />
       <keyword name="deviceFileName" id="deviceFileName" ref="eric7.SystemUtilities.FileSystemUtilities.html#deviceFileName" />
       <keyword name="direntries" id="direntries" ref="eric7.SystemUtilities.FileSystemUtilities.html#direntries" />
@@ -20776,6 +20774,7 @@
       <keyword name="isNumber" id="isNumber" ref="eric7.Plugins.CheckerPlugins.CodeStyleChecker.AstUtilities.html#isNumber" />
       <keyword name="isPlainFileName" id="isPlainFileName" ref="eric7.SystemUtilities.FileSystemUtilities.html#isPlainFileName" />
       <keyword name="isPythonProgram" id="isPythonProgram" ref="eric7.DebugClients.Python.DebugUtilities.html#isPythonProgram" />
+      <keyword name="isPythonSource" id="isPythonSource" ref="eric7.SystemUtilities.PythonUtilities.html#isPythonSource" />
       <keyword name="isRemoteFileName" id="isRemoteFileName" ref="eric7.SystemUtilities.FileSystemUtilities.html#isRemoteFileName" />
       <keyword name="isString" id="isString" ref="eric7.Plugins.CheckerPlugins.CodeStyleChecker.AstUtilities.html#isString" />
       <keyword name="isSupportedType" id="isSupportedType" ref="eric7.Utilities.ClassBrowsers.__init__.html#isSupportedType" />
--- a/src/eric7/Documentation/Source/eric7.QScintilla.Editor.html	Tue Jul 09 14:09:40 2024 +0200
+++ b/src/eric7/Documentation/Source/eric7.QScintilla.Editor.html	Tue Jul 09 17:20:18 2024 +0200
@@ -462,10 +462,6 @@
 <td>Private method to get an override value for the current file type.</td>
 </tr>
 <tr>
-<td><a href="#Editor.__getPyVersion">__getPyVersion</a></td>
-<td>Private method to return the Python main version or 0 if it's not a Python file at all.</td>
-</tr>
-<tr>
 <td><a href="#Editor.__getSaveFileName">__getSaveFileName</a></td>
 <td>Private method to get the name of the file to be saved.</td>
 </tr>
@@ -2799,26 +2795,6 @@
 Any
 </dd>
 </dl>
-<a NAME="Editor.__getPyVersion" ID="Editor.__getPyVersion"></a>
-<h4>Editor.__getPyVersion</h4>
-<b>__getPyVersion</b>(<i></i>)
-<p>
-        Private method to return the Python main version or 0 if it's
-        not a Python file at all.
-</p>
-
-<dl>
-<dt>Return:</dt>
-<dd>
-Python version or 0 if it's not a Python file
-</dd>
-</dl>
-<dl>
-<dt>Return Type:</dt>
-<dd>
-int
-</dd>
-</dl>
 <a NAME="Editor.__getSaveFileName" ID="Editor.__getSaveFileName"></a>
 <h4>Editor.__getSaveFileName</h4>
 <b>__getSaveFileName</b>(<i>path=None, remote=False</i>)
--- a/src/eric7/Documentation/Source/eric7.SystemUtilities.PythonUtilities.html	Tue Jul 09 14:09:40 2024 +0200
+++ b/src/eric7/Documentation/Source/eric7.SystemUtilities.PythonUtilities.html	Tue Jul 09 17:20:18 2024 +0200
@@ -32,10 +32,6 @@
 <td>Function to determine a list of all Python interpreters available via the executable search path (i.e.</td>
 </tr>
 <tr>
-<td><a href="#determinePythonVersion">determinePythonVersion</a></td>
-<td>Function to determine the python version of a given file.</td>
-</tr>
-<tr>
 <td><a href="#getExePath">getExePath</a></td>
 <td></td>
 </tr>
@@ -60,6 +56,10 @@
 <td>Function to get the Python version (major, minor) as an integer value.</td>
 </tr>
 <tr>
+<td><a href="#isPythonSource">isPythonSource</a></td>
+<td>Function to check for a Python source code file.</td>
+</tr>
+<tr>
 <td><a href="#searchInterpreters">searchInterpreters</a></td>
 <td>Function to determine a list of all Python interpreters available via the executable search path (i.e.</td>
 </tr>
@@ -129,43 +129,6 @@
 <div align="right"><a href="#top">Up</a></div>
 <hr />
 <hr />
-<a NAME="determinePythonVersion" ID="determinePythonVersion"></a>
-<h2>determinePythonVersion</h2>
-<b>determinePythonVersion</b>(<i>filename, source, editor=None</i>)
-<p>
-    Function to determine the python version of a given file.
-</p>
-
-<dl>
-
-<dt><i>filename</i> (str)</dt>
-<dd>
-name of the file with extension
-</dd>
-<dt><i>source</i> (str)</dt>
-<dd>
-of the file
-</dd>
-<dt><i>editor</i> (Editor)</dt>
-<dd>
-reference to the editor, if the file is opened already
-</dd>
-</dl>
-<dl>
-<dt>Return:</dt>
-<dd>
-Python version if file is Python3
-</dd>
-</dl>
-<dl>
-<dt>Return Type:</dt>
-<dd>
-int
-</dd>
-</dl>
-<div align="right"><a href="#top">Up</a></div>
-<hr />
-<hr />
 <a NAME="getExePath" ID="getExePath"></a>
 <h2>getExePath</h2>
 <b>getExePath</b>(<i>branch, access, versionStr</i>)
@@ -283,6 +246,44 @@
 <div align="right"><a href="#top">Up</a></div>
 <hr />
 <hr />
+<a NAME="isPythonSource" ID="isPythonSource"></a>
+<h2>isPythonSource</h2>
+<b>isPythonSource</b>(<i>filename, source, editor=None</i>)
+<p>
+    Function to check for a Python source code file.
+</p>
+
+<dl>
+
+<dt><i>filename</i> (str)</dt>
+<dd>
+name of the file with extension
+</dd>
+<dt><i>source</i> (str)</dt>
+<dd>
+of the file
+</dd>
+<dt><i>editor</i> (Editor (optional))</dt>
+<dd>
+reference to the editor, if the file is opened already
+        (defaults to None)
+</dd>
+</dl>
+<dl>
+<dt>Return:</dt>
+<dd>
+flag indicating Python source code
+</dd>
+</dl>
+<dl>
+<dt>Return Type:</dt>
+<dd>
+bool
+</dd>
+</dl>
+<div align="right"><a href="#top">Up</a></div>
+<hr />
+<hr />
 <a NAME="searchInterpreters" ID="searchInterpreters"></a>
 <h2>searchInterpreters</h2>
 <b>searchInterpreters</b>(<i>environments=None</i>)
--- a/src/eric7/Plugins/CheckerPlugins/SyntaxChecker/SyntaxCheckService.py	Tue Jul 09 14:09:40 2024 +0200
+++ b/src/eric7/Plugins/CheckerPlugins/SyntaxChecker/SyntaxCheckService.py	Tue Jul 09 17:20:18 2024 +0200
@@ -55,9 +55,8 @@
         @return language of the file or None if not found
         @rtype str or None
         """
-        pyVer = PythonUtilities.determinePythonVersion(filename, source)
-        if pyVer:
-            return "Python{0}".format(pyVer)
+        if PythonUtilities.isPythonSource(filename, source):
+            return "Python3"
 
         for lang, (_env, _getArgs, getExt) in self.__supportedLanguages.items():
             if filename.endswith(tuple(getExt())):
--- a/src/eric7/Plugins/PluginCodeStyleChecker.py	Tue Jul 09 14:09:40 2024 +0200
+++ b/src/eric7/Plugins/PluginCodeStyleChecker.py	Tue Jul 09 17:20:18 2024 +0200
@@ -173,15 +173,12 @@
         @type list of (str, str, bool, str, str, bool, int, list of (int, int),
             bool, str, dict, dict, list of str, str, str, bool)
         """
-        if lang is None:
-            lang = "Python{0}".format(
-                PythonUtilities.determinePythonVersion(filename, source)
-            )
-        if lang != "Python3":
-            return
-
-        data = [source, args]
-        self.backgroundService.enqueueRequest("style", lang, filename, data)
+        if (
+            (lang is None and PythonUtilities.isPythonSource(filename, source))
+            or (lang is not None and lang == "Python3")
+        ):
+            data = [source, args]
+            self.backgroundService.enqueueRequest("style", "Python3", filename, data)
 
     def styleBatchCheck(self, argumentsList):
         """
@@ -196,13 +193,8 @@
             "Python3": [],
         }
         for filename, source, args in argumentsList:
-            lang = "Python{0}".format(
-                PythonUtilities.determinePythonVersion(filename, source)
-            )
-            if lang != "Python3":
-                continue
-            else:
-                data[lang].append((filename, source, args))
+            if PythonUtilities.isPythonSource(filename, source):
+                data["Python3"].append((filename, source, args))
 
         self.queuedBatches = []
         if data["Python3"]:
--- a/src/eric7/QScintilla/Editor.py	Tue Jul 09 14:09:40 2024 +0200
+++ b/src/eric7/QScintilla/Editor.py	Tue Jul 09 17:20:18 2024 +0200
@@ -1999,10 +1999,7 @@
                     language = Preferences.getEditorLexerAssoc(bindName)
             if language == "Python":
                 # correction for Python
-                pyVer = PythonUtilities.determinePythonVersion(
-                    filename, self.text(0), self
-                )
-                language = "Python{0}".format(pyVer)
+                language = "Python3"
             if language in [
                 "Python3",
                 "MicroPython",
@@ -2154,9 +2151,8 @@
         filename = os.path.basename(filename)
         apiLanguage = Preferences.getEditorLexerAssoc(filename)
         if apiLanguage == "":
-            pyVer = self.__getPyVersion()
-            if pyVer:
-                apiLanguage = "Python{0}".format(pyVer)
+            if PythonUtilities.isPythonSource(self.fileName, self.text(0), self):
+                apiLanguage = "Python3"
             elif self.isRubyFile():
                 apiLanguage = "Ruby"
 
@@ -2302,9 +2298,8 @@
         """
         ftype = self.filetype
         if not ftype:
-            pyVer = self.__getPyVersion()
-            if pyVer:
-                ftype = "Python{0}".format(pyVer)
+            if PythonUtilities.isPythonSource(self.fileName, self.text(0), self):
+                ftype = "Python3"
             elif self.isRubyFile():
                 ftype = "Ruby"
             else:
@@ -2321,16 +2316,6 @@
         """
         return self.encoding
 
-    def __getPyVersion(self):
-        """
-        Private method to return the Python main version or 0 if it's
-        not a Python file at all.
-
-        @return Python version or 0 if it's not a Python file
-        @rtype int
-        """
-        return PythonUtilities.determinePythonVersion(self.fileName, self.text(0), self)
-
     def isPyFile(self):
         """
         Public method to return a flag indicating a Python (2 or 3) file.
@@ -2338,7 +2323,7 @@
         @return flag indicating a Python3 file
         @rtype bool
         """
-        return self.__getPyVersion() == 3
+        return PythonUtilities.isPythonSource(self.fileName, self.text(0), self)
 
     def isPy3File(self):
         """
@@ -2347,7 +2332,7 @@
         @return flag indicating a Python3 file
         @rtype bool
         """
-        return self.__getPyVersion() == 3
+        return PythonUtilities.isPythonSource(self.fileName, self.text(0), self)
 
     def isMicroPythonFile(self):
         """
--- 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