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
26 """
27 Return unicode string
28 """
29
30 if isinstance(string, unicode):
31 return string
32
33 if enc is None:
34 enc = xyzenc
35
36 if not isinstance(string, str):
37 return unicode(string)
38
39
40 return string.decode(enc, 'replace')
41
42
43
45 """
46 Return encoded byte string
47 """
48
49 if isinstance(bstr, str):
50 return bstr
51
52 if enc is None:
53 enc = xyzenc
54
55 if not isinstance(bstr, unicode):
56 return str(bstr)
57
58 return bstr.encode(enc, 'replace')
59
60
61
63 """
64 Return current terminal settings
65 """
66
67 stdin = sys.stdin.fileno()
68
69
70 if not os.isatty(stdin):
71 return None
72
73 return termios.tcgetattr(stdin)
74
75
76
78 """
79 Terminal initialization
80 @return: Old terminal settings
81 """
82
83 term = term_settings()
84 stdin = sys.stdin.fileno()
85
86 if term is None:
87 return None
88
89 try:
90 vdisable = os.fpathconf(stdin, "PC_VDISABLE")
91 except ValueError:
92 return
93
94 _saved_term = copy.deepcopy(term[-1])
95
96
97 _todisable = [getattr(termios, x) for x in ("VQUIT",
98 "VINTR",
99 "VSUSP",
100 "VLNEXT",
101 "VSTART",
102 "VSTOP",
103 "VDISCARD",
104 )]
105
106 for _key in _todisable:
107 term[-1][_key] = vdisable
108
109 termios.tcsetattr(stdin, termios.TCSANOW, term)
110
111 return _saved_term
112
113
114
116 """
117 Restore terminal settings
118 """
119
120 stdin = sys.stdin.fileno()
121
122 term = term_settings()
123
124 if term is None:
125 return None
126
127 term[-1] = term_data
128
129 if os.isatty(stdin):
130 termios.tcsetattr(stdin, termios.TCSANOW, term)
131
132
133
135 """
136 Check if object is of function type
137 """
138
139 return isinstance(obj, types.FunctionType) or \
140 isinstance(obj, types.MethodType)
141
142
143
145 """
146 Find intersection between two strings
147 """
148
149 s1 = ustring(s1)
150 s2 = ustring(s2)
151
152 for index in range(len(s2) - 1, 0, -1):
153 if s1.endswith(s2[:index]):
154 return s2[index:]
155
156 return s2
157
158
159
180