Utilities/__init__.py

changeset 46
b09750fd2a06
parent 45
9a18f4dbb493
child 53
c3eb7cc1ff8b
--- a/Utilities/__init__.py	Sun Jan 10 19:19:52 2010 +0000
+++ b/Utilities/__init__.py	Mon Jan 11 15:23:45 2010 +0000
@@ -131,7 +131,16 @@
     """
     f = open(filename, "rb")
     text = f.read()
-    f.close()
+    f.close()
+    return decode(text)
+
+def decode(text):
+    """
+    Function to decode some byte text into a string.
+    
+    @param text byte text to decode (bytes)
+    @return tuple of decoded text and encoding (string, string)
+    """
     try:
         if text.startswith(BOM_UTF8):
             # UTF-8 with BOM
@@ -195,6 +204,22 @@
     @param text text to be written (string)
     @param orig_coding type of the original encoding (string)
     @return encoding used for writing the file (string)
+    """
+    etext, encoding = encode(text, orig_coding)
+    
+    f = open(filename, "wb")
+    f.write(etext)
+    f.close()
+    
+    return encoding
+
+def encode(text, orig_coding):
+    """
+    Function to encode text into a byte text.
+    
+    @param text text to be encoded (string)
+    @param orig_coding type of the original encoding (string)
+    @return tuple of encoded text and encoding used (bytes, string)
     """
     encoding = None
     if orig_coding == 'utf-8-bom':
@@ -240,11 +265,7 @@
                         # Save as UTF-8 without BOM
                         etext, encoding = text.encode('utf-8'), 'utf-8'
     
-    f = open(filename, "wb")
-    f.write(etext)
-    f.close()
-    
-    return encoding
+    return etext, encoding
     
 _escape = re.compile(eval(r'"[&<>\"\u0080-\uffff]"'))
 

eric ide

mercurial