Skip to content

Commit ae5e003

Browse files
committed
more tdd files
1 parent 986c058 commit ae5e003

16 files changed

Lines changed: 199 additions & 0 deletions
File renamed without changes.
File renamed without changes.
File renamed without changes.

tdd/transformations/MailerProg.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import os
2+
import re
3+
4+
"""
5+
def find_msg_files(d):
6+
msg_files = []
7+
for f in os.listdir(d):
8+
if f.endswith('.msg'):
9+
msg_files.append(f)
10+
return msg_files
11+
"""
12+
13+
def get_all_msgs():
14+
msgs = []
15+
msg_file_names = find_msg_files('c:/temp')
16+
for msg_file_name in msg_file_names:
17+
msgs.append(parse_msg_file(msg_file_name))
18+
return msgs
19+
20+
def find_msg_files(d):
21+
return [f for f in os.listdir(d) if f.endswith('.msg')]
22+
23+
def parse_msg_file(fname):
24+
fp = open(fname, 'r')
25+
reading_body = False
26+
ffrom = ""
27+
to = ""
28+
cc = ""
29+
tstamp = ""
30+
mime = ""
31+
body = ""
32+
for line in fp.readlines():
33+
if reading_body:
34+
body += line
35+
else:
36+
if line.startswith('to: '):
37+
to = line[len('to: '):]
38+
to = __remove_newline_chars(to)
39+
elif line.startswith('from: '):
40+
ffrom = line[len('from: '):]
41+
ffrom = __remove_newline_chars(ffrom)
42+
elif line.startswith('cc: '):
43+
cc = line[len('cc: '):]
44+
cc = __remove_newline_chars(cc)
45+
elif line.startswith('tstamp: '):
46+
tstamp = line[len('tstamp: '):]
47+
tstamp = __remove_newline_chars(tstamp)
48+
elif line.startswith('mime: '):
49+
mime = line[len('mime: '):]
50+
mime = __remove_newline_chars(mime)
51+
elif line.startswith('body:'):
52+
reading_body = True
53+
54+
return Msg(to=to, ffrom=ffrom, cc=cc, tstamp=tstamp, mime=mime, body=body)
55+
56+
def __remove_newline_chars(s):
57+
s = s.replace('\n','')
58+
s = s.replace('\r', '')
59+
return s
60+
61+
class Msg(object):
62+
def __init__(self, ffrom="", to="", cc="", tstamp="", mime="", body=""):
63+
self.ffrom = ffrom
64+
self.to = []
65+
self.cc = []
66+
self.tstamp = tstamp
67+
self.mime = mime
68+
self.body = body
69+
self.to = re.split(',\s*', to)
70+
if '' in self.to:
71+
self.to.remove('')
72+
self.cc = re.split(',\s*', cc)
73+
if '' in self.cc:
74+
self.cc.remove('')
75+
76+
def __str__(self):
77+
return """
78+
from: %s
79+
to: %s
80+
cc: %s
81+
mime: %s
82+
tstamp: %s
83+
body: %s""" % (self.ffrom, self.to, self.cc, self.mime, self.tstamp, self.body)
84+
85+
86+
87+
88+
if "__main__" == __name__:
89+
print find_msg_files('c:/temp')

tdd/transformations/Msg2Xml.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from xml.etree.ElementTree import Element, SubElement, Comment, tostring
2+
3+
top = Element('top')
4+
5+
comment = Comment('Generated for PyMOTW')
6+
top.append(comment)
7+
8+
child = SubElement(top, 'child')
9+
child.text = 'This child contains text.'
10+
11+
child_with_tail = SubElement(top, 'child_with_tail')
12+
child_with_tail.text = 'This child has regular text.'
13+
child_with_tail.tail = 'And "tail" text.'
14+
15+
child_with_entity_ref = SubElement(top, 'child_with_entity_ref')
16+
child_with_entity_ref.text = 'This & that'
17+
18+
print tostring(top)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import unittest
2+
import MailerProg
3+
import os
4+
5+
class TestMailerProg(unittest.TestCase):
6+
7+
def setUp(self):
8+
super(TestMailerProg, self).setUp()
9+
10+
def tearDown(self):
11+
super(TestMailerProg, self).tearDown()
12+
13+
def test_find_msg_files(self):
14+
fqn = os.path.abspath(__file__)
15+
msg_files = MailerProg.find_msg_files(fqn[0:fqn.rfind('/')])
16+
self.assertEqual(5, len(msg_files))
17+
self.assertTrue('email1.msg' in msg_files)
18+
self.assertTrue('email2.msg' in msg_files)
19+
self.assertTrue('email3.msg' in msg_files)
20+
self.assertTrue('email4.msg' in msg_files)
21+
self.assertTrue('email5.msg' in msg_files)
22+
23+
def test_parse_msg_file(self):
24+
fqn = os.path.abspath(__file__)
25+
curr_dir = fqn[0:fqn.rfind('/')]
26+
msg = MailerProg.parse_msg_file(os.path.join(curr_dir,'email1.msg'))
27+
self.assertEqual(msg.to[0], 'user1@domaina.com')
28+
29+
30+
if __name__ == "__main__":
31+
unittest.main()

0 commit comments

Comments
 (0)