-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataParser.py
155 lines (128 loc) · 4.03 KB
/
dataParser.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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
from HTMLParser import HTMLParser
from htmlentitydefs import name2codepoint
import re
import time
class sipMsg:
def __init__(self, direction = '', \
msgFrom = '', to = '', time = '', method = '', \
statuscode = '', sipCallId = '', isRequest = '', msg = ''):
self.direction = direction
self.msgFrom = msgFrom
self.to = to
self.time = time
self.method = method
self.statuscode = statuscode
self.sipCallId = sipCallId
self.isRequest = isRequest
self.msg = msg
def parseMsg(self):
print 'parseMsg'
def __enter__(self):
return self
def __exit__(self, type, value, traceback):
for file in self.files:
os.unlink(file)
del self
class dataParser(HTMLParser):
def init(self):
self.msgFlow = list()
self.tmpMsg = sipMsg()
def handle_starttag(self, tag, attrs):
# print "Start tag:", attrs
for attr in attrs:
if attr[0] == 'direction':
self.tmpMsg.direction = attr[1]
if attr[0] == 'from':
self.tmpMsg.msgFrom = attr[1]
if attr[0] == 'to':
self.tmpMsg.to = attr[1]
if attr[0] == 'time':
self.tmpMsg.time = attr[1]
if attr[0] == 'method':
self.tmpMsg.method = attr[1]
if attr[0] == 'statuscode':
self.tmpMsg.statuscode = attr[1]
if attr[0] == 'sipCallId':
self.tmpMsg.sipCallId = attr[1]
if attr[0] == 'isRequest':
self.tmpMsg.isRequest = attr[1]
def handle_endtag(self, tag):
self.msgFlow.append(self.tmpMsg)
self.tmpMsg = sipMsg()
# def handle_data(self, data):
# #self.tmpMsg.msg = data
# print "data :", data
def handle_comment(self, data):
if (data.find('CDATA') != -1):
self.tmpMsg.msg = data
def handle_entityref(self, name):
c = unichr(name2codepoint[name])
print "Named ent:", c
def handle_charref(self, name):
if name.startswith('x'):
c = unichr(int(name[1:], 16))
else:
c = unichr(int(name))
print "Num ent :", c
def handle_decl(self, data):
print "Decl :", data
# def get_starttag_text(self, data):
# print "message :", data
def getMsgFlow(self):
return self.msgFlow
def __enter__(self):
return self
def __exit__(self, type, value, traceback):
for file in self.files:
os.unlink(file)
del self
class dataFormat():
# def __init__(self, file):
# self.filePath = file
def __init__(self, f):
self.rawData = ''
self.file = f
def readData(self):
# try:
# f = open(self.filePath, 'r')
# except Exception, e:
# raise e
self.rawData = self.file.read()
self.file.close()
def formatData(self):
tmpData = self.rawData.replace('<![', '<!--[')
tmpData = tmpData.replace(']>', ']-->')
self.formatedData = tmpData
del tmpData
def getRawData(self):
return self.rawData
def getFormatedData(self):
return self.formatedData
def __enter__(self):
return self
def __exit__(self, type, value, traceback):
for file in self.files:
os.unlink(file)
del self
def printFlow(flow):
for m in flow:
print m.direction
print m.msgFrom
print m.to
# print time.strftime("%D %H:%M", time.localtime(int(m.time + '')))
print m.time
print m.method
print m.statuscode
print m.sipCallId
print m.isRequest
print m.msg
def testParser(file):
data = dataFormat(file)
data.readData()
data.formatData()
parser = dataParser()
parser.init()
parser.feed(data.getFormatedData())
printFlow(parser.getMsgFlow())
if __name__ == '__main__':
testParser('data/sipserver118.1.trace')