src/eric7/Utilities/__init__.py

branch
eric7
changeset 9482
a2bc06a54d9d
parent 9473
3f23dbf37dbe
child 9507
1f39839655ea
equal deleted inserted replaced
9481:0b936ff1bbb9 9482:a2bc06a54d9d
12 import ctypes 12 import ctypes
13 import fnmatch 13 import fnmatch
14 import functools 14 import functools
15 import getpass 15 import getpass
16 import glob 16 import glob
17 import json
17 import os 18 import os
18 import pathlib 19 import pathlib
19 import re 20 import re
20 import shlex 21 import shlex
21 import subprocess # secok 22 import subprocess # secok
22 import sys 23 import sys
23 24
24 25 with contextlib.suppress(ImportError):
25 def __showwarning(message, category, filename, lineno, file=None, line=""): 26 import pwd # only available on Unix systems
26 """
27 Module function to raise a SyntaxError for a SyntaxWarning.
28
29 @param message warning object
30 @param category type object of the warning
31 @param filename name of the file causing the warning (string)
32 @param lineno line number causing the warning (integer)
33 @param file file to write the warning message to (ignored)
34 @param line line causing the warning (ignored)
35 @raise err exception of type SyntaxError
36 """
37 if category is SyntaxWarning:
38 err = SyntaxError(str(message))
39 err.filename = filename
40 err.lineno = lineno
41 raise err
42
43 27
44 import warnings 28 import warnings
45 29
46 warnings.showwarning = __showwarning
47
48 from codecs import BOM_UTF8, BOM_UTF16, BOM_UTF32 30 from codecs import BOM_UTF8, BOM_UTF16, BOM_UTF32
49 31
32 import chardet
33
34 from PyQt6 import sip
50 from PyQt6.Qsci import QSCINTILLA_VERSION_STR, QsciScintilla 35 from PyQt6.Qsci import QSCINTILLA_VERSION_STR, QsciScintilla
51 from PyQt6.QtCore import ( 36 from PyQt6.QtCore import (
52 PYQT_VERSION_STR, 37 PYQT_VERSION_STR,
53 QByteArray, 38 QByteArray,
54 QCoreApplication, 39 QCoreApplication,
77 isWindowsPlatform, 62 isWindowsPlatform,
78 qVersionTuple, 63 qVersionTuple,
79 sessionType, 64 sessionType,
80 setConfigDir, 65 setConfigDir,
81 ) 66 )
67
68 # TODO: move 'normalizeCode' here
82 from eric7.Plugins.CheckerPlugins.SyntaxChecker.SyntaxCheck import ( # __IGNORE_WARNING__ 69 from eric7.Plugins.CheckerPlugins.SyntaxChecker.SyntaxCheck import ( # __IGNORE_WARNING__
83 normalizeCode, 70 normalizeCode,
84 ) 71 )
85 from eric7.UI.Info import Program, Version 72 from eric7.UI.Info import Program, Version
73
74
75 def __showwarning(message, category, filename, lineno, file=None, line=""):
76 """
77 Module function to raise a SyntaxError for a SyntaxWarning.
78
79 @param message warning object
80 @param category type object of the warning
81 @param filename name of the file causing the warning (string)
82 @param lineno line number causing the warning (integer)
83 @param file file to write the warning message to (ignored)
84 @param line line causing the warning (ignored)
85 @raise err exception of type SyntaxError
86 """
87 if category is SyntaxWarning:
88 err = SyntaxError(str(message))
89 err.filename = filename
90 err.lineno = lineno
91 raise err
92
93
94 warnings.showwarning = __showwarning
86 95
87 configDir = None 96 configDir = None
88 97
89 codingBytes_regexps = [ 98 codingBytes_regexps = [
90 (5, re.compile(rb"""coding[:=]\s*([-\w_.]+)""")), 99 (5, re.compile(rb"""coding[:=]\s*([-\w_.]+)""")),
314 323
315 guess = None 324 guess = None
316 if Preferences.getEditor("AdvancedEncodingDetection"): 325 if Preferences.getEditor("AdvancedEncodingDetection"):
317 # Try the universal character encoding detector 326 # Try the universal character encoding detector
318 try: 327 try:
319 import chardet
320
321 guess = chardet.detect(text) 328 guess = chardet.detect(text)
322 if guess and guess["confidence"] > 0.95 and guess["encoding"] is not None: 329 if guess and guess["confidence"] > 0.95 and guess["encoding"] is not None:
323 codec = guess["encoding"].lower() 330 codec = guess["encoding"].lower()
324 return str(text, codec), "{0}-guessed".format(codec) 331 return str(text, codec), "{0}-guessed".format(codec)
325 except (UnicodeError, LookupError): 332 except (UnicodeError, LookupError):
504 with contextlib.suppress(UnicodeError): 511 with contextlib.suppress(UnicodeError):
505 return str(buffer, encoding="utf-8") 512 return str(buffer, encoding="utf-8")
506 513
507 # try codec detection 514 # try codec detection
508 try: 515 try:
509 import chardet
510
511 guess = chardet.detect(buffer) 516 guess = chardet.detect(buffer)
512 if guess and guess["encoding"] is not None: 517 if guess and guess["encoding"] is not None:
513 codec = guess["encoding"].lower() 518 codec = guess["encoding"].lower()
514 return str(buffer, encoding=codec) 519 return str(buffer, encoding=codec)
515 except (UnicodeError, LookupError): 520 except (UnicodeError, LookupError):
1639 @return real name of the user (string) 1644 @return real name of the user (string)
1640 """ 1645 """
1641 if isWindowsPlatform(): 1646 if isWindowsPlatform():
1642 return win32_getRealName() 1647 return win32_getRealName()
1643 else: 1648 else:
1644 import pwd
1645
1646 user = getpass.getuser() 1649 user = getpass.getuser()
1647 return pwd.getpwnam(user).pw_gecos 1650 return pwd.getpwnam(user).pw_gecos
1648 1651
1649 1652
1650 def getHomeDir(): 1653 def getHomeDir():
2033 @type str 2036 @type str
2034 @return string with version infos 2037 @return string with version infos
2035 @rtype str 2038 @rtype str
2036 """ 2039 """
2037 try: 2040 try:
2038 try:
2039 from PyQt6 import sip
2040 except ImportError:
2041 import sip
2042 sip_version_str = sip.SIP_VERSION_STR 2041 sip_version_str = sip.SIP_VERSION_STR
2043 except (ImportError, AttributeError): 2042 except AttributeError:
2044 sip_version_str = "sip version not available" 2043 sip_version_str = "sip version not available"
2045 2044
2046 sizeStr = "64-Bit" if sys.maxsize > 2**32 else "32-Bit" 2045 sizeStr = "64-Bit" if sys.maxsize > 2**32 else "32-Bit"
2047 2046
2048 info = ["Version Numbers:"] 2047 info = ["Version Numbers:"]
2049 2048
2050 info.append(" Python {0}, {1}".format(sys.version.split()[0], sizeStr)) 2049 info.append(" Python {0}, {1}".format(sys.version.split()[0], sizeStr))
2051 info.append(" Qt {0}".format(qVersion())) 2050 info.append(" Qt {0}".format(qVersion()))
2052 info.append(" PyQt6 {0}".format(PYQT_VERSION_STR)) 2051 info.append(" PyQt6 {0}".format(PYQT_VERSION_STR))
2053 try: 2052 try:
2054 from PyQt6 import QtCharts 2053 from PyQt6 import QtCharts # __IGNORE_WARNING_I10__
2055 2054
2056 info.append(" PyQt6-Charts {0}".format(QtCharts.PYQT_CHART_VERSION_STR)) 2055 info.append(" PyQt6-Charts {0}".format(QtCharts.PYQT_CHART_VERSION_STR))
2057 except (ImportError, AttributeError): 2056 except (ImportError, AttributeError):
2058 info.append(" PyQt6-Charts not installed") 2057 info.append(" PyQt6-Charts not installed")
2059 try: 2058 try:
2060 from PyQt6 import QtWebEngineCore 2059 from PyQt6 import QtWebEngineCore # __IGNORE_WARNING_I10__
2061 2060
2062 info.append( 2061 info.append(
2063 " PyQt6-WebEngine {0}".format(QtWebEngineCore.PYQT_WEBENGINE_VERSION_STR) 2062 " PyQt6-WebEngine {0}".format(QtWebEngineCore.PYQT_WEBENGINE_VERSION_STR)
2064 ) 2063 )
2065 except (ImportError, AttributeError): 2064 except (ImportError, AttributeError):
2067 info.append(" PyQt6-QScintilla {0}".format(QSCINTILLA_VERSION_STR)) 2066 info.append(" PyQt6-QScintilla {0}".format(QSCINTILLA_VERSION_STR))
2068 info.append(" sip {0}".format(sip_version_str)) 2067 info.append(" sip {0}".format(sip_version_str))
2069 with contextlib.suppress(ImportError): 2068 with contextlib.suppress(ImportError):
2070 from PyQt6 import QtWebEngineWidgets # __IGNORE_WARNING__ 2069 from PyQt6 import QtWebEngineWidgets # __IGNORE_WARNING__
2071 2070
2072 from eric7.WebBrowser.Tools import WebBrowserTools 2071 from eric7.WebBrowser.Tools import WebBrowserTools # __IGNORE_WARNING_I101__
2073 2072
2074 ( 2073 (
2075 chromiumVersion, 2074 chromiumVersion,
2076 chromiumSecurityVersion, 2075 chromiumSecurityVersion,
2077 ) = WebBrowserTools.getWebEngineVersions()[0:2] 2076 ) = WebBrowserTools.getWebEngineVersions()[0:2]
2175 @type str 2174 @type str
2176 @return list containing sys.path of the interpreter; an empty list 2175 @return list containing sys.path of the interpreter; an empty list
2177 is returned, if the interpreter is the one used to run eric itself 2176 is returned, if the interpreter is the one used to run eric itself
2178 @rtype list of str 2177 @rtype list of str
2179 """ 2178 """
2180 import json
2181
2182 sysPath = [] 2179 sysPath = []
2183 2180
2184 getSysPathSkript = os.path.join(os.path.dirname(__file__), "GetSysPath.py") 2181 getSysPathSkript = os.path.join(os.path.dirname(__file__), "GetSysPath.py")
2185 args = [getSysPathSkript] 2182 args = [getSysPathSkript]
2186 proc = QProcess() 2183 proc = QProcess()
2214 Function to provide an os.kill equivalent for Win32. 2211 Function to provide an os.kill equivalent for Win32.
2215 2212
2216 @param pid process id (integer) 2213 @param pid process id (integer)
2217 @return result of the kill (boolean) 2214 @return result of the kill (boolean)
2218 """ 2215 """
2219 import win32api 2216 import win32api # __IGNORE_WARNING_I102__
2220 2217
2221 handle = win32api.OpenProcess(1, 0, pid) 2218 handle = win32api.OpenProcess(1, 0, pid)
2222 return 0 != win32api.TerminateProcess(handle, 0) 2219 return 0 != win32api.TerminateProcess(handle, 0)
2223 2220
2224 2221
2227 Function to get the user name under Win32. 2224 Function to get the user name under Win32.
2228 2225
2229 @return user name (string) 2226 @return user name (string)
2230 """ 2227 """
2231 try: 2228 try:
2232 import win32api 2229 import win32api # __IGNORE_WARNING_I10__
2233 2230
2234 return win32api.GetUserName() 2231 return win32api.GetUserName()
2235 except ImportError: 2232 except ImportError:
2236 try: 2233 try:
2237 u = getEnvironmentEntry("USERNAME") 2234 u = getEnvironmentEntry("USERNAME")
2244 """ 2241 """
2245 Function to get the user's real name (aka. display name) under Win32. 2242 Function to get the user's real name (aka. display name) under Win32.
2246 2243
2247 @return real name of the current user (string) 2244 @return real name of the current user (string)
2248 """ 2245 """
2249 import ctypes
2250
2251 GetUserNameEx = ctypes.windll.secur32.GetUserNameExW 2246 GetUserNameEx = ctypes.windll.secur32.GetUserNameExW
2252 NameDisplay = 3 2247 NameDisplay = 3
2253 2248
2254 size = ctypes.pointer(ctypes.c_ulong(0)) 2249 size = ctypes.pointer(ctypes.c_ulong(0))
2255 GetUserNameEx(NameDisplay, None, size) 2250 GetUserNameEx(NameDisplay, None, size)

eric ide

mercurial