Bumped to version 1.0.1
[be.git] / libbe / util / encoding.py
1 # Copyright (C) 2008-2011 Chris Ball <cjb@laptop.org>
2 #                         Gianluca Montecchi <gian@grys.it>
3 #                         W. Trevor King <wking@drexel.edu>
4 #
5 # This file is part of Bugs Everywhere.
6 #
7 # Bugs Everywhere is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by the
9 # Free Software Foundation, either version 2 of the License, or (at your
10 # option) any later version.
11 #
12 # Bugs Everywhere is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 # General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Bugs Everywhere.  If not, see <http://www.gnu.org/licenses/>.
19
20 """
21 Support input/output/filesystem encodings (e.g. UTF-8).
22 """
23
24 import codecs
25 import locale
26 import sys
27 import types
28
29 import libbe
30 if libbe.TESTING == True:
31     import doctest
32
33
34 ENCODING = None # override get_encoding() output by setting this
35
36 def get_encoding():
37     """
38     Guess a useful input/output/filesystem encoding...  Maybe we need
39     seperate encodings for input/output and filesystem?  Hmm...
40     """
41     if ENCODING != None:
42         return ENCODING
43     encoding = locale.getpreferredencoding() or sys.getdefaultencoding()
44     if sys.platform != 'win32' or sys.version_info[:2] > (2, 3):
45         encoding = locale.getlocale(locale.LC_TIME)[1] or encoding
46         # Python 2.3 on windows doesn't know about 'XYZ' alias for 'cpXYZ'
47     return encoding
48
49 def get_input_encoding():
50     return get_encoding()
51
52 def get_output_encoding():
53     return get_encoding()
54
55 def get_filesystem_encoding():
56     return get_encoding()
57
58 def known_encoding(encoding):
59     """
60     >>> known_encoding("highly-unlikely-encoding")
61     False
62     >>> known_encoding(get_encoding())
63     True
64     """
65     try:
66         codecs.lookup(encoding)
67         return True
68     except LookupError:
69         return False
70
71 def get_file_contents(path, mode='r', encoding=None, decode=False):
72     if decode == True:
73         if encoding == None:
74             encoding = get_filesystem_encoding()
75         f = codecs.open(path, mode, encoding)
76     else:
77         f = open(path, mode)
78     contents = f.read()
79     f.close()
80     return contents
81
82 def set_file_contents(path, contents, mode='w', encoding=None):
83     if type(contents) == types.UnicodeType:
84         if encoding == None:
85             encoding = get_filesystem_encoding()
86         f = codecs.open(path, mode, encoding)
87     else:
88         f = open(path, mode)
89     f.write(contents)
90     f.close()
91
92 if libbe.TESTING == True:
93     suite = doctest.DocTestSuite()