-
Notifications
You must be signed in to change notification settings - Fork 217
/
generate_index.py
58 lines (51 loc) · 1.99 KB
/
generate_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
import argparse
from operator import itemgetter
import os
from pathlib import Path
import sys
import rfcs
def update(fname, tmp_fname):
if not os.path.exists(fname):
os.rename(tmp_fname, fname)
print('Generated %s.' % fname)
return
with open(fname, encoding='utf-8', mode='rt') as f:
old = f.read()
with open(tmp_fname, encoding='utf-8', mode='rt') as f:
new = f.read()
if old == new:
print('No change to %s.' % fname)
return
os.remove(fname)
os.rename(tmp_fname, fname)
print('Updated %s.' % fname)
def main(fname = None):
if not fname:
fname = os.path.join(rfcs.root_folder, 'index.md')
# Load all metadata
all = [rfc for rfc in rfcs.walk()]
all.sort(key=lambda x: x.num)
tmp_fname = fname + '.tmp'
with open(tmp_fname, 'w', encoding='utf-8') as out:
out.write("# Aries RFCs by Status\n")
for status in rfcs.status_list:
out.write(f"\n## [{status}](README.md#{status.lower()})\n")
with_status = [rfc for rfc in all if rfc.status == status]
for rfc in with_status:
line = f"* [{rfc.num}: {rfc.title}]({rfc.relpath})"
tags = [f"[`{x}`](/tags.md#{x})" for x in rfc.tags]
line += f" ({rfc.since}"
if rfc.impl_count:
line += f", [{rfc.impl_count} impl"
if rfc.impl_count > 1:
line += 's'
line += '](' + rfc.relpath + '#implementations)'
line += ' — ' + ' '.join(tags) + ')'
out.write(line + '\n')
out.write("\n\n>(This file is machine-generated; see [code/generate_index.py](code/generate_index.py).)\n")
update(fname, tmp_fname)
if __name__ == '__main__':
ap = argparse.ArgumentParser('Genrate index')
ap.add_argument('altpath', metavar='PATH', nargs='?', default=None, help='override where index is generated')
args = ap.parse_args()
main(args.altpath)