-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmdrreader.py
272 lines (229 loc) · 9.51 KB
/
smdrreader.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
import re
import os
import sys
import zipfile
import datetime
class SMDRReader(object):
def __init__(self, data_directory, start_date, end_date):
try:
os.listdir(data_directory)
self.data_directory = data_directory
except FileNotFoundError:
raise InvalidInputException('The specified path does not exist')
except PermissionError:
raise InvalidInputException('You do not have permission to access'
' this directory')
try:
self.start_date = datetime.datetime.strptime(start_date, '%Y-%m-%d')
except:
raise InvalidInputException('Invalid start date')
try:
self.end_date = datetime.datetime.strptime(end_date, '%Y-%m-%d')
except:
raise InvalidInputException('Invalid end date')
def get_node_dirs(self, data_dir):
node_dirs = []
for dir in os.listdir(data_dir):
if re.match('Node_\d\d+', dir, re.I) is not None:
node_dirs.append(os.path.join(data_dir, dir))
return node_dirs
def get_file_by_date(self, path, date):
basename = None
datestring = date.strftime('%Y%m%d')
filelist = os.listdir(path)
for file in filelist:
if re.match('s' + datestring + '\.(zip|txt)',
file, re.I) is not None:
basename = file
break
if basename is None:
return None
filename,fileextension = os.path.splitext(basename)
filefullpath = os.path.join(path, basename)
if fileextension.lower() == '.zip':
myzip = zipfile.ZipFile(filefullpath)
with myzip.open('{}.txt'.format(filename)) as smdr_file:
file_lines = smdr_file.readlines()
myzip.close()
elif fileextension.lower() == '.txt':
with open(filefullpath, 'rb') as smdr_file:
file_lines = smdr_file.readlines()
else:
return None
return file_lines
def file_reader(self):
self.current_date = self.start_date
while self.current_date <= self.end_date:
file = self.get_file_by_date(self.data_directory, self.current_date)
if file is not None:
yield file
else:
yield []
self.current_date += datetime.timedelta(days=1)
def date_reader(self):
self.current_date = self.start_date
node_dirs = self.get_node_dirs(self.data_directory)
file_dict = dict((os.path.basename(dir), None) for dir in node_dirs)
while self.current_date <= self.end_date:
for dir in node_dirs:
file = self.get_file_by_date(dir, self.current_date)
file_dict[os.path.basename(dir)] = file
yield file_dict
self.current_date += datetime.timedelta(days=1)
def change_directory(self, new_directory):
if os.path.isdir(new_directory):
self.data_directory = new_directory
return True
else:
return False
class InvalidInputException(Exception):
def __init__(self, value, severity=0):
self.value = value
self.severity = severity
def __str__(self):
return str(self.value)
def __repr__(self):
return self.__str__()
class SMDREvent(object):
def __init__(self, smdr_string):
if type(smdr_string) != str:
raise InvalidInputException('Input is not a string', severity=1)
if smdr_string[0] not in (' ', '-', '%', '+'):
raise InvalidInputException('Input string is not an SMDR event')
smdr_string = smdr_string.rstrip('\r\n')
self.smdr_string = smdr_string
event_length = len(smdr_string)
if event_length == 207:
self.validate_207(smdr_string)
self.init_207(smdr_string)
elif event_length == 204:
self.validate_204(smdr_string)
self.init_204(smdr_string)
elif event_length == 113:
self.validate_113(smdr_String)
self.init_113(smdr_string)
else:
raise InvalidInputException('Unknown SMDR event encountered', severity=1)
def __str__(self):
return self.smdr_string
def __repr__(self):
return self.__str__()
def raise_validation_exception(self, length, smdr_string):
raise InvalidInputException('SMDR event with length '
'{} does not match expected format: {}'
.format(length, smdr_string), severity=1)
def init_non_event(self):
self.length_flag = ''
self.date = ''
self.time = ''
self.duration = ''
self.calling_party = ''
self.time_to_answer = ''
self.dialed_digits = ''
self.completion_flag = ''
self.speed_call_flag = ''
self.called_party = ''
self.trans_conf_flag = ''
self.third_party = ''
self.system_id = ''
self.ani = ''
self.dnis = ''
self.call_id = ''
self.sequence_id = ''
self.associated_id = ''
def validate_207(self, smdr_string):
try:
datetime.datetime.strptime(smdr_string[7:15], '%H:%M:%S')
except ValueError:
self.raise_validation_exception(207, smdr_string)
for char in (smdr_string[6],
smdr_string[15],
smdr_string[27],
smdr_string[40],
smdr_string[85],
smdr_string[110]):
if char != ' ':
self.raise_validation_exception(207, smdr_string)
def init_207(self, smdr_string):
self.length_flag = smdr_string[0]
self.date = smdr_string[1:6]
self.time = smdr_string[7:15]
self.duration = smdr_string[17:27]
self.calling_party = smdr_string[28:35].strip()
self.time_to_answer = smdr_string[36:40].strip()
self.dialed_digits = smdr_string[41:67].strip()
self.completion_flag = smdr_string[67].strip()
self.speed_call_flag = smdr_string[68].strip()
self.called_party = smdr_string[69:76].strip()
self.trans_conf_flag = smdr_string[84].strip()
self.third_party = smdr_string[86:92].strip()
self.system_id = smdr_string[107:110]
self.ani = smdr_string[113:123].strip()
self.dnis = smdr_string[134:144].strip()
self.call_id = smdr_string[153:161]
self.sequence_id = smdr_string[162]
self.associated_id = smdr_string[164:172].strip()
def validate_204(self, smdr_string):
try:
datetime.datetime.strptime(smdr_string[7:12], '%H:%M')
except ValueError:
self.raise_validation_exception(204, smdr_string)
for char in (smdr_string[6],
smdr_string[12],
smdr_string[24],
smdr_string[37],
smdr_string[82],
smdr_string[107]):
if char != ' ':
self.raise_validation_exception(204, smdr_string)
def init_204(self, smdr_string):
self.length_flag = smdr_string[0]
self.date = smdr_string[1:6]
self.time = smdr_string[7:12] + ':00'
self.duration = smdr_string[14:24]
self.calling_party = smdr_string[25:32].strip()
self.time_to_answer = smdr_string[33:37].strip()
self.dialed_digits = smdr_string[38:64].strip()
self.completion_flag = smdr_string[64].strip()
self.speed_call_flag = smdr_string[65].strip()
self.called_party = smdr_string[66:73].strip()
self.trans_conf_flag = smdr_string[81].strip()
self.third_party = smdr_string[83:90].strip()
self.system_id = smdr_string[104:107]
self.ani = smdr_string[110:120].strip()
self.dnis = smdr_string[131:141].strip()
self.call_id = smdr_string[150:158]
self.sequence_id = smdr_string[159]
self.associated_id = smdr_string[161:169].strip()
def validate_113(self, smdr_string):
try:
datetime.datetime.strptime(smdr_string[7:13] + 'M', '%I:%M%p')
except ValueError:
self.raise_validation_exception(113, smdr_string)
for char in (smdr_string[6],
smdr_string[13],
smdr_string[32],
smdr_string[66],
smdr_string[101]):
if char != ' ':
self.raise_validation_exception(113, smdr_string)
def init_113(self, smdr_string):
self.length_flag = smdr_string[0]
self.date = smdr_string[1:6]
time = datetime.datetime.strptime(smdr_string[7:13] + 'M', '%I:%M%p')
self.time = time.strftime('%H:%M:%S')
self.duration = smdr_string[14:22]
self.calling_party = smdr_string[23:28].strip()
self.time_to_answer = smdr_string[29:32].strip()
self.dialed_digits = smdr_string[33:59].strip()
self.completion_flag = smdr_string[59].strip()
self.speed_call_flag = smdr_string[60].strip()
self.called_party = smdr_string[61:65].strip()
self.trans_conf_flag = smdr_string[65].strip()
self.third_party = smdr_string[67:71].strip()
self.system_id = ''
self.ani = smdr_string[91:101].strip()
self.dnis = smdr_string[102:112].strip()
self.call_id = ''
self.sequence_id = ''
self.associated_id = ''