forked from doloopwhile/Python-CoffeeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsole.py
More file actions
57 lines (48 loc) · 1.85 KB
/
Copy pathconsole.py
File metadata and controls
57 lines (48 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
Copyright (c) 2011 Evgeny V. Generalov.
mailto:e.generalov@gmail.com
"""
import sys
import locale
class Writer(object):
"""text writer"""
def __init__(self, output=None, out_encoding=None):
self.out = None
self.out_encoding = out_encoding
self.set_output(output)
def set_output(self, output=None):
"""set output stream"""
self.out = output or sys.stdout
if type(self.out) is str or type(self.out) is unicode:
self.out = open(self.out, 'w')
def write(self, string):
"""write a string to the output"""
self.out.write(self._encode(string))
def writeln(self, string):
"""write a line to the output"""
print >> self.out, self._encode(string)
def _encode(self, data):
"""encode data to string"""
# py3k streams handle their encoding :
if sys.version_info >= (3, 0):
return data
if not isinstance(data, unicode):
return data
# data is string
encoding = (self.out_encoding or
getattr(self.out, 'encoding', None) or
locale.getdefaultlocale()[1] or
sys.getdefaultencoding())
return data.encode(encoding)