src/eric7/Utilities/__init__.py

branch
server
changeset 10546
300487f5f517
parent 10496
f9925e08dbce
child 10561
be23a662d709
--- a/src/eric7/Utilities/__init__.py	Fri Feb 02 14:55:14 2024 +0100
+++ b/src/eric7/Utilities/__init__.py	Mon Feb 05 11:15:47 2024 +0100
@@ -338,6 +338,31 @@
     return str(text, "utf-8", "ignore"), "utf-8-ignore"
 
 
+def decodeWithEncoding(text, encoding):
+    """
+    Function to decode some byte text into a string.
+
+    @param text byte text to decode
+    @type bytes
+    @param encoding encoding to be used to read the file
+    @type str
+    @return tuple of decoded text and encoding
+    @rtype tuple of (str, str)
+    """
+    if encoding:
+        with contextlib.suppress(UnicodeError, LookupError):
+            return str(text, encoding), "{0}-selected".format(encoding)
+
+        # Try default encoding
+        with contextlib.suppress(UnicodeError, LookupError):
+            codec = Preferences.getEditor("DefaultEncoding")
+            return str(text, codec), "{0}-default".format(codec)
+
+        # Assume UTF-8 loosing information
+        return str(text, "utf-8", "ignore"), "utf-8-ignore"
+    else:
+        return decode(text)
+
 def readEncodedFileWithEncoding(filename, encoding):
     """
     Function to read a file and decode its contents into proper text.
@@ -351,19 +376,7 @@
     """
     with open(filename, "rb") as f:
         text = f.read()
-    if encoding:
-        with contextlib.suppress(UnicodeError, LookupError):
-            return str(text, encoding), "{0}-selected".format(encoding)
-
-        # Try default encoding
-        with contextlib.suppress(UnicodeError, LookupError):
-            codec = Preferences.getEditor("DefaultEncoding")
-            return str(text, codec), "{0}-default".format(codec)
-
-        # Assume UTF-8 loosing information
-        return str(text, "utf-8", "ignore"), "utf-8-ignore"
-    else:
-        return decode(text)
+    return decodeWithEncoding(text, encoding)
 
 
 def writeEncodedFile(filename, text, origEncoding, forcedEncoding=""):

eric ide

mercurial