Add unicode-capable IO to hooke.ui.commandline.
[hooke.git] / hooke / util / encoding.py
1 # Copyright
2
3 """Define useful encoding-extraction functions.
4 """
5
6 import locale
7 import sys
8
9
10 def get_encoding():
11     """Guess a useful input/output/filesystem encoding.
12
13     Maybe we need seperate encodings for input/output and filesystem?
14     Hmm.
15     """
16     encoding = locale.getpreferredencoding() or sys.getdefaultencoding()
17     if sys.platform != 'win32' or sys.version_info[:2] > (2, 3):
18         encoding = locale.getlocale(locale.LC_TIME)[1] or encoding
19         # Python 2.3 on windows doesn't know about 'XYZ' alias for 'cpXYZ'
20     return encoding
21
22 def get_input_encoding():
23     "Guess the input encoding."
24     return get_encoding()
25
26 def get_output_encoding():
27     "Guess the output encoding."
28     return get_encoding()
29
30 def get_filesystem_encoding():
31     "Guess the filesystem encoding."
32     return get_encoding()