src/eric7/WebBrowser/Tools/WebBrowserTools.py

branch
eric7
changeset 9221
bf71ee032bb4
parent 9209
b99e7fd55fd3
child 9473
3f23dbf37dbe
diff -r e9e7eca7efee -r bf71ee032bb4 src/eric7/WebBrowser/Tools/WebBrowserTools.py
--- a/src/eric7/WebBrowser/Tools/WebBrowserTools.py	Wed Jul 13 11:16:20 2022 +0200
+++ b/src/eric7/WebBrowser/Tools/WebBrowserTools.py	Wed Jul 13 14:55:47 2022 +0200
@@ -11,9 +11,7 @@
 import re
 import mimetypes
 
-from PyQt6.QtCore import (
-    QByteArray, QUrl, QCoreApplication, QBuffer, QIODevice
-)
+from PyQt6.QtCore import QByteArray, QUrl, QCoreApplication, QBuffer, QIODevice
 from PyQt6.QtGui import QPixmap
 
 
@@ -27,7 +25,7 @@
 def readAllFileContents(filename):
     """
     Function to read the string contents of the given file.
-    
+
     @param filename name of the file
     @type str
     @return contents of the file
@@ -43,7 +41,7 @@
 def containsSpace(string):
     """
     Function to check, if a string contains whitespace characters.
-    
+
     @param string string to be checked
     @type str
     @return flag indicating the presence of at least one whitespace character
@@ -55,7 +53,7 @@
 def ensureUniqueFilename(name, appendFormat="({0})"):
     """
     Module function to generate an unique file name based on a pattern.
-    
+
     @param name desired file name (string)
     @param appendFormat format pattern to be used to make the unique name
         (string)
@@ -63,61 +61,58 @@
     """
     if not os.path.exists(name):
         return name
-    
+
     tmpFileName = name
     i = 1
     while os.path.exists(tmpFileName):
         tmpFileName = name
         index = tmpFileName.rfind(".")
-        
+
         appendString = appendFormat.format(i)
         if index == -1:
             tmpFileName += appendString
         else:
-            tmpFileName = (
-                tmpFileName[:index] + appendString + tmpFileName[index:]
-            )
+            tmpFileName = tmpFileName[:index] + appendString + tmpFileName[index:]
         i += 1
-    
+
     return tmpFileName
 
 
 def getFileNameFromUrl(url):
     """
     Module function to generate a file name based on the given URL.
-    
+
     @param url URL (QUrl)
     @return file name (string)
     """
     fileName = url.toString(
-        QUrl.UrlFormattingOption.RemoveFragment |
-        QUrl.UrlFormattingOption.RemoveQuery |
-        QUrl.UrlFormattingOption.RemoveScheme |
-        QUrl.UrlFormattingOption.RemovePort
+        QUrl.UrlFormattingOption.RemoveFragment
+        | QUrl.UrlFormattingOption.RemoveQuery
+        | QUrl.UrlFormattingOption.RemoveScheme
+        | QUrl.UrlFormattingOption.RemovePort
     )
     if fileName.find("/") != -1:
         pos = fileName.rfind("/")
         fileName = fileName[pos:]
         fileName = fileName.replace("/", "")
-    
+
     fileName = filterCharsFromFilename(fileName)
-    
+
     if not fileName:
         fileName = filterCharsFromFilename(url.host().replace(".", "_"))
