-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontact-data-parser.py
124 lines (93 loc) · 2.92 KB
/
contact-data-parser.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
from sys import argv
import json
class ConactParseError(Exception):
pass
def contact_to_dict(info_list):
tags = ["frequency",
"mode",
"date",
"utc_time",
"callsign",
"serial_number",
"presidence",
"check",
"section",
"callsign",
"serial_number",
"presidence",
"check",
"section"]
if len(info_list) != len(tags):
print(len(info_list), len(tags))
raise Exception('wrong number of input args')
pairs = list(zip(tags, info_list))
metadata = {tag: val for tag, val in pairs[:4]}
operator = {tag: val for tag, val in pairs[4:9]}
contact = {tag: val for tag, val in pairs[9:]}
return {
**metadata,
"operator": operator,
"contact": contact
}
class ContactDataParser(object):
def __init__(self):
self.file_loaded = False
self.contacts = []
self.metadata = []
def fromFile(self, file_path):
with open(file_path) as f:
raw_data = f.read()
self.file_loaded = True
self._parse(raw_data)
return self.contacts, self.metadata
def _parse(self, raw_data):
for line in raw_data.split("\n"):
if "END-OF-LOG" in line:
break
self._parse_line(line)
print(f"{len(self.contacts)} total contacts")
print(f"{json.dumps(self.contacts[0], indent=2)}")
def _parse_line(self, line):
try:
self._parse_contact(line)
except ConactParseError:
self._parse_metadata(line)
def _parse_contact(self, line):
key, contact = line.split(':')
contact_data = [d for d in contact.split(' ') if d != '']
if len(contact_data) != 14:
raise ConactParseError()
self.contacts.append(
contact_to_dict(contact_data)
)
def _parse_metadata(self, meta):
name, value = meta.split(':')
self.metadata.append({
'name': name,
'value': value
})
def parse_contact_data(path):
return ContactDataParser().fromFile(path)
def write_to_file(file_path, meta, contacts):
json_log = json.dumps(meta, indent=2) + '\n' + \
json.dumps(contacts, indent=2)
with open(file_path, 'w') as f:
f.write(json_log)
if __name__ == "__main__":
if len(argv) == 1:
print(
"python contact-data-parser [input_file_path] [output_file_path]"
)
exit(1)
try:
input_file_path = argv[1]
except IndexError:
print("file name required as command line arg.")
else:
metadata, contact_data = parse_contact_data(input_file_path)
try:
output_file_path = argv[2]
except IndexError:
print("file name required to write to json")
else:
write_to_file(output_file_path, metadata, contact_data)