Commits

Diego Búrigo Zacarão committed 6350811

Added support for file content to the detect_encoding function

  • Participants
  • Parent commits 2d56f63

Comments (0)

Files changed (1)

 # }}}
 # function detect_encoding() {{{
 
-def detect_encoding(fpath, binary_mode=False):
+def detect_encoding(pofile, binary_mode=False, is_content=False):
     """
-    Try to detect the encoding used by the file *fpath*. The function will
-    return polib default *encoding* if it's unable to detect it.
+    Try to detect the encoding used by the *pofile*. The parameter *pofile*
+    might be a PO file path or its content. In case of using the content, the
+    *is_content* parameter should be set to True. The function will return 
+    polib default *encoding* if it's unable to detect it.
 
     **Keyword argument**:
-      - *fpath*: string, full or relative path to the mo file to parse.
+      - *pofile*: string, full or relative path to the po/mo file or its content.
+      - *is_contents*: boolean, True if *pofile* has a file content.
+      - *binary_mode*: boolean, True if *pofile* has a mo file path.
 
     **Examples**:
 
     utf-8
     >>> print(detect_encoding('tests/test_utf8.po'))
     UTF-8
+    >>> print(detect_encoding(open('tests/test_utf8.po','r').read(), is_content=True))
+    UTF-8
     >>> print(detect_encoding('tests/test_utf8.mo', True))
     UTF-8
     >>> print(detect_encoding('tests/test_iso-8859-15.po'))
     """
     import re
     rx = re.compile(r'"?Content-Type:.+? charset=([\w_\-:\.]+)')
-    if binary_mode:
-        mode = 'rb'
+
+    if is_content:
+            match = rx.search(pofile)
+            if match:
+                return match.group(1).strip()
     else:
-        mode = 'r'
-    f = open(fpath, mode)
-    for l in f.readlines():
-        match = rx.search(l)
-        if match:
-            f.close()
-            return match.group(1).strip()
-    f.close()
+        if binary_mode:
+            mode = 'rb'
+        else:
+            mode = 'r'
+        f = open(pofile, mode)
+        for l in f.readlines():
+            match = rx.search(l)
+            if match:
+                f.close()
+                return match.group(1).strip()
+        f.close()
     return default_encoding
 
 # }}}