Repository navigation
Expand file tree
/
Copy pathparser.py
More file actions
54 lines (42 loc) · 1.44 KB
/
Copy pathparser.py
File metadata and controls
54 lines (42 loc) · 1.44 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
from datetime import date as Date
def transform_header(email, date):
# <mailto:nico@email.com|nico@email.com> -> nico@email.com
if "mailto:" in email and "|" in email:
start = email.index(":") + 1
end = email.index("|")
email = email[start:end].strip()
# replace "today"
if date.strip().lower() == "today":
date = Date.today().strftime("%m%d%Y")
return email, date
def transform_entry(entry):
if entry["issues"].strip().lower() in ("no issues", "none", ""):
entry["issues"] = "None"
if entry["inventory"].strip().lower() in ("no materials", "none", ""):
entry["inventory"] = "None"
return entry
def parse_text(raw: str):
# split into lines
lines = [l.strip() for l in raw.splitlines() if l.strip()]
# defaults in first 4 lines
email, name, school, date = lines[:4]
# clean headers
email, date = transform_header(email, date)
# rest of form
data = lines[4:]
entries = []
for i in range(0, len(data), 5):
p = data[i : i + 5]
if len(p) == 5:
entries.append(
transform_entry(
{
"workshop": p[0],
"week": p[1],
"summary": p[2],
"issues": p[3],
"inventory": p[4],
}
)
)
return email, name, school, date, entries