Skip to content

Commit

Permalink
Writer classes written and fixed reader
Browse files Browse the repository at this point in the history
Fixed Reader for empty messages
Base writer class and XML Writer class written
  • Loading branch information
cnu committed May 25, 2008
1 parent f05e611 commit 317b2bf
Showing 1 changed file with 77 additions and 4 deletions.
81 changes: 77 additions & 4 deletions pyvmg.py
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('<', '&lt;')
xmldata = xmldata.replace('>', '&gt;')
xmldata = xmldata.replace('"', '&quot;')
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()

0 comments on commit 317b2bf

Please sign in to comment.