-
Notifications
You must be signed in to change notification settings - Fork 5
/
update_index.py
executable file
·183 lines (157 loc) · 6.06 KB
/
update_index.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/usr/bin/env python3.8
from __future__ import annotations
from typing import * # NoQA
import dataclasses
import os
import pathlib
import re
Status = Literal[
"active",
"inactive",
"final",
"accepted",
"rejected",
"deferred",
"draft",
]
CURRENT_DIR: Final = pathlib.Path(__file__).parent
RFC_FILENAME = re.compile(r"^(?P<number>\d\d\d\d)-(?P<slug>[a-z0-9_-]+)\.rst$")
RFC_TITLE = re.compile(r"^RFC (?P<number>\d+)(: | - )(?P<title>.+)$")
HEADER_FIELD = re.compile(r"^ +(?P<name>[^:]+): (?P<value>.*)$")
HEADING = re.compile(r"^[=-~]+$")
AUTOGENERATED = "NOTE: This section is auto-generated with update_index.py"
ACCEPTED_STATUSES: Set[Status] = {"active", "inactive", "final", "accepted"}
STATUSES: Set[Status] = ACCEPTED_STATUSES | {
"rejected", "deferred", "draft", "withdrawn"}
@dataclasses.dataclass
class RFC:
number: int
title: str
status: Status
filename: str
@classmethod
def from_file(cls, path: pathlib.Path) -> RFC:
number = 0
title = "unknown"
status = "unknown"
filename = path.name
expected_length = 0
states = [
"before_preamble",
"in_empty_line",
"in_preamble",
"before_title",
"after_title",
]
with path.open() as file:
for line in file:
line = line.rstrip()
if states[0] == "before_preamble":
if line == "::":
states.pop(0)
elif states[0] == "in_empty_line":
if not line:
states.pop(0)
elif states[0] == "in_preamble":
if m := HEADER_FIELD.match(line):
if m.group("name") == "Status":
status = m.group("value").lower()
else:
states.pop(0)
elif states[0] == "before_title":
if m := RFC_TITLE.match(line):
number = int(m.group("number"))
title = m.group("title")
expected_length = len(line)
elif HEADING.match(line):
if expected_length == 0:
# pre-heading overline
continue
if len(line) == expected_length:
states.pop(0)
else:
# previous RFC title match, if any, was invalid
number = 0
title = "unknown"
elif states[0] == "after_title":
break
if number == 0 or title == "unknown":
raise LookupError(
f"Invalid RFC document {path}: {number=} {title=}"
)
if status in STATUSES:
status = cast(Status, status)
else:
raise LookupError(f"Invalid RFC document {path}: {status=}")
return RFC(
number=number, title=title, status=status, filename=filename
)
def list_rfcs(directory: pathlib.Path) -> Dict[Status, List[RFC]]:
result: Dict[Status, List[RFC]] = {}
for file in os.listdir(directory):
if not RFC_FILENAME.match(file):
continue
rfc = RFC.from_file(directory / file)
status = rfc.status
if status in ACCEPTED_STATUSES:
status = "accepted"
result.setdefault(status, []).append(rfc)
for rfcs in result.values():
rfcs.sort(key=lambda elem: elem.number)
return result
def patch_readme(readme: pathlib.Path, rfcs: Dict[Status, List[RFC]]) -> None:
lines: List[str] = []
states = ["before_rfc_list", "in_rfc_list", "after_rfc_list"]
with readme.open() as original:
for line in original:
if states[0] == "before_rfc_list":
lines.append(line)
if line.rstrip() == "## List of RFCs":
states.pop(0)
elif states[0] == "in_rfc_list":
if line.startswith("## "):
states.pop(0)
lines.append(f'[//]: # "{AUTOGENERATED}"\n\n')
if "draft" in rfcs:
lines.append("### Current Drafts\n")
for r in rfcs["draft"]:
lines.append(
f"* [RFC {r.number} - {r.title}]"
f"(./text/{r.filename})\n"
)
lines.append("\n")
if "accepted" in rfcs:
lines.append("### Accepted\n")
for r in rfcs["accepted"]:
lines.append(
f"* [RFC {r.number} - {r.title}]"
f"(./text/{r.filename}) ({r.status})\n"
)
lines.append("\n")
if "deferred" in rfcs:
lines.append("### Deferred\n")
for r in rfcs["deferred"]:
lines.append(
f"* [RFC {r.number} - {r.title}]"
f"(./text/{r.filename})\n"
)
lines.append("\n")
if "rejected" in rfcs:
lines.append("### Rejected\n")
for r in rfcs["rejected"]:
lines.append(
f"* [RFC {r.number} - {r.title}]"
f"(./text/{r.filename})\n"
)
lines.append("\n")
lines.append(line)
elif states[0] == "after_rfc_list":
lines.append(line)
with readme.open("w") as new:
for line in lines:
new.write(line)
def main() -> None:
rfcs = list_rfcs(CURRENT_DIR / "text")
patch_readme(CURRENT_DIR / "README.md", rfcs)
if __name__ == "__main__":
main()