forked from joesitton/statusbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ts3utils.py
53 lines (47 loc) · 1.08 KB
/
ts3utils.py
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
import re
escape_strings = [
(chr(92) , r'\\'), # \
(chr(47) , r'\/'), # /
(chr(32) , r'\s'), # Space
(chr(124), r'\p'), # |
(chr(7) , r'\a'), # Bell
(chr(8) , r'\b'), # Backspace
(chr(12) , r'\f'), # Formfeed
(chr(10) , r'\n'), # Newline
(chr(13) , r'\r'), # Carrage Return
(chr(9) , r'\t'), # Horizontal Tab
(chr(11) , r'\v'), # Vertical tab
]
TSRegex = re.compile(r'(\w+)=(.*?)(\s|$|\|)')
def escape(data):
'''
Escape to TS3Query-Format
'''
data = str(data)
for normal, ts3 in escape_strings:
data = data.replace(normal, ts3)
return data
def unescape(data):
'''
Escape to Human-Readable-Format
'''
for normal, ts3 in escape_strings:
data = data.replace(ts3, normal)
try:
data = int(data)
return data
except ValueError:
return data
def parseData(data):
'''
Parse telnet-data to key|value-dict
'''
parts = data.split('|')
parsed = []
for part in parts:
d = {}
regexed = TSRegex.findall(part)
for key in regexed:
d[key[0]] = unescape(key[1])
parsed.append(d)
return parsed