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