1 # Copyright (C) 2008-2010 Gianluca Montecchi <gian@grys.it>
2 # W. Trevor King <wking@drexel.edu>
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.
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.
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.
19 Support input/output/filesystem encodings (e.g. UTF-8).
28 if libbe.TESTING == True:
32 ENCODING = None # override get_encoding() output by setting this
36 Guess a useful input/output/filesystem encoding... Maybe we need
37 seperate encodings for input/output and filesystem? Hmm...
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'
47 def get_input_encoding():
50 def get_output_encoding():
53 def get_filesystem_encoding():
56 def known_encoding(encoding):
58 >>> known_encoding("highly-unlikely-encoding")
60 >>> known_encoding(get_encoding())
64 codecs.lookup(encoding)
69 def get_file_contents(path, mode='r', encoding=None, decode=False):
72 encoding = get_filesystem_encoding()
73 f = codecs.open(path, mode, encoding)
80 def set_file_contents(path, contents, mode='w', encoding=None):
81 if type(contents) == types.UnicodeType:
83 encoding = get_filesystem_encoding()
84 f = codecs.open(path, mode, encoding)
90 if libbe.TESTING == True:
91 suite = doctest.DocTestSuite()