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
23 from libxyz.parser import Lexer
24 from libxyz.exceptions import XYZValueError
25
27 """
28 Return unicode string
29 """
30
31 if isinstance(string, unicode):
32 return string
33
34 if enc is None:
35 enc = xyzenc
36
37 if not isinstance(string, str):
38 return unicode(string)
39
40
41 return string.decode(enc, 'replace')
42
43
44
46 """
47 Return encoded byte string
48 """
49
50 if isinstance(bstr, str):
51 return bstr
52
53 if enc is None:
54 enc = xyzenc
55
56 if not isinstance(bstr, unicode):
57 return str(bstr)
58
59 return bstr.encode(enc, 'replace')
60
61
62
64 """
65 Return current terminal settings
66 """
67
68 stdin = sys.stdin.fileno()
69
70
71 if not os.isatty(stdin):
72 return None
73
74 return termios.tcgetattr(stdin)
75
76
77
79 """
80 Terminal initialization
81 @return: Old terminal settings
82 """
83
84 term = term_settings()
85 stdin = sys.stdin.fileno()
86
87 if term is None:
88 return None
89
90 try:
91 vdisable = os.fpathconf(stdin, "PC_VDISABLE")
92 except ValueError:
93 return
94
95 _saved_term = copy.deepcopy(term[-1])
96
97
98 _todisable = [getattr(termios, x) for x in ("VQUIT",
99 "VINTR",
100 "VSUSP",
101 "VLNEXT",
102 "VSTART",
103 "VSTOP",
104 "VDISCARD",
105 )]
106
107 for _key in _todisable:
108 term[-1][_key] = vdisable
109
110 termios.tcsetattr(stdin, termios.TCSANOW, term)
111
112 return _saved_term
113
114
115
117 """
118 Restore terminal settings
119 """
120
121 stdin = sys.stdin.fileno()
122
123 term = term_settings()
124
125 if term is None:
126 return None
127
128 term[-1] = term_data
129
130 if os.isatty(stdin):
131 termios.tcsetattr(stdin, termios.TCSANOW, term)
132
133
134
136 """
137 Check if object is of function type
138 """
139
140 return isinstance(obj, types.FunctionType) or \
141 isinstance(obj, types.MethodType)
142
143
144
146 """
147 Find intersection between two strings
148 """
149
150 s1 = ustring(s1)
151 s2 = ustring(s2)
152
153 for index in range(len(s2) - 1, 0, -1):
154 if s1.endswith(s2[:index]):
155 return s2[index:]
156
157 return s2
158
159
160
181
182
183
185 msg = kw.get('msg', (u"Invalid argument type %s"))
186 error = kw.get('error', XYZValueError)
187
188 def _wrap1(fun):
189 def _wrap2(*args, **kwargs):
190 for t, arg in zip(types, args):
191 if t is not None and not isinstance(arg, t):
192 raise error(msg % str(type(arg)))
193
194 return fun(*args, **kwargs)
195
196 return _wrap2
197
198 return _wrap1
199