src/eric7/Utilities/__init__.py

branch
eric7-maintenance
changeset 9549
67295777d9fe
parent 9449
c982bacca23f
parent 9531
155b2646799a
child 9654
7328efba128b
--- a/src/eric7/Utilities/__init__.py	Mon Oct 31 14:07:57 2022 +0100
+++ b/src/eric7/Utilities/__init__.py	Wed Nov 30 09:19:51 2022 +0100
@@ -14,6 +14,7 @@
 import functools
 import getpass
 import glob
+import json
 import os
 import pathlib
 import re
@@ -21,6 +22,50 @@
 import subprocess  # secok
 import sys
 
+with contextlib.suppress(ImportError):
+    import pwd  # only available on Unix systems
+
+import warnings
+
+from codecs import BOM_UTF8, BOM_UTF16, BOM_UTF32
+
+import chardet
+
+from PyQt6 import sip
+from PyQt6.Qsci import QSCINTILLA_VERSION_STR, QsciScintilla
+from PyQt6.QtCore import (
+    PYQT_VERSION_STR,
+    QByteArray,
+    QCoreApplication,
+    QCryptographicHash,
+    QDir,
+    QProcess,
+    qVersion,
+)
+
+from eric7 import Preferences
+from eric7.EricWidgets.EricApplication import ericApp
+
+# import these methods into the Utilities namespace
+from eric7.Globals import (  # __IGNORE_WARNING__
+    desktopName,
+    getConfig,
+    getConfigDir,
+    getPyQt6ModulesDirectory,
+    getPyQtToolsPath,
+    getPythonExecutable,
+    getPythonLibraryDirectory,
+    getPythonScriptsDirectory,
+    getQtBinariesPath,
+    isLinuxPlatform,
+    isMacPlatform,
+    isWindowsPlatform,
+    qVersionTuple,
+    sessionType,
+    setConfigDir,
+)
+from eric7.UI.Info import Program, Version
+
 
 def __showwarning(message, category, filename, lineno, file=None, line=""):
     """
@@ -41,53 +86,8 @@
         raise err
 
 
-import warnings
-
 warnings.showwarning = __showwarning
 
-from codecs import BOM_UTF8, BOM_UTF16, BOM_UTF32
-
-from PyQt6.QtCore import (
-    qVersion,
-    PYQT_VERSION_STR,
-    QDir,
-    QProcess,
-    QByteArray,
-    QCoreApplication,
-    QCryptographicHash,
-)
-from PyQt6.Qsci import QSCINTILLA_VERSION_STR, QsciScintilla
-
-# import these methods into the Utilities namespace
-from eric7.Globals import (  # __IGNORE_WARNING__
-    isWindowsPlatform,
-    isLinuxPlatform,
-    isMacPlatform,
-    desktopName,
-    sessionType,
-    getConfigDir,
-    setConfigDir,
-    getPythonLibraryDirectory,
-    getPythonScriptsDirectory,
-    getPyQt6ModulesDirectory,
-    getQtBinariesPath,
-    getPyQtToolsPath,
-    qVersionTuple,
-    getPythonExecutable,
-)
-
-from eric7.EricWidgets.EricApplication import ericApp
-
-from eric7.UI.Info import Program, Version
-
-from eric7 import Preferences
-from eric7.Plugins.CheckerPlugins.SyntaxChecker.SyntaxCheck import (
-    # __IGNORE_WARNING__
-    normalizeCode,
-)
-
-from eric7.Globals import getConfig
-
 configDir = None
 
 codingBytes_regexps = [
@@ -320,8 +320,6 @@
     if Preferences.getEditor("AdvancedEncodingDetection"):
         # Try the universal character encoding detector
         try:
-            import chardet
-
             guess = chardet.detect(text)
             if guess and guess["confidence"] > 0.95 and guess["encoding"] is not None:
                 codec = guess["encoding"].lower()
@@ -510,8 +508,6 @@
 
     # try codec detection
     try:
-        import chardet
-
         guess = chardet.detect(buffer)
         if guess and guess["encoding"] is not None:
             codec = guess["encoding"].lower()
@@ -537,6 +533,21 @@
     return data.decode("utf-8")
 
 
+def normalizeCode(codestring):
+    """
+    Function to normalize the given code.
+
+    @param codestring code to be normalized (string)
+    @return normalized code (string)
+    """
+    codestring = codestring.replace("\r\n", "\n").replace("\r", "\n")
+
+    if codestring and codestring[-1] != "\n":
+        codestring += "\n"
+
+    return codestring
+
+
 _escape = re.compile("[&<>\"'\u0080-\uffff]")
 
 _escape_map = {
@@ -1645,8 +1656,6 @@
     if isWindowsPlatform():
         return win32_getRealName()
     else:
-        import pwd
-
         user = getpass.getuser()
         return pwd.getpwnam(user).pw_gecos
 
@@ -2039,12 +2048,8 @@
     @rtype str
     """
     try:
