Mercurial > xib
comparison encoding.py @ 0:4c842d23d4ce
Initial commit, version 0.1
Signed-off-by: Charly COSTE <changaco@changaco.net>
author | Charly COSTE <changaco@changaco.net> |
---|---|
date | Sun, 16 Aug 2009 01:47:03 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c842d23d4ce |
---|---|
1 #!/usr/bin/env python | |
2 # -*- coding: utf-8 -*- | |
3 | |
4 # This program is free software: you can redistribute it and/or modify | |
5 # it under the terms of the GNU General Public License as published by | |
6 # the Free Software Foundation, either version 3 of the License, or | |
7 # (at your option) any later version. | |
8 # | |
9 # This program is distributed in the hope that it will be useful, | |
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 # GNU General Public License for more details. | |
13 # | |
14 # You should have received a copy of the GNU General Public License | |
15 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
16 | |
17 | |
18 class EncodingException(Exception): pass | |
19 | |
20 | |
21 def auto_encode(s): | |
22 for codec in ['utf-8', 'iso8859_15']: | |
23 try: | |
24 return s.encode(codec) | |
25 except (UnicodeEncodeError, UnicodeDecodeError) as e: | |
26 print e | |
27 pass | |
28 raise EncodingException('no suitable codec found') | |
29 | |
30 | |
31 def auto_decode(s): | |
32 for codec in ['utf-8', 'iso8859_15']: | |
33 try: | |
34 return s.decode(codec) | |
35 except (UnicodeEncodeError, UnicodeDecodeError) as e: | |
36 print e | |
37 pass | |
38 raise EncodingException('no suitable codec found') |