-    
+
     return fileName
 
 
 def filterCharsFromFilename(name):
     """
     Module function to filter illegal characters.
-    
+
     @param name name to be sanitized (string)
     @return sanitized name (string)
     """
     return (
-        name
-        .replace("/", "_")
+        name.replace("/", "_")
         .replace("\\", "")
         .replace(":", "")
         .replace("*", "")
@@ -132,7 +127,7 @@
 def pixmapFromByteArray(data):
     """
     Module function to convert a byte array to a pixmap.
-    
+
     @param data data for the pixmap
     @type bytes or QByteArray
     @return extracted pixmap
@@ -141,7 +136,7 @@
     pixmap = QPixmap()
     barray = QByteArray.fromBase64(data)
     pixmap.loadFromData(barray)
-    
+
     return pixmap
 
 
@@ -149,7 +144,7 @@
     """
     Module function to convert a pixmap to a byte array containing the pixmap
     as a PNG encoded as base64.
-    
+
     @param pixmap pixmap to be converted
     @type QPixmap
     @return byte array containing the pixmap
@@ -160,14 +155,14 @@
     buffer.open(QIODevice.OpenModeFlag.WriteOnly)
     if pixmap.save(buffer, "PNG"):
         return buffer.buffer().toBase64()
-    
+
     return QByteArray()
 
 
 def pixmapToDataUrl(pixmap, mimetype="image/png"):
     """
     Module function to convert a pixmap to a data: URL.
-    
+
     @param pixmap pixmap to be converted
     @type QPixmap
     @param mimetype MIME type to be used
@@ -186,10 +181,10 @@
     """
     Module function to load a pixmap file and convert the pixmap to a
     data: URL.
-    
+
     Note: If the given pixmap file path is not absolute, it is assumed to
     denote a pixmap file in the icons data directory.
-    
+
     @param pixmapFile file name of the pixmap file
     @type str
     @param asString flag indicating a string representation is requested
@@ -199,13 +194,13 @@
     """
     if not os.path.isabs(pixmapFile):
         pixmapFile = os.path.join(WebBrowserDataDirectory["icons"], pixmapFile)
-    
+
     mime = mimetypes.guess_type(pixmapFile, strict=False)[0]
     if mime is None:
         # assume PNG file
         mime = "image/png"
     url = pixmapToDataUrl(QPixmap(pixmapFile), mimetype=mime)
-    
+
     if asString:
         return url.toString()
     else:
@@ -216,78 +211,78 @@
     """
     Module function to extract the web engine related versions from the default
     user agent string.
-    
+
     Note: For PyQt 6.3.1 or newer the data is extracted via some Qt functions.
-    
+
     @return tuple containing the Chromium version, the Chromium security patch
         version and the QtWebEngine version
     @rtype tuple of (str, str, str)
     """
     try:
         from PyQt6.QtWebEngineCore import (
-            qWebEngineVersion, qWebEngineChromiumVersion,
-            qWebEngineChromiumSecurityPatchVersion
+            qWebEngineVersion,
+            qWebEngineChromiumVersion,
+            qWebEngineChromiumSecurityPatchVersion,
         )
+
         chromiumVersion = qWebEngineChromiumVersion()
         chromiumSecurityVersion = qWebEngineChromiumSecurityPatchVersion()
         webengineVersion = qWebEngineVersion()
     except ImportError:
         # backwards compatibility for PyQt < 6.3.1
         from PyQt6.QtWebEngineCore import QWebEngineProfile
-        
+
         useragent = QWebEngineProfile.defaultProfile().httpUserAgent()
         match = re.search(r"""Chrome/([\d.]+)""", useragent)
         chromiumVersion = (
             match.group(1)
-            if match else
-            QCoreApplication.translate("WebBrowserTools", "<unknown>")
+            if match
+            else QCoreApplication.translate("WebBrowserTools", "<unknown>")
         )
         match = re.search(r"""QtWebEngine/([\d.]+)""", useragent)
         webengineVersion = (
             match.group(1)
-            if match else
-            QCoreApplication.translate("WebBrowserTools", "<unknown>")
+            if match
+            else QCoreApplication.translate("WebBrowserTools", "<unknown>")
         )
         chromiumSecurityVersion = ""
         # not available via the user agent string
-    
+
     return (chromiumVersion, chromiumSecurityVersion, webengineVersion)
 
 
 def getHtmlPage(pageFileName):
     """
     Module function to load a HTML page.
-    
+
     Note: If the given HTML file path is not absolute, it is assumed to
     denote a HTML file in the html data directory.
-    
+
     @param pageFileName file name of the HTML file
     @type str
     @return HTML page
     @rtype str
     """
     if not os.path.isabs(pageFileName):
-        pageFileName = os.path.join(
-            WebBrowserDataDirectory["html"], pageFileName)
-    
+        pageFileName = os.path.join(WebBrowserDataDirectory["html"], pageFileName)
+
     return readAllFileContents(pageFileName)
 
 
 def getJavascript(jsFileName):
     """
     Module function to load a JavaScript source file.
-    
+
     Note: If the given JavaScript source file path is not absolute, it is
     assumed to denote a JavaScript source file in the javascript data
     directory.
-    
+
     @param jsFileName file name of the JavaScript source file
     @type str
     @return JavaScript source
     @rtype str
     """
     if not os.path.isabs(jsFileName):
-        jsFileName = os.path.join(
-            WebBrowserDataDirectory["js"], jsFileName)
-    
+        jsFileName = os.path.join(WebBrowserDataDirectory["js"], jsFileName)
+
     return readAllFileContents(jsFileName)

eric ide

mercurial