Coverage for Charset.py: 44%

43 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-09-28 16:41 +0000

1#!/usr/bin/env python 

2# -*- coding: utf-8 -*- 

3 

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. 

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 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. 

17 

18 

19#import L10n 

20#_ = L10n.get_translation() 

21 

22# Error logging 

23import sys 

24 

25# String manipulation 

26import codecs 

27 

28# Settings 

29 

30import Configuration 

31 

32encoder_to_utf = codecs.lookup('utf-8') 

33encoder_to_sys = codecs.lookup(Configuration.LOCALE_ENCODING) 

34 

35# I'm saving a few cycles with this one 

36not_needed1, not_needed2, not_needed3 = False, False, False 

37if Configuration.LOCALE_ENCODING == 'UTF8': 

38 not_needed1, not_needed2, not_needed3 = True, True, True 

39 

40def to_utf8(s): 

41 if not_needed1: return s 

42 

43 try: 

44 #(_out, _len) = encoder_to_utf.encode(s) 

45 _out = str(s, Configuration.LOCALE_ENCODING).encode('utf-8') 

46 return _out 

47 except UnicodeDecodeError: 

48 sys.stderr.write(('Could not convert: "%s"') % (s+"\n")) 

49 raise 

50 except UnicodeEncodeError: 

51 sys.stderr.write(('Could not encode: "%s"') % (s+"\n")) 

52 raise 

53 except TypeError: # TypeError is raised when we give unicode() an already encoded string 

54 return s 

55 

56def to_db_utf8(s): 

57 if not_needed2: return s 

58 

59 try: 

60 (_out, _len) = encoder_to_utf.encode(str(s)) 

61 return _out 

62 except UnicodeDecodeError: 

63 sys.stderr.write(('Could not convert: "%s"') % (s+"\n")) 

64 raise 

65 except UnicodeEncodeError: 

66 sys.stderr.write(('Could not encode: "%s"') % (s+"\n")) 

67 raise 

68 

69def to_gui(s): 

70 if not_needed3: return s 

71 

72 try: 

73 # we usually don't want to use 'replace' but this is only for displaying 

74 # in the gui so it doesn't matter if names are missing an accent or two 

75 (_out, _len) = encoder_to_sys.encode(s, 'replace') 

76 return _out 

77 except UnicodeDecodeError: 

78 sys.stderr.write(('Could not convert: "%s"') % (s+"\n")) 

79 raise 

80 except UnicodeEncodeError: 

81 sys.stderr.write(('Could not encode: "%s"') % (s+"\n")) 

82 raise