-
Notifications
You must be signed in to change notification settings - Fork 0
/
console_app.py
79 lines (62 loc) · 2.03 KB
/
console_app.py
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
"""python console_app.py email_body_filename [sender_filename]
"""
import sys
from os import path
from classifier import Classifier, ClassifierException
from const import CLASSIFIER_PATH
def delete_signature(email_filename, sender_filename=None):
"""Возвращает текст письма и подпись, если подпись не найдена,
то второе значение None
"""
with open(email_filename, 'r') as f_email:
email_lines = f_email.read().split('\n')
if sender_filename:
with open(sender_filename, 'r') as f_sender:
sender = f_sender.readline()
else:
sender = ''
try:
c = Classifier(CLASSIFIER_PATH)
except ClassifierException as e:
raise e
signature_lines = []
other_lines = []
email_lines = [l for l in email_lines if l.strip()]
for line in email_lines:
if c.check_line_for_signature(line, sender):
signature_lines.append(line)
else:
other_lines.append(line)
return other_lines, signature_lines
def main():
try:
email_filename = sys.argv[1]
except IndexError:
print("Set email filename")
return
try:
sender_filename = sys.argv[2]
except IndexError:
sender_filename = None
if not path.exists(email_filename):
print("File '{}' not exist".format(email_filename))
return
if sender_filename and not path.exists(sender_filename):
print("File '{}' not exist".format(sender_filename))
sender_filename = None
try:
text, signature = delete_signature(email_filename, sender_filename)
except ClassifierException as e:
print("Exception:", str(e))
return
print("Text lines: ")
for line in text:
print(line)
if len(signature) > 0:
print("\nSiganture Lines")
for line in signature:
print(line)
else:
print("\nSignature not found in email")
if __name__ == '__main__':
main()