-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Writer classes written and fixed reader
Fixed Reader for empty messages Base writer class and XML Writer class written
- Loading branch information
Showing
1 changed file
with
77 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,96 @@ | ||
import re | ||
import glob | ||
import datetime | ||
|
||
def escapexml(xmldata): | ||
xmldata = xmldata.replace('&', '&') | ||
xmldata = xmldata.replace('<', '<') | ||
xmldata = xmldata.replace('>', '>') | ||
xmldata = xmldata.replace('"', '"') | ||
return xmldata | ||
|
||
def datecmp(x,y): | ||
if x['date'] < y['date']: | ||
return -1 | ||
elif x['date'] == y['date']: | ||
return 0 | ||
else: | ||
return 1 | ||
|
||
class VMGReader(object): | ||
"""Reader for a .vmg file to get back the telephone number, date, body | ||
""" | ||
def __init__(self): | ||
"""Initialize with the required regexes | ||
""" | ||
self.telre = re.compile(r'TEL:(\+?\d+)') | ||
self.datere = re.compile(r'X-NOK-DT:([\dTZ]+)') | ||
self.bodyre = re.compile(r'Date:[\d.: ]+\n(.*)END:VBODY',re.DOTALL) | ||
|
||
def read(self, filename): | ||
"""Open a .vmg file and remove the NULL characters and store the text message | ||
""" | ||
self.filename = filename | ||
self.message = open(filename, 'r').read() | ||
self.message = self.message.replace('\0', '') | ||
|
||
def process(self): | ||
"""Parse the message and return back a dictionary with | ||
telephone number, date and body of message | ||
""" | ||
data = {} | ||
data['telno'] = self.telre.search(self.message).group(1) | ||
data['date'] = self.datere.search(self.message).group(1) | ||
data['date'] = datetime.datetime.strptime(data['date'], '%Y%m%dT%H%M%SZ') | ||
data['body'] = self.bodyre.search(self.message).group(1) | ||
telmatch = self.telre.search(self.message) | ||
if telmatch: | ||
data['telno'] = telmatch.group(1) | ||
else: | ||
data['telno'] = '' | ||
datematch = self.datere.search(self.message) | ||
if datematch: | ||
data['date'] = datematch.group(1) | ||
try: | ||
data['date'] = datetime.datetime.strptime(data['date'], '%Y%m%dT%H%M%SZ') | ||
except ValueError: | ||
# Use Epoch as date if no date was available | ||
data['date'] = datetime.datetime.strptime('1970-01-01 00:00:00', '%Y-%m-%d %H:%M:%S') | ||
bodymatch = self.bodyre.search(self.message) | ||
if bodymatch: | ||
data['body'] = escapexml(bodymatch.group(1)) | ||
else: | ||
data['body'] = '' | ||
return data | ||
|
||
class Writer(object): | ||
"""Base class for a writer object to convert all VMG files to a single file | ||
""" | ||
def __init__(self, filename): | ||
"""Create a file writer object with the filename specified | ||
""" | ||
self.filename = filename | ||
self.file = open(filename, 'w') | ||
|
||
def processdir(self, dirpath): | ||
"""Given a directory path, process all the .vmg files in it and store as a list | ||
""" | ||
files = glob.glob(dirpath + '/*.vmg') | ||
reader = VMGReader() | ||
self.messages = [] | ||
for f in files: | ||
print f | ||
reader.read(f) | ||
self.messages.append(reader.process()) | ||
self.messages.sort(datecmp) # Sort the messages according to date | ||
|
||
class XMLWriter(Writer): | ||
"""Writer object for XML file as output | ||
""" | ||
def write(self): | ||
"""Read every message in the list and write to a XML file | ||
""" | ||
self.file.write('<messages>') | ||
tmpl = "<message><tel>%s</tel><date>%s</date><body>%s</body></message>" | ||
for msg in self.messages: | ||
xmlstr = tmpl %(msg['telno'], msg['date'].strftime('%Y-%m-%d %H:%M:%S'), msg['body']) | ||
self.file.write(xmlstr) | ||
self.file.write('</messages>') | ||
self.file.close() | ||
|