1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 import sys
18 import os
19 import termios
20 import copy
21 import types
22
24 """
25 Return unicode string
26 """
27
28 if isinstance(string, unicode):
29 return string
30
31 if enc is None:
32 enc = xyzenc
33
34 if not isinstance(string, str):
35 return unicode(string)
36
37
38 return string.decode(enc, 'replace')
39
40
41
43 """
44 Return encoded byte string
45 """
46
47 if isinstance(bstr, str):
48 return bstr
49
50 if enc is None:
51 enc = xyzenc
52
53 if not isinstance(bstr, unicode):
54 return str(bstr)
55
56 return bstr.encode(enc, 'replace')
57
58
59
61 """
62 Return current terminal settings
63 """
64
65 stdin = sys.stdin.fileno()
66
67
68 if not os.isatty(stdin):
69 return None
70
71 return termios.tcgetattr(stdin)
72
73
74
76 """
77 Terminal initialization
78 @return: Old terminal settings
79 """
80
81 term = term_settings()
82 stdin = sys.stdin.fileno()
83
84 if term is None:
85 return None
86
87 try:
88 vdisable = os.fpathconf(stdin, "PC_VDISABLE")
89 except ValueError:
90 return
91
92 _saved_term = copy.deepcopy(term[-1])
93
94
95 _todisable = [getattr(termios, x) for x in ("VQUIT",
96 "VINTR",
97 "VSUSP",
98 "VLNEXT",
99 "VSTART",
100 "VSTOP",
101 "VDISCARD",
102 )]
103
104 for _key in _todisable:
105 term[-1][_key] = vdisable
106
107 termios.tcsetattr(stdin, termios.TCSANOW, term)
108
109 return _saved_term
110
111
112
114 """
115 Restore terminal settings
116 """
117
118 stdin = sys.stdin.fileno()
119
120 term = term_settings()
121
122 if term is None:
123 return None
124
125 term[-1] = term_data
126
127 if os.isatty(stdin):
128 termios.tcsetattr(stdin, termios.TCSANOW, term)
129
130
131
133 """
134 Check if object is of function type
135 """
136
137 return isinstance(obj, types.FunctionType) or \
138 isinstance(obj, types.MethodType)
139