-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoutputparser.py
More file actions
141 lines (118 loc) · 4.34 KB
/
outputparser.py
File metadata and controls
141 lines (118 loc) · 4.34 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import re
from collections import defaultdict
import sys
import os
import multiprocessing
FLUSH_COMMAND = "\x1bF"
class OutputParserServer(object):
def __init__(self, escape_code_map):
self.outputParser = OutputParser()
self.outputParser.loadEscapeMap(escape_code_map)
(self.charPipe, self.commandPipe) = multiprocessing.Pipe(True)
(self.charPipe_r, self.charPipe_w) = os.pipe()
def getPipes(self):
"Returns a tuple of outPipe, inPipe. Caller writes to outPipe, reads from inPipe"
return self.charPipe_w, self.charPipe #commandPipe
def start(self):
self.process = multiprocessing.Process(target=self._run, name="OutputParserServer",
kwargs={"charPipe":self.charPipe_r,
"commandPipe":self.commandPipe})
self.process.start()
print >>sys.stderr, "OutputParserServer: %d", self.process.pid
def stop(self):
self.charPipe_r.close()
self.commandPipe.close()
self.process.terminate()
self.process.join()
def quit(self):
f = open("/tmp/undecoded.txt", "w")
f.write("TEST")
f.write(str(self.outputParser.undecodedMap))
f.close()
def _run(self, charPipe, commandPipe):
try:
while True:
char = os.read(charPipe, 1)
if char == "\d":
return
ret = self.outputParser.parseChar(char)
if ret is not None:
commandPipe.send(ret[0])
commandPipe.send(ret[1])
except EOFError:
print "EOF"
return
finally:
commandPipe.send(self.outputParser.undecodedMap)
commandPipe.send("QUIT")
class OutputParser(object):
MAX_SEQUENCE = 15
def __init__(self):
self.regexList = []
self.buf = []
self.seqLen = 0
self.maxSeqLen = 0
self.maxBuf = ""
self.undecodedMap = defaultdict(int)
def addEscapeCode(self, key, meaningTuple):
regex = self.escapeCodeToRegex(key)
regex = r"^%s(.*)$" % regex
pattern = re.compile(regex)
self.regexList.append((pattern, meaningTuple))
def parseChar(self, char):
if char is None:
b = self.buf
self.buf = []
self.seqLen = 0
return self.match("".join(b))
self.seqLen += 1
if self.buf and (char == "\x1b" or self.seqLen > OutputParser.MAX_SEQUENCE):
self.seqLen = 0
b = self.buf
self.buf = [char]
if b[0] != "\x1b":
return "".join(b), ""
match = self.match("".join(b))
#self.logStats(match)
return match
self.buf.append(char)
# def logStats(self, match):
# self.seqLen -= len(match[1]) #subtract non-sequence chars
# self.maxSeqLen = max(self.seqLen, self.maxSeqLen)
# if self.maxSeqLen == self.seqLen:
# self.maxBuf = b
# self.seqLen = 0
# if type(match) == str and match[0] == "\x1b":
# self.logUndecoded(str)
# return match
def match(self, buf):
if buf in (FLUSH_COMMAND, "\x1b(B"): #dump switch-charset cmds
return None
for (pattern, actionTuple) in self.regexList:
matcher = pattern.match(buf)
if matcher is not None:
return (actionTuple + tuple(matcher.groups()[:-1]), matcher.groups()[-1])
return buf, ""
def logUndecoded(self, buf):
self.undecodedMap[buf] += 1
def escapeCodeToRegex(self, code):
STAR_SECTION = r"((?:\d*;?)*)"
codeParts = code.split(" ")
patternBuf = []
for char in codeParts:
if not char:
continue
if char == "esc":
patternBuf.append("\\x1b")
elif char == "***":
patternBuf.append(STAR_SECTION)
elif char == "**":
patternBuf.append(r"(\d*)")
elif char == "*X":
patternBuf.append("([^\x1b]{4})")
else:
patternBuf.append(re.escape(char))
return "".join(patternBuf);
def loadEscapeMap(self, escapeMap):
for k, v in escapeMap.iteritems():
self.addEscapeCode(k, v)