ef928526b2d4e28973e24d50debc8670f6729831
[hooke.git] / hooke / util / encoding.py
1 # Copyright (C) 2010-2011 W. Trevor King <wking@drexel.edu>
2 #
3 # This file is part of Hooke.
4 #
5 # Hooke is free software: you can redistribute it and/or modify it
6 # under the terms of the GNU Lesser General Public License as
7 # published by the Free Software Foundation, either version 3 of the
8 # License, or (at your option) any later version.
9 #
10 # Hooke is distributed in the hope that it will be useful, but WITHOUT
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 # or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General
13 # Public License for more details.
14 #
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with Hooke.  If not, see
17 # <http://www.gnu.org/licenses/>.
18
19 """Define useful encoding-extraction functions.
20 """
21
22 import locale
23 import sys
24
25
26 def get_encoding():
27     """Guess a useful input/output/filesystem encoding.
28
29     Maybe we need seperate encodings for input/output and filesystem?
30     Hmm.
31     """
32     encoding = locale.getpreferredencoding() or sys.getdefaultencoding()
33     if sys.platform != 'win32' or sys.version_info[:2] > (2, 3):
34         encoding = locale.getlocale(locale.LC_TIME)[1] or encoding
35         # Python 2.3 on windows doesn't know about 'XYZ' alias for 'cpXYZ'
36     return encoding
37
38 def get_input_encoding():
39     "Guess the input encoding."
40     return get_encoding()
41
42 def get_output_encoding():
43     "Guess the output encoding."
44     return get_encoding()
45
46 def get_filesystem_encoding():
47     "Guess the filesystem encoding."
48     return get_encoding()