2010-07-08 20:01:03 +02:00
|
|
|
#!/usr/bin/python
|
2010-07-04 03:05:16 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2010-01-21 17:12:45 +01:00
|
|
|
|
|
|
|
#Copyright 2010 Mika Bostrom
|
|
|
|
#This program is free software: you can redistribute it and/or modify
|
|
|
|
#it under the terms of the GNU Affero General Public License as published by
|
|
|
|
#the Free Software Foundation, version 3 of the License.
|
|
|
|
#
|
|
|
|
#This program is distributed in the hope that it will be useful,
|
|
|
|
#but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
#GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
#You should have received a copy of the GNU Affero General Public License
|
|
|
|
#along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2010-07-04 03:05:16 +02:00
|
|
|
#In the "official" distribution you can find the license in agpl-3.0.txt.
|
2010-01-21 17:12:45 +01:00
|
|
|
|
2010-01-24 21:17:03 +01:00
|
|
|
# Error logging
|
|
|
|
import sys
|
|
|
|
|
2010-01-21 17:12:45 +01:00
|
|
|
# String manipulation
|
|
|
|
import codecs
|
|
|
|
|
|
|
|
# Settings
|
|
|
|
import Configuration
|
|
|
|
|
|
|
|
encoder_to_utf = codecs.lookup('utf-8')
|
|
|
|
encoder_to_sys = codecs.lookup(Configuration.LOCALE_ENCODING)
|
2010-06-03 23:36:59 +02:00
|
|
|
coder_hex = codecs.lookup('hex_codec')
|
2010-01-21 17:12:45 +01:00
|
|
|
|
2010-06-03 23:36:59 +02:00
|
|
|
hex_coding = False #FIXME: Should only be on if db is not UTF8 - test in Database.py?
|
2010-01-21 20:23:13 +01:00
|
|
|
# I'm saving a few cycles with this one
|
2010-01-30 12:05:34 +01:00
|
|
|
not_needed1, not_needed2, not_needed3 = False, False, False
|
2010-01-26 07:01:46 +01:00
|
|
|
if Configuration.LOCALE_ENCODING == 'UTF8':
|
2010-01-30 12:05:34 +01:00
|
|
|
not_needed1, not_needed2, not_needed3 = True, True, True
|
2010-01-21 20:23:13 +01:00
|
|
|
|
2010-01-21 17:12:45 +01:00
|
|
|
def to_utf8(s):
|
2010-01-30 12:05:34 +01:00
|
|
|
if not_needed1: return s
|
2010-01-21 20:31:19 +01:00
|
|
|
|
|
|
|
try:
|
2010-01-21 20:46:14 +01:00
|
|
|
#(_out, _len) = encoder_to_utf.encode(s)
|
|
|
|
_out = unicode(s, Configuration.LOCALE_ENCODING).encode('utf-8')
|
2010-01-21 20:31:19 +01:00
|
|
|
return _out
|
|
|
|
except UnicodeDecodeError:
|
2010-08-15 07:27:53 +02:00
|
|
|
sys.stderr.write(_('Could not convert: "%s"\n') % s)
|
2010-01-21 20:31:19 +01:00
|
|
|
raise
|
2010-02-07 00:12:11 +01:00
|
|
|
except UnicodeEncodeError:
|
2010-08-15 07:27:53 +02:00
|
|
|
sys.stderr.write(_('Could not encode: "%s"\n') % s)
|
2010-02-07 00:12:11 +01:00
|
|
|
raise
|
2010-01-29 18:01:51 +01:00
|
|
|
except TypeError: # TypeError is raised when we give unicode() an already encoded string
|
|
|
|
return s
|
2010-01-21 17:12:45 +01:00
|
|
|
|
2010-01-24 20:09:30 +01:00
|
|
|
def to_db_utf8(s):
|
2010-01-30 12:05:34 +01:00
|
|
|
if not_needed2: return s
|
2010-01-24 20:09:30 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
(_out, _len) = encoder_to_utf.encode(unicode(s))
|
|
|
|
return _out
|
|
|
|
except UnicodeDecodeError:
|
2010-08-15 07:27:53 +02:00
|
|
|
sys.stderr.write(_('Could not convert: "%s"\n') % s)
|
2010-01-24 20:09:30 +01:00
|
|
|
raise
|
2010-02-07 00:12:11 +01:00
|
|
|
except UnicodeEncodeError:
|
2010-08-15 07:27:53 +02:00
|
|
|
sys.stderr.write(_('Could not encode: "%s"\n') % s)
|
2010-02-07 00:12:11 +01:00
|
|
|
raise
|
2010-01-24 20:09:30 +01:00
|
|
|
|
2010-01-21 17:12:45 +01:00
|
|
|
def to_gui(s):
|
2010-01-30 12:05:34 +01:00
|
|
|
if not_needed3: return s
|
2010-01-21 20:31:19 +01:00
|
|
|
|
|
|
|
try:
|
2010-02-07 00:12:11 +01:00
|
|
|
# we usually don't want to use 'replace' but this is only for displaying
|
|
|
|
# in the gui so it doesn't matter if names are missing an accent or two
|
|
|
|
(_out, _len) = encoder_to_sys.encode(s, 'replace')
|
2010-01-21 20:31:19 +01:00
|
|
|
return _out
|
|
|
|
except UnicodeDecodeError:
|
2010-08-15 07:27:53 +02:00
|
|
|
sys.stderr.write(_('Could not convert: "%s"\n') % s)
|
2010-01-21 20:31:19 +01:00
|
|
|
raise
|
2010-02-07 00:12:11 +01:00
|
|
|
except UnicodeEncodeError:
|
2010-08-15 07:27:53 +02:00
|
|
|
sys.stderr.write(_('Could not encode: "%s"\n') % s)
|
2010-02-07 00:12:11 +01:00
|
|
|
raise
|
2010-06-03 23:36:59 +02:00
|
|
|
|
|
|
|
def to_hex(s):
|
|
|
|
try:
|
|
|
|
out = coder_hex.encode(s)[0]
|
|
|
|
return out
|
|
|
|
except UnicodeDecodeError:
|
2010-08-15 07:27:53 +02:00
|
|
|
sys.stderr.write(_('Could not convert: "%s"\n') % s)
|
2010-06-03 23:36:59 +02:00
|
|
|
return s
|
|
|
|
|
|
|
|
def from_hex(s):
|
|
|
|
try:
|
|
|
|
out = coder_hex.decode(s)[0]
|
|
|
|
return out
|
|
|
|
except UnicodeDecodeError:
|
2010-08-15 07:27:53 +02:00
|
|
|
sys.stderr.write(_('Could not convert: "%s"\n') % s)
|
2010-06-03 23:36:59 +02:00
|
|
|
return s
|