Utilities/__init__.py

changeset 3394
d1d4d79b4f11
parent 3206
f516fc427551
child 3432
7cfe7471175d
diff -r 080ace4829b4 -r d1d4d79b4f11 Utilities/__init__.py
--- a/Utilities/__init__.py	Mon Mar 17 19:11:48 2014 +0100
+++ b/Utilities/__init__.py	Mon Mar 17 19:58:45 2014 +0100
@@ -252,6 +252,34 @@
     return str(text, "utf-8", "ignore"), 'utf-8-ignore'
 
 
+def readEncodedFileWithEncoding(filename, encoding):
+    """
+    Function to read a file and decode its contents into proper text.
+    
+    @param filename name of the file to read (string)
+    @keyparam encoding encoding to be used to read the file (string)
+    @return tuple of decoded text and encoding (string, string)
+    """
+    f = open(filename, "rb")
+    text = f.read()
+    f.close()
+    if encoding:
+        try:
+            return str(text, encoding), '{0}-selected'.format(encoding)
+        except (UnicodeError, LookupError):
+            pass
+        # Try default encoding
+        try:
+            codec = Preferences.getEditor("DefaultEncoding")
+            return str(text, codec), '{0}-default'.format(codec)
+        except (UnicodeError, LookupError):
+            pass
+        # Assume UTF-8 loosing information
+        return str(text, "utf-8", "ignore"), 'utf-8-ignore'
+    else:
+        return decode(text)
+
+
 def writeEncodedFile(filename, text, orig_coding):
     """
     Function to write a file with properly encoded text.

eric ide

mercurial