-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconsolemsg.py
More file actions
executable file
·92 lines (73 loc) · 2.72 KB
/
consolemsg.py
File metadata and controls
executable file
·92 lines (73 loc) · 2.72 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/env python
# -*- encoding: utf8 -*-
def _decoratedStream(stream):
# respect the encoding if set
if stream.encoding:
return stream
# if not use utf8
import codecs
return codecs.getwriter('utf8')(stream)
def stderr():
if hasattr(stderr, 'cached'):
return stderr.cached
import sys
stderr.cached = _decoratedStream(sys.stderr)
return stderr.cached
def stdout():
if hasattr(stdout, 'cached'):
return stdout.cached
import sys
stdout.cached = _decoratedStream(sys.stdout)
return stdout.cached
def b(text):
if type(text) == type(b''): return text
if type(text) == type(u''): return text.encode('utf8')
return type(u'')(text).encode('utf8')
def u(text):
if type(text) == type(u''): return text
if type(text) == type(b''): return text.decode('utf8')
return type(u'')(text)
def printStdError(*args) :
stderr().write(u' '.join(u(arg) for arg in args))
stderr().write(u'\n')
stderr().flush()
def printStdOut(*args) :
stdout().write(u' '.join(u(arg) for arg in args))
stdout().write(u'\n')
stdout().flush()
def color(color, message, *args, **kwds):
if args or kwds:
message=u(message).format(*args,**kwds)
return u"\033[{0}m{1}\033[0m".format(u(color),u(message))
def success(message, *args, **kwds):
printStdError(color('32;1', ">> "+u(message), *args, **kwds))
def step(message, *args, **kwds):
printStdError(color('34;1', ":: "+u(message), *args, **kwds))
def error(message, *args, **kwds):
printStdError(color('31;1', "Error: "+u(message), *args, **kwds))
def warn(message, *args, **kwds):
printStdError(color('33', "Warning: "+u(message), *args, **kwds))
def fail(message, code=-1, *args, **kwds):
error(message, *args, **kwds)
import sys
sys.exit(code)
def out(message, *args, **kwds):
printStdOut(u(message).format(*args,**kwds))
if __name__ == "__main__":
step('Testing common messages')
success('Success!')
warn('This might be dangerous')
error('Something bad happened')
step('Testing common messages')
success('{} did this', 'Joe')
warn('{thing} might be dangerous', thing="Swords")
# Parameters
error(u'Unicode castaña unicode param {what}', what=u"castañin") # Beyond ASCIII use unicode
error(b'Encoded casta\xc3\xb1a unicode param {what}', what=u"castañin") # Some permisivity with templates, not with params
error('{what} happened', what="Murphy")
error(2323) # Message is converted to unicode
out('This goes {} to the output', 'undecorated')
out(u'Supporting castañas as well')
out(b'Supporting encoded casta\xc3\xb1as as well')
fail('Something very bad happened and i will die. {}', -2, 'Bye')
# vim: et ts=4 sw=4