Another fix for the fixed limit Limits filter. This one is commented, so doesn't...
[fpdb-dooglus.git] / pyfpdb / Charset.py
blobf1581c737d13406af5eba3f3d8e907c1f2783eef
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
4 #Copyright 2010-2011 Mika Bostrom
5 #This program is free software: you can redistribute it and/or modify
6 #it under the terms of the GNU Affero General Public License as published by
7 #the Free Software Foundation, version 3 of the License.
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.
14 #You should have received a copy of the GNU Affero General Public License
15 #along with this program. If not, see <http://www.gnu.org/licenses/>.
16 #In the "official" distribution you can find the license in agpl-3.0.txt.
18 # Error logging
19 import sys
21 # String manipulation
22 import codecs
24 # Settings
25 import Configuration
27 encoder_to_utf = codecs.lookup('utf-8')
28 encoder_to_sys = codecs.lookup(Configuration.LOCALE_ENCODING)
30 # I'm saving a few cycles with this one
31 not_needed1, not_needed2, not_needed3 = False, False, False
32 if Configuration.LOCALE_ENCODING == 'UTF8':
33 not_needed1, not_needed2, not_needed3 = True, True, True
35 def to_utf8(s):
36 if not_needed1: return s
38 try:
39 #(_out, _len) = encoder_to_utf.encode(s)
40 _out = unicode(s, Configuration.LOCALE_ENCODING).encode('utf-8')
41 return _out
42 except UnicodeDecodeError:
43 sys.stderr.write(_('Could not convert: "%s"') % (s+"\n"))
44 raise
45 except UnicodeEncodeError:
46 sys.stderr.write(_('Could not encode: "%s"') % (s+"\n"))
47 raise
48 except TypeError: # TypeError is raised when we give unicode() an already encoded string
49 return s
51 def to_db_utf8(s):
52 if not_needed2: return s
54 try:
55 (_out, _len) = encoder_to_utf.encode(unicode(s))
56 return _out
57 except UnicodeDecodeError:
58 sys.stderr.write(_('Could not convert: "%s"') % (s+"\n"))
59 raise
60 except UnicodeEncodeError:
61 sys.stderr.write(_('Could not encode: "%s"') % (s+"\n"))
62 raise
64 def to_gui(s):
65 if not_needed3: return s
67 try:
68 # we usually don't want to use 'replace' but this is only for displaying
69 # in the gui so it doesn't matter if names are missing an accent or two
70 (_out, _len) = encoder_to_sys.encode(s, 'replace')
71 return _out
72 except UnicodeDecodeError:
73 sys.stderr.write(_('Could not convert: "%s"') % (s+"\n"))
74 raise
75 except UnicodeEncodeError:
76 sys.stderr.write(_('Could not encode: "%s"') % (s+"\n"))
77 raise