Reported bug with utf-8 strings
[be.git] / libbe / util / encoding.py
1 # Copyright (C) 2008-2010 Gianluca Montecchi <gian@grys.it>
2 #                         W. Trevor King <wking@drexel.edu>
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 2 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 along
15 # with this program; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
18 """
19 Support input/output/filesystem encodings (e.g. UTF-8).
20 """
21
22 import codecs
23 import locale
24 import sys
25 import types
26
27 import libbe
28 if libbe.TESTING == True:
29     import doctest
30
31
32 ENCODING = None # override get_encoding() output by setting this
33
34 def get_encoding():
35     """
36     Guess a useful input/output/filesystem encoding...  Maybe we need
37     seperate encodings for input/output and filesystem?  Hmm...
38     """
39     if ENCODING != None:
40         return ENCODING
41     encoding = locale.getpreferredencoding() or sys.getdefaultencoding()
42     if sys.platform != 'win32' or sys.version_info[:2] > (2, 3):
43         encoding = locale.getlocale(locale.LC_TIME)[1] or encoding
44         # Python 2.3 on windows doesn't know about 'XYZ' alias for 'cpXYZ'
45     return encoding
46
47 def get_input_encoding():
48     return get_encoding()
49
50 def get_output_encoding():
51     return get_encoding()
52
53 def get_filesystem_encoding():
54     return get_encoding()
55
56 def known_encoding(encoding):
57     """
58     >>> known_encoding("highly-unlikely-encoding")
59     False
60     >>> known_encoding(get_encoding())
61     True
62     """
63     try:
64         codecs.lookup(encoding)
65         return True
66     except LookupError:
67         return False
68
69 def get_file_contents(path, mode='r', encoding=None, decode=False):
70     if decode == True:
71         if encoding == None:
72             encoding = get_filesystem_encoding()
73         f = codecs.open(path, mode, encoding)
74     else:
75         f = open(path, mode)
76     contents = f.read()
77     f.close()
78     return contents
79
80 def set_file_contents(path, contents, mode='w', encoding=None):
81     if type(contents) == types.UnicodeType:
82         if encoding == None:
83             encoding = get_filesystem_encoding()
84         f = codecs.open(path, mode, encoding)
85     else:
86         f = open(path, mode)
87     f.write(contents)
88     f.close()
89
90 if libbe.TESTING == True:
91     suite = doctest.DocTestSuite()