diff Generic/utils.py @ 26:16f91684686b default tip

Upgrade to python 3. Keep python 2/3 compatibility
author Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
date Tue, 18 Jun 2019 17:50:23 +0200
parents 65e7ae0d0e63
children
line wrap: on
line diff
--- a/Generic/utils.py	Mon May 20 13:18:33 2019 +0200
+++ b/Generic/utils.py	Tue Jun 18 17:50:23 2019 +0200
@@ -20,14 +20,22 @@
 ## along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 # Modules
+
+# python 2/3 compatibility
+from __future__ import absolute_import, division, print_function, unicode_literals
+from builtins import str as text
+from six import text_type
+from io import IOBase
+
 import re 
 import imghdr
 import os.path
 
+
 # add svg to imghdr
 def test_svg(h, f):
     """SVG """
-    if  isinstance(f,file):
+    if  isinstance(f,IOBase):
         _pos = f.tell()
         f.seek(0)
         _h = f.read(32)
@@ -37,35 +45,35 @@
     else:
         _h = h
         _l = h[-32:]
-    if "<?xml" in _h and "</svg>" in  _l:
+    if b"<?xml" in _h and b"</svg>" in  _l:
         return 'svg'
 imghdr.tests.append(test_svg)
 
 # add ico to imghdr
 def test_ico(h, f):
     """ico image file"""
-    if h[:4] == "\x00\x00\x01\x00":
+    if h[:4] == b"\x00\x00\x01\x00":
         return 'ico'
 imghdr.tests.append(test_ico)
 
 # add wmf to imghdr
 def test_pdf(h, f):
     """pdf file"""
-    if h[:4] == "%PDF":
+    if h[:4] == b"%PDF":
         return 'pdf'
 imghdr.tests.append(test_pdf)
 
 # add wmf to imghdr
 def test_wmf(h, f):
     """wmf image library"""
-    if h[:6] == "\xd7\xcd\xc6\x9a\x00\x00":
+    if h[:6] == b"\xd7\xcd\xc6\x9a\x00\x00":
         return 'wmf'
 imghdr.tests.append(test_wmf)
 
 # add dxf to imghdr
 def test_dxf(h, f):
     """AutoCAD DXF: Drawing Interchange Format"""
-    if  isinstance(f,file):
+    if  isinstance(f,IOBase):
         _pos = f.tell()
         f.seek(0)
         _h = f.read(128)
@@ -75,16 +83,16 @@
     else:
         _h = h
         _l = h[-32:]
-    _h = _h.replace("\r","")
-    _l = _l.replace("\r","")
-    if ("  0\nSECTION\n  2\nHEADER\n" in _h or\
-       "  0\nSECTION\n  2\nCLASSES\n" in _h or\
-       "  0\nSECTION\n  2\nTABLES\n" in _h or\
-       "  0\nSECTION\n  2\nBLOCKS\n" in _h or\
-       "  0\nSECTION\n  2\nENTITIES\n" in _h or\
-       "  0\nSECTION\n  2\nOBJECTS\n" in _h or\
-       "  0\nSECTION\n  2\nTHUMBNAILIMAGE\n" in _h) and \
-       _l[-19:] == "  0\nENDSEC\n  0\nEOF\n":
+    _h = _h.replace(b"\r",b"")
+    _l = _l.replace(b"\r",b"")
+    if (b"  0\nSECTION\n  2\nHEADER\n" in _h or\
+       b"  0\nSECTION\n  2\nCLASSES\n" in _h or\
+       b"  0\nSECTION\n  2\nTABLES\n" in _h or\
+       b"  0\nSECTION\n  2\nBLOCKS\n" in _h or\
+       b"  0\nSECTION\n  2\nENTITIES\n" in _h or\
+       b"  0\nSECTION\n  2\nOBJECTS\n" in _h or\
+       b"  0\nSECTION\n  2\nTHUMBNAILIMAGE\n" in _h) and \
+       _l[-19:] == b"  0\nENDSEC\n  0\nEOF\n":
         return 'dxf'
 imghdr.tests.append(test_dxf)
 
@@ -100,8 +108,7 @@
         "Invalid type (%s) in record: %s" %(type, record)
     """
     for _index in range(len(tuple_strings)):
-        string = string.replace("$" + str(_index+1), tuple_strings[_index])
-        #string = string.replace("$" + str(_index+1), str(tuple[_index]))
+        string = string.replace("$" + text(_index+1), tuple_strings[_index])
     return string
 
 def eliminate_duplicates(list):
@@ -143,40 +150,52 @@
             code: the code or the corrected code
     """
     _is_valid = True
-    if not isinstance(code, str):
-        print("Not a string, code: " + code + type(code) )
+    if not isinstance(code, text_type):
+        _tuni = _("Not a text string, code: $1, type: $2")
+        _uni = mapping(_tuni, (code ,text(type(code))))
+        print(_uni)
         return False, False
     if code == "":
         return False, False
     try:
-        _unicode_code = unicode(code, "utf8",'replace')
-        _code_utf8 = _unicode_code.encode("utf8",'replace')
+        #_unicode_code = unicode(code, "utf-8",'replace')
+        _unicode_code = code
+        _code_utf8 = _unicode_code.encode("utf-8",'replace')
         _code_cp850 = _unicode_code.encode("cp850",'replace')
-        _unicode_code = unicode(_code_cp850, "cp850",'replace')
+        _unicode_code = text(_code_cp850, "cp850",'replace')
 
     except UnicodeError:
-        print ("Unicode Error, code: " +  code )
+        _tuni = _("Unicode Error, code: $1")
+        _uni = mapping(_tuni, (code, ))
+        print (_uni)
         return False, False
-    if _code_utf8 != code:
-        print ("Not in cp950, code: " +  code )
+    if code != _unicode_code:
+        _tuni = _("Not in cp850, code: $1")
+        _uni = mapping(_tuni, (code, ))
+        print (_uni)
         _is_valid = False
         if _code_utf8 == "":
             return False, False
         code = _code_utf8
     _code2 = re.sub("[\t \n\r~|\\\]","",code)
     if _code2 != code:
-        print("Control characters in code: " + code )
+        _tuni = _("Control characters in code: $1")
+        _uni = mapping(_tuni, (code, ))
+        print(_uni)
         if _code2 == "":
             return False, False
         _is_valid = False
         code = _code2
     if code[-1] == "#":
-        print("# in code: " + code )
-        _is_valid =  False
+        _tuni = _("# in code: $1")
+        _uni = mapping(_tuni, (code, ))
+        print(_uni)
+        _is_valid = False
         while code[-1] == "#":
             code = code[:-1]
     if code == "":
-        print("Empty code")
+        _tuni = _("Empty code")
+        print(_tuni)
         return False, False
     return _is_valid, code