Mercurial > pyarq-presupuestos
comparison Generic/utils.py @ 17:a7b9f7e7dfa4
Improvements importing FIEBDC files
| author | Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es> |
|---|---|
| date | Sat, 02 Nov 2013 19:26:09 +0100 |
| parents | 2ac1551ad2ab |
| children | 65e7ae0d0e63 |
comparison
equal
deleted
inserted
replaced
| 16:60bc5117926c | 17:a7b9f7e7dfa4 |
|---|---|
| 1 #!/usr/bin/python | 1 #!/usr/bin/python |
| 2 # -*- coding: utf-8 -*- | 2 # -*- coding: utf-8 -*- |
| 3 ## File utils.py | 3 ## File utils.py |
| 4 ## This file is part of pyArq-Presupuestos. | 4 ## This file is part of pyArq-Presupuestos. |
| 5 ## | 5 ## |
| 6 ## Copyright (C) 2010 Miguel Ángel Bárcena Rodríguez | 6 ## Copyright (C) 2010-2013 Miguel Ángel Bárcena Rodríguez |
| 7 ## <miguelangel@obraencurso.es> | 7 ## <miguelangel@obraencurso.es> |
| 8 ## | 8 ## |
| 9 ## pyArq-Presupuestos is free software: you can redistribute it and/or modify | 9 ## pyArq-Presupuestos is free software: you can redistribute it and/or modify |
| 10 ## it under the terms of the GNU General Public License as published by | 10 ## it under the terms of the GNU General Public License as published by |
| 11 ## the Free Software Foundation, either version 3 of the License, or | 11 ## the Free Software Foundation, either version 3 of the License, or |
| 109 False: the code is not valid and can not be corrected | 109 False: the code is not valid and can not be corrected |
| 110 code: the code or the corrected code | 110 code: the code or the corrected code |
| 111 """ | 111 """ |
| 112 _is_valid = True | 112 _is_valid = True |
| 113 if not isinstance(code, str): | 113 if not isinstance(code, str): |
| 114 print "Not a string, code:", code, type(code) | |
| 114 return False, False | 115 return False, False |
| 115 if code == "": | 116 if code == "": |
| 117 | |
| 116 return False, False | 118 return False, False |
| 117 try: | 119 try: |
| 118 _unicode_code = unicode(code, "utf8") | 120 _unicode_code = unicode(code, "utf8",'replace') |
| 119 _code_cp850 = _unicode_code.encode("cp850") | 121 _code_utf8 = _unicode_code.encode("utf8",'replace') |
| 120 _unicode_code = unicode(_code_cp850, "cp850") | 122 _code_cp850 = _unicode_code.encode("cp850",'replace') |
| 121 _code_utf8 = _unicode_code.encode("utf8") | 123 _unicode_code = unicode(_code_cp850, "cp850",'replace') |
| 124 | |
| 122 except UnicodeError: | 125 except UnicodeError: |
| 126 print "Unicode Error, code:", code | |
| 123 return False, False | 127 return False, False |
| 124 if _code_utf8 != code: | 128 if _code_utf8 != code: |
| 129 print "Not in cp950, code:", code | |
| 125 _is_valid = False | 130 _is_valid = False |
| 126 if _code_utf8 == "": | 131 if _code_utf8 == "": |
| 127 return False, False | 132 return False, False |
| 128 code = _code_utf8 | 133 code = _code_utf8 |
| 129 _code2 = re.sub("[\t \n\r~|\\\]","",code) | 134 _code2 = re.sub("[\t \n\r~|\\\]","",code) |
| 130 if _code2 != code: | 135 if _code2 != code: |
| 136 print "Control characters in code:", code | |
| 131 if _code2 == "": | 137 if _code2 == "": |
| 132 return False, False | 138 return False, False |
| 133 _is_valid = False | 139 _is_valid = False |
| 134 code = _code2 | 140 code = _code2 |
| 135 if code[-1] == "#": | 141 if code[-1] == "#": |
| 142 print "# in code:", code | |
| 136 _is_valid = False | 143 _is_valid = False |
| 137 while code[-1] == "#": | 144 while code[-1] == "#": |
| 138 code = code[:-1] | 145 code = code[:-1] |
| 139 if code == "": | 146 if code == "": |
| 147 print "Empty code" | |
| 140 return False, False | 148 return False, False |
| 141 return _is_valid, code | 149 return _is_valid, code |
| 142 | 150 |
| 143 def getFiletype(filename, h=None): | 151 def getFiletype(filename, h=None): |
| 144 """getFiletype(filename, h=None): | 152 """getFiletype(filename, h=None): |