-        try:
-            from PyQt6 import sip
-        except ImportError:
-            import sip
         sip_version_str = sip.SIP_VERSION_STR
-    except (ImportError, AttributeError):
+    except AttributeError:
         sip_version_str = "sip version not available"
 
     sizeStr = "64-Bit" if sys.maxsize > 2**32 else "32-Bit"
@@ -2055,13 +2060,13 @@
     info.append("  Qt {0}".format(qVersion()))
     info.append("  PyQt6 {0}".format(PYQT_VERSION_STR))
     try:
-        from PyQt6 import QtCharts
+        from PyQt6 import QtCharts  # __IGNORE_WARNING_I10__
 
         info.append("  PyQt6-Charts {0}".format(QtCharts.PYQT_CHART_VERSION_STR))
     except (ImportError, AttributeError):
         info.append("  PyQt6-Charts not installed")
     try:
-        from PyQt6 import QtWebEngineCore
+        from PyQt6 import QtWebEngineCore  # __IGNORE_WARNING_I10__
 
         info.append(
             "  PyQt6-WebEngine {0}".format(QtWebEngineCore.PYQT_WEBENGINE_VERSION_STR)
@@ -2072,7 +2077,8 @@
     info.append("  sip {0}".format(sip_version_str))
     with contextlib.suppress(ImportError):
         from PyQt6 import QtWebEngineWidgets  # __IGNORE_WARNING__
-        from eric7.WebBrowser.Tools import WebBrowserTools
+
+        from eric7.WebBrowser.Tools import WebBrowserTools  # __IGNORE_WARNING_I101__
 
         (
             chromiumVersion,
@@ -2084,6 +2090,8 @@
     info.append("  {0} {1}".format(Program, Version))
     info.append("")
     info.append("Platform: {0}".format(sys.platform))
+    if os.environ.get("SOMMELIER_VERSION", ""):
+        info[-1] += ", ChromeOS"
     info.append(sys.version)
     desktop = desktopName()
     if desktop:
@@ -2178,8 +2186,6 @@
         is returned, if the interpreter is the one used to run eric itself
     @rtype list of str
     """
-    import json
-
     sysPath = []
 
     getSysPathSkript = os.path.join(os.path.dirname(__file__), "GetSysPath.py")
@@ -2217,7 +2223,7 @@
     @param pid process id (integer)
     @return result of the kill (boolean)
     """
-    import win32api
+    import win32api  # __IGNORE_WARNING_I102__
 
     handle = win32api.OpenProcess(1, 0, pid)
     return 0 != win32api.TerminateProcess(handle, 0)
@@ -2230,7 +2236,7 @@
     @return user name (string)
     """
     try:
-        import win32api
+        import win32api  # __IGNORE_WARNING_I10__
 
         return win32api.GetUserName()
     except ImportError:
@@ -2247,8 +2253,6 @@
 
     @return real name of the current user (string)
     """
-    import ctypes
-
     GetUserNameEx = ctypes.windll.secur32.GetUserNameExW
     NameDisplay = 3
 

eric ide

mercurial