src/eric7/Utilities/__init__.py

branch
eric7
changeset 9482
a2bc06a54d9d
parent 9473
3f23dbf37dbe
child 9507
1f39839655ea
diff -r 0b936ff1bbb9 -r a2bc06a54d9d src/eric7/Utilities/__init__.py
--- a/src/eric7/Utilities/__init__.py	Sun Nov 06 11:22:39 2022 +0100
+++ b/src/eric7/Utilities/__init__.py	Mon Nov 07 17:19:58 2022 +0100
@@ -14,6 +14,7 @@
 import functools
 import getpass
 import glob
+import json
 import os
 import pathlib
 import re
@@ -21,32 +22,16 @@
 import subprocess  # secok
 import sys
 
-
-def __showwarning(message, category, filename, lineno, file=None, line=""):
-    """
-    Module function to raise a SyntaxError for a SyntaxWarning.
-
-    @param message warning object
-    @param category type object of the warning
-    @param filename name of the file causing the warning (string)
-    @param lineno line number causing the warning (integer)
-    @param file file to write the warning message to (ignored)
-    @param line line causing the warning (ignored)
-    @raise err exception of type SyntaxError
-    """
-    if category is SyntaxWarning:
-        err = SyntaxError(str(message))
-        err.filename = filename
-        err.lineno = lineno
-        raise err
-
+with contextlib.suppress(ImportError):
+    import pwd  # only available on Unix systems
 
 import warnings
 
-warnings.showwarning = __showwarning
-
 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,
@@ -79,11 +64,35 @@
     sessionType,
     setConfigDir,
 )
+
+# TODO: move 'normalizeCode' here
 from eric7.Plugins.CheckerPlugins.SyntaxChecker.SyntaxCheck import (  # __IGNORE_WARNING__
     normalizeCode,
 )
 from eric7.UI.Info import Program, Version
 
+
+def __showwarning(message, category, filename, lineno, file=None, line=""):
+    """
+    Module function to raise a SyntaxError for a SyntaxWarning.
+
+    @param message warning object
+    @param category type object of the warning
+    @param filename name of the file causing the warning (string)
+    @param lineno line number causing the warning (integer)
+    @param file file to write the warning message to (ignored)
+    @param line line causing the warning (ignored)
+    @raise err exception of type SyntaxError
+    """
+    if category is SyntaxWarning:
+        err = SyntaxError(str(message))
+        err.filename = filename
+        err.lineno = lineno
+        raise err
+
+
+warnings.showwarning = __showwarning
+
 configDir = None
 
 codingBytes_regexps = [
@@ -316,8 +325,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()
@@ -506,8 +513,6 @@
 
     # try codec detection
     try:
-        import chardet
-
         guess = chardet.detect(buffer)
         if guess and guess["encoding"] is not None:
             codec = guess["encoding"].lower()
@@ -1641,8 +1646,6 @@
     if isWindowsPlatform():
         return win32_getRealName()
     else:
-        import pwd
-
         user = getpass.getuser()
         return pwd.getpwnam(user).pw_gecos
 
@@ -2035,12 +2038,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"
@@ -2051,13 +2050,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)
@@ -2069,7 +2068,7 @@
     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,
@@ -2177,8 +2176,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")
@@ -2216,7 +2213,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)
@@ -2229,7 +2226,7 @@
     @return user name (string)
     """
     try:
-        import win32api
+        import win32api  # __IGNORE_WARNING_I10__
 
         return win32api.GetUserName()
     except ImportError:
@@ -2246,8 +2243,6 @@
 
     @return real name of the current user (string)
     """
-    import ctypes
-
     GetUserNameEx = ctypes.windll.secur32.GetUserNameExW
     NameDisplay = 3
 

eric ide

mercurial