diff Generic/utils.py @ 23:65e7ae0d0e63

GTK2 to GTK3
author Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
date Thu, 02 May 2019 16:31:17 +0200
parents a7b9f7e7dfa4
children 16f91684686b
line wrap: on
line diff
--- a/Generic/utils.py	Tue Sep 30 17:16:50 2014 +0200
+++ b/Generic/utils.py	Thu May 02 16:31:17 2019 +0200
@@ -3,7 +3,7 @@
 ## File utils.py
 ## This file is part of pyArq-Presupuestos.
 ##
-## Copyright (C) 2010-2013 Miguel Ángel Bárcena Rodríguez
+## Copyright (C) 2010-2019 Miguel Ángel Bárcena Rodríguez
 ##                         <miguelangel@obraencurso.es>
 ##
 ## pyArq-Presupuestos is free software: you can redistribute it and/or modify
@@ -22,6 +22,38 @@
 # Modules
 import re 
 import imghdr
+import os.path
+
+# add svg to imghdr
+def test_svg(h, f):
+    """SVG """
+    if  isinstance(f,file):
+        _pos = f.tell()
+        f.seek(0)
+        _h = f.read(32)
+        f.seek(-32, 2)
+        _l = f.read(32)
+        f.seek(_pos)
+    else:
+        _h = h
+        _l = h[-32:]
+    if "<?xml" in _h and "</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":
+        return 'ico'
+imghdr.tests.append(test_ico)
+
+# add wmf to imghdr
+def test_pdf(h, f):
+    """pdf file"""
+    if h[:4] == "%PDF":
+        return 'pdf'
+imghdr.tests.append(test_pdf)
 
 # add wmf to imghdr
 def test_wmf(h, f):
@@ -57,18 +89,19 @@
 imghdr.tests.append(test_dxf)
 
 
-def mapping(string, tuple):
+def mapping(string, tuple_strings):
     """mapping(string, tuple)
     
     string: a message string
-    tuple: a truple with string items
+    tuple_strings: a truple with string items
     Return the string replacing the $[n] words whith its corresponding value
     from the tuple.
     It is used because the gettext module can not #-#supotr#-# strings as:
         "Invalid type (%s) in record: %s" %(type, record)
     """
-    for _index in range(len(tuple)):
-        string = string.replace("$" + str(_index+1), str(tuple[_index]))
+    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]))
     return string
 
 def eliminate_duplicates(list):
@@ -111,10 +144,9 @@
     """
     _is_valid = True
     if not isinstance(code, str):
-        print "Not a string, code:", code, type(code)
+        print("Not a string, code: " + code + type(code) )
         return False, False
     if code == "":
-
         return False, False
     try:
         _unicode_code = unicode(code, "utf8",'replace')
@@ -123,28 +155,28 @@
         _unicode_code = unicode(_code_cp850, "cp850",'replace')
 
     except UnicodeError:
-        print "Unicode Error, code:", code
+        print ("Unicode Error, code: " +  code )
         return False, False
     if _code_utf8 != code:
-        print "Not in cp950, code:", code
+        print ("Not in cp950, code: " +  code )
         _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
+        print("Control characters in code: " + code )
         if _code2 == "":
             return False, False
         _is_valid = False
         code = _code2
     if code[-1] == "#":
-        print "# in code:", code
+        print("# in code: " + code )
         _is_valid =  False
         while code[-1] == "#":
             code = code[:-1]
     if code == "":
-        print "Empty code"
+        print("Empty code")
         return False, False
     return _is_valid, code
 
@@ -154,20 +186,48 @@
     filename: the filename to test
     h: raw string, if h is not None the filename is ignored and h is assumed
     to contain the byte stream to test
+    
+    valid types:
+     "image", "wmf", "dxf", "pdf" , "video",
+     "office-document", "office-presentation", "office-spreadsheet",
+     "html", "rtf", "txt"
     """
+    _ext = os.path.splitext(filename)[1][1:].lower()
+    
+    _video_types = ["avi", "mp4", "m4p", "m4v2", "m4v","amv", "mpg", "m2v",
+                    "mp2", "mpe", "mpv", "mpeg", "ogg", "ogv", "webm", "mkv",
+                    "ogm", "flv", "f4v", "f4p", "f4a", "f4b", "vob", "drc",
+                    "mts", "m2ts", "mov", "qt", "wmv", "yuv", "rm", "rmvb",
+                    "asf", "svi", "3gp", "3g2", "mxf", "roq", "nsv"]
+    _document_types = ["doc", "docx", "odt"]
+    _spreadsheet_types = ["xls", "xlsx", "ods"]
+    _presentation_types = ["pps", "ppsx", "ppt", "pptx", "odp"]
+    _html_types = ["html", "xhtml"]
+    if _ext in _video_types:
+        return "video"
+    elif _ext in _document_types:
+        return "office-document"
+    elif _ext in _spreadsheet_types:
+        return "office-spreadsheet"
+    elif _ext in _presentation_types:
+        return "office-presentation"
+    elif _ext in _html_types:
+        return "html"
+    elif _ext == "rtf":
+        return "rtf"
+    elif _ext == "txt":
+        return "txt"
     _type = imghdr.what(filename, h)
-    _image_types = ["rgb", "gif", "pbm", "pgm", "ppm", "tiff", "rast", "xbm",
-                    "jpeg", "bmp", "png", "wmf"]
-    if _type in _image_types:
+    _image_types = ["rgb", "gif", "pbm", "pgm", "ppm" ,"tiff", "tif", "rast",
+                    "xbm", "jpeg", "jpg", "bmp", "png", "webp", "exr",
+                    "ico", "svg"]
+    if _type in _image_types and _ext in _image_types:
         return "image"
-    elif _type == "dxf":
+    elif _type == "wmf" and _ext == "wmf":
+        return "wmf"
+    elif _type == "dxf" and _ext == "dxf":
         return "dxf"
-##    _video_types = ["avi", "mpg", "mkv", "ogm"]
-##    elif _type in _video_types:
-##        return "video"
-##    elif _type == "pdf":
-##        return "pdf"
-##    elif _type == "ppt" or _type == "odp":
-##        return "presentation"
-    else:
-        return None
+
+    elif _type == "pdf" and _ext == "pdf":
+        return "pdf"
+    return None