1 # Copyright (C) 2008-2011 Chris Ball <cjb@laptop.org>
2 # Gianluca Montecchi <gian@grys.it>
3 # W. Trevor King <wking@drexel.edu>
5 # This file is part of Bugs Everywhere.
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.
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.
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/>.
21 Support input/output/filesystem encodings (e.g. UTF-8).
30 if libbe.TESTING == True:
34 ENCODING = None # override get_encoding() output by setting this
38 Guess a useful input/output/filesystem encoding... Maybe we need
39 seperate encodings for input/output and filesystem? Hmm...
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'
49 def get_input_encoding():
52 def get_output_encoding():
55 def get_filesystem_encoding():
58 def known_encoding(encoding):
60 >>> known_encoding("highly-unlikely-encoding")
62 >>> known_encoding(get_encoding())
66 codecs.lookup(encoding)
71 def get_file_contents(path, mode='r', encoding=None, decode=False):
74 encoding = get_filesystem_encoding()
75 f = codecs.open(path, mode, encoding)
82 def set_file_contents(path, contents, mode='w', encoding=None):
83 if type(contents) == types.UnicodeType:
85 encoding = get_filesystem_encoding()
86 f = codecs.open(path, mode, encoding)
92 if libbe.TESTING == True:
93 suite = doctest.DocTestSuite()