-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
51 lines (38 loc) · 1.32 KB
/
test.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
from mail_handler import handle_mail, update_statistics
from os import listdir
from os.path import join, isfile
from pathlib import Path
from generator import generate
from sqlite import get_sqlite_database
from config import setup_logging
import logging
import sqlite3
CURR_DIR = Path(__file__).parent.resolve()
DIR = CURR_DIR / "test_mails"
DB_FILE = get_sqlite_database()
def ingest_emails():
files = [str(join(DIR, f)) for f in listdir(DIR) if f.endswith(".eml") and isfile(join(DIR, f))]
for f in sorted(files):
with open(f, mode="r", encoding="utf-8") as e:
handled, subject = handle_mail(e.read())
if handled:
logging.debug(f'SUCCESS: correctly parse file: "{f}"')
else:
logging.error(f'FAILURE: failed to parse file: "{f}"')
logging.error(f'Subject: "{subject}"')
def correct_database_timestamps():
con = sqlite3.connect(str(DB_FILE))
cur = con.cursor()
test_migration = CURR_DIR / "migrations" / "99_fix_test_db.sql"
with open(test_migration) as f:
cur.executescript(f.read())
def final_step():
update_statistics()
generate.generate_coffee_report(DB_FILE)
def main():
setup_logging()
ingest_emails()
correct_database_timestamps()
final_step()
if __name__ == "__main__":
main()