Created
November 16, 2012 17:32
-
-
Save TkTech/4089245 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import sys | |
from datetime import datetime | |
from collections import namedtuple | |
Message = namedtuple('Message', [ | |
'title', | |
'description', | |
'location', | |
'added', | |
'lines' | |
]) | |
def parse_messages(fin): | |
state = 0 | |
title = desc = loc = added = None | |
lines = [] | |
for line in fin: | |
if state == 0: | |
title = line | |
state = 10 | |
elif state == 10 and line.startswith('-'): | |
desc, loc, added = [p.strip() for p in line.split('|')] | |
added = datetime.strptime( | |
added, 'Added on %A, %B %d, %Y, %I:%M %p') | |
state = 20 | |
elif state == 20: | |
if not line.strip(): | |
continue | |
elif line.startswith('=========='): | |
state = 0 | |
yield Message(title, desc, loc, added, lines) | |
del lines[:] | |
else: | |
lines.append(line.strip()) | |
def main(argv): | |
with open(argv[1], 'rU') as fin: | |
for message in parse_messages(fin): | |
print(message) | |
if __name__ == '__main__': | |
sys.exit(main(sys.argv)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment