-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathhUIPon.py
executable file
·86 lines (65 loc) · 2.73 KB
/
hUIPon.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
#!/usr/bin/env python3
# Copyright (c) 2018 The Unit-e developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
#
# This is an adation of hADRon.py
# (https://github.com/dtr-org/unit-e-docs/blob/master/adrs/hADRon.py)
import os
import re
"""A simple script that generates a TOC for all the UIPs"""
class HUIPon:
def __init__(self):
pass
def generate(self):
toc_filename = "README.md"
lines =[]
with open(toc_filename, "r") as toc_file:
found_table = False
for line in toc_file:
if line.startswith('|'):
if not found_table:
lines += self.generate_table()
found_table = True
else:
lines.append(line.rstrip())
with open(toc_filename, "w") as toc_file:
for l in lines:
toc_file.write(l + "\n")
def generate_table(self):
header = "| UIP | Title | Status | Created |"
sub_h = "|---|---|:---:|:---:|"
lines = [header, sub_h]
uips = self.list_uips()
for uip in uips:
num = "[" + uip[:-3] + "](https://github.com/dtr-org/uips/blob/master/" + uip + ")"
with open(uip, "r") as file:
title = ""
status = ""
created = ""
complete = False
for i in range(1, 10):
next_line = file.readline()
if not title and re.match("^# UIP-[0-9]*[: ].*$", next_line):
title = re.search("^# UIP-[0-9]*[: ](.*)$", next_line).group(1).strip()
if not status and re.match("^Status:.*$", next_line):
status = re.search("^Status:(.*)$", next_line).group(1).strip()
if not created and re.match("^Created:.*$", next_line):
created = re.search("^Created:(.*)$", next_line).group(1).strip()
complete = True
break
if not complete:
raise Exception("Cannot parse file: " + uip)
new_entry = "|" + num + "|" + title + "|" + status + "|" + created + "|"
lines.append(new_entry)
return lines
def list_uips(self):
uips = []
for filename in os.listdir("."):
if re.match("^UIP-[0-9]{4}\.md", filename) and filename != "UIP-0000.md":
uips.append(filename)
uips.sort(key=lambda a: int(re.search("^UIP-([0-9]{4})\.md", a).group(1)))
return uips
if __name__ == '__main__':
os.chdir(os.path.dirname(__file__))
HUIPon().generate()