-
Notifications
You must be signed in to change notification settings - Fork 12
/
phep_to_yaml.py
68 lines (64 loc) · 2.26 KB
/
phep_to_yaml.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
#!/usr/bin/env python3
import email
import email.policy
import glob
import os.path
import re
import warnings
import yaml
phep_src = glob.glob(os.path.join(
os.path.dirname(os.path.abspath(__file__)), 'pheps', 'phep-*.md'))
pheps = [] # fully parsed
for ph in phep_src:
if not re.match(r"^phep-\d+\.md$", os.path.basename(ph)):
continue
with open(ph, "rt") as f:
if f.readline().rstrip() != "```":
warnings.warn(f"{ph} preamble has no opening code fence, skipping.")
continue
headers = []
for l in f:
if l.rstrip() == "```":
break
headers.append(l)
else:
warnings.warn(f"{ph} preamble has no closing code fence, skipping.")
continue
body = f.readlines()
m = email.message_from_string("".join(headers),
policy=email.policy.EmailPolicy())
phep = {k: str(v) for k, v in m.items()}
required = (
"PHEP", "Title", "Author", "Discussions-To", "Revision", "Status",
"Type", "Content-Type", "Created", "Post-History",
)
missing = [k for k in required if k not in phep]
if missing:
warnings.warn(f"{ph} missing headers {', '.join(missing)}, skipping.")
continue
optional = ("Requires", "Replaces", "Replaced-By", "Resolution", )
bad = [k for k in phep if k not in required + optional]
if bad:
warnings.warn(
f"{ph} has unexpected headers {', '.join(bad)}, skipping.")
continue
for k in ('PHEP', 'Revision'):
phep[k] = int(phep[k])
phep["Filename"] = os.path.basename(ph)
idx = next((i for i, l in enumerate(body)
if l.startswith("# Copyright")), None)
if idx is not None:
doi_lines = [l for l in body[idx:] if " doi " in l]
for doi_line in doi_lines:
m = re.search(r"10\.\d{4}(\.\d+)*/[-._;()/:A-Z0-9]+",
doi_line, flags=re.IGNORECASE)
if m:
phep["DOI"] = m.group(0)
break
if "DOI" not in phep:
warnings.warn(
f"Found no DOI in {ph}, must fix output by hand.")
phep["DOI"] = "Unknown"
pheps.append(phep)
with open("pheps.yml", "wt") as f:
f.write(yaml.dump(pheps))