-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathparse.py
68 lines (53 loc) · 1.56 KB
/
parse.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
"""Parser functions for extracting fields from text."""
import os
import json
import datetime
def organiser_name(meta):
"""Get name from organiser text."""
v = meta.get('organiser') or ''
assert type(v) in (dict, str), f"{type(v)} found"
if type(v) == dict:
return v.get('name', '')
return v
def organiser_email(meta):
"""Get email from organiser text."""
v = meta.get('organiser') or ''
if type(v) == dict:
return v.get('email', '')
return ''
def location_json(meta):
"""Get location data from location text."""
v = meta.get('location')
if not v:
return ''
if type(v) == str:
v = {'name': v.title()}
return json.dumps(v)
def date_start(meta):
"""Get start date."""
v = meta.get('starts') or ''
assert type(v) == datetime.date or type(v) == str, (
f"{type(v)} found")
if type(v) == datetime.date:
return v.strftime('%Y-%m-%d')
return v
def date_end(meta):
"""Get end date."""
v = meta.get('ends') or ''
assert type(v) == datetime.date or type(v) == str, (
f"{type(v)} found")
if type(v) == datetime.date:
return v.strftime('%Y-%m-%d')
return v
def date_from_filepath(path):
"""Parse date from filename."""
fname = os.path.basename(path)
date = fname[:10]
# Assert dt format
datetime.datetime.strptime(date, '%Y-%m-%d')
return date
def csv_escape(text):
"""Escape functional characters in text for embedding in CSV."""
if text is None:
return ''
return '~' + text.strip('\n -') + '~'