This repository was archived by the owner on Jul 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.py
More file actions
228 lines (187 loc) · 7.16 KB
/
common.py
File metadata and controls
228 lines (187 loc) · 7.16 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#!/usr/bin/python
# coding: iso-8859-1
# Common.py
# Common functions module for IMAP and POP3 parsing implementation
#### Written by:
# Erika Burdon
# Software Engineering Student
# University of Victoria British Columbia, CANADA
# https://github.com/eburdon
# eburdonGIT@gmail.com
# All comments and critques on this script are welcome!
#
# website: web.uvic.ca/~eburdon
# LinkedIn: /in/eburdon
# Riipen: /users/1017
########
# Functions that are used similarly between IMAP and POP3
# ------------------------- Imports ------------------------------
import mimetypes
import re # Reg Expressions
import string # Parsing message strings
import sys # sys.exit
import base64 # For decoding base-64 subject lines
import codecs
import myGlobals # Defines host, usrnm, usrpss, etc...
import wx
sys.stdout = codecs.getwriter('utf-8')(sys.stdout)
from _codecs import decode
from email.header import decode_header
reload(sys)
sys.setdefaultencoding('utf8')
# ------------------ Outsourced Functions ------------------------
# Ask user yes/no question via raw_input; return decisive action.
# This function is overkill for this script, but I wanted to it
# be complete so that I have it for future reference.
# Function modified from:
# Recipe 577058: http://code.activestate.com/recipes/577058/
## Note: 'default' is presumed answer if user hits 'enter'
## (e.g., by accident)
def confirm_yes_no(question, default="no"):
valid = {"Y":True, "y":True, "Yes":True, "yes":True,
"N":False, "n":False, "No":False, "no":False}
if default == None:
prompt = "[y/n]"
elif default == "yes" or default == "Yes":
prompt = "[Y/n]"
elif default == "no" or default == "No":
prompt = "[y/N]"
while True:
print question + prompt
choice = raw_input()
if default is not None and choice == '':
return valid[default]
elif choice in valid:
return valid[choice]
else:
print "Please enter a valid response: Type 'y' or ",
print "'n' and press ENTER"
# Note that if you want to get text content (body) and the email
# contains multiple payloads (plaintext/html), you must parse
# each message separately. Use something like the following:
# (taken from a stackoverflow post)
def get_payload(email_message_instance):
maintype = email_message_instance.get_content_maintype()
if maintype == 'multipart':
for part in email_message_instance.get_payload():
if part.get_content_maintype() == 'text':
return part.get_payload(decode=True).split()
else:
print part.get_content_maintype()
# Do nothing... you're NOTHING TO ME
elif maintype == 'text':
return email_message_instance.get_payload()
## ------------------ Parsing Functions -------------------------
# Various functions parsing the email's full payload, fullMsg
def get_DE_word(vFlag, newWord, wod, meaning, fullMsg):
# Find the index POSITION of the word 'Day' (parse fullMsg)
for item in [item for item, x in enumerate(fullMsg) if x == 'Day']:
wodpos = item+1
while(1):
wodd = fullMsg[wodpos]
if ':' in wodd:
wodd = wodd.strip(':')
wod.append(wodd)
newWord.append(wodd)
break;
else:
# Append found match
wod.append(wodd)
newWord.append(wodd)
# Increase position; Look at next word (loop)
wodpos += 1
# Get english word (immediately following)
derp = get_EN_meaning(vFlag, meaning, wodpos, fullMsg)
if derp == False:
break;
def get_EN_meaning(vFlag, meaning, wodpos, fullMsg):
# Check if word is verb; Easier to understand later if 'to'
# is in front
if vFlag:
meaning.append("to " + fullMsg[wodpos+2])
return True
else:
while(1):
# Ensure you reach the end of the english part
if '-' is fullMsg[wodpos+2]:
meaning.append(fullMsg[wodpos+1])
break;
else:
# If not, append and carry on
meaning.append(fullMsg[wodpos+1])
wodpos+=1
return False
def get_part_speech(vFlag, prtspch, fullMsg):
# Get word's part of speech (& set flag)
for item in [item for item, x in enumerate(fullMsg) if x == 'speech:']:
prtspchh = fullMsg[item+1]
if prtspchh == 'verb':
vFlag = True
prtspch.append(prtspchh)
def get_DE_example(fullMsg, exStart, exEnd, exSentence):
for item in [item for item, x in enumerate(fullMsg) if x == 'sentence:']: # 'Example sentence:'
exStart = item+1
for item in [item for item, x in enumerate(fullMsg) if x == 'Sentence']: # 'Sentence meaning:'
exEnd = item
for word in range(exStart, exEnd):
exSentence.append(fullMsg[word])
return exEnd
def get_EN_example(fullMsg, exEnd, exTrSentence):
for item in [item for item, x in enumerate(fullMsg) if x == 'Listen']: # 'Sentence meaning:'
for word in range(exEnd+2, item):
exTrSentence.append(fullMsg[word])
## ------------------- Add-to-file Functions ---------------------
def add_element(fp, element):
for i in element:
fp.write(i)
fp.write(" ")
fp.write("\n")
# ---------------------- Closing Actions -------------------------
def confirm_new(newWord):
print "This script successfully added the following (new) ",
print "words into your vocabulary:\n"
for word in newWord:
print word
print ""
def exit_program(server, exit_message):
print "Terminating script..."
server.close()
server.logout()
if exit_message == 1:
# Some error! (Tracing)
sys.exit(1)
else:
# Clean Exit (No problems)
sys.exit(0)
# ----------------- OTHER----------------------- ----------
def newPopMessages():
wx.MessageBox('You have new messages from the last 30 days!', 'Alert!', wx.OK)
def noNewMessages():
wx.MessageBox('You have no new messages.', 'Alert!', wx.OK)
# def debug_print_all_subjects(messages):
# ## DEBUG; What are ALL my messages (subjects)?
# print "\n\nDEBUG: Printing all message subjects"
# n = 1
# for message in messages:
# print n, message['subject']
# n += 1
# print "\n\n"
# Puts all messages into array re: reference and iteration;
def get_vocab_messages(messages, matches, delList):
i = 0
for message in messages:
re_DEword = re.compile(r'German Word of the Day(.)*')
match = re_DEword.search(message['subject'])
if match:
matches.append(message)
delList.append(i)
i += 1
# POP3 CONNECTION ONLY?
def get_status_new_vocab(pop_conn, matches):
if len(matches) > 0:
# print "\nYou have new vocabulary words!"
newPopMessages()
else:
# print "\nNo new words. Terminating script."
noNewMessages()
pop_conn.quit()