-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathupdate_readme.py
86 lines (63 loc) · 2.57 KB
/
update_readme.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
# -*- coding: utf-8 -*-
import os
import re
import hashlib
class InvalidFileFormatException(Exception):
pass
def load_projects():
project_set = set() # a set of tuples: ( name, other_info )
# "x | y | z"
registry_pattern_1 = r""" ([^|]+) # team
\| # separator
([^|]+) # project description
\| # separator
([^|]+) # link to project repo
"""
# "|x | y | z|"
registry_pattern_2 = r""" \| # left-sep
([^|]+) # team
\| # mid-separator-1
([^|]+) # project description
\| # mid-separator-2
([^|]+) # link to project repo
\| # right-sep
"""
## pattern = re.compile( '%s | %s' % ( sig_pattern_1, sig_pattern_2 ), re.X )
pattern1 = re.compile( registry_pattern_1, re.VERBOSE )
pattern2 = re.compile( registry_pattern_2, re.VERBOSE )
dir = 'projects'
for basename in os.listdir(dir):
filename = os.path.join(dir, basename)
if not os.path.isfile(filename):
print('Skipping non-file "%s"' % filename)
continue
with open(filename) as inp:
for line in inp:
line = line.strip()
if not line:
continue
m = re.match(pattern1, line) or re.match(pattern2, line)
if not m :
raise InvalidFileFormatException(
'File "%s" does not follow the format:\n\t"%s"'
% (filename, line)
)
project_set.add((m.group(1).strip(), m.group(2).strip(), m.group(3).strip()))
project_list = sorted(project_set, key=lambda pair: hashlib.sha256(repr(pair).encode('utf-8')).hexdigest())
return project_list
def write_projects(projects, outp):
for project in projects:
outp.write('| {:<14} | {:<41} | {:<41} |\n'.format(project[0], project[1], project[2]))
def update_readme(projects):
with open('pre-readme.md') as inp, open('README.md', 'w') as outp:
for line in inp:
if line.strip() == '<!-- Projects -->':
write_projects(projects, outp)
else:
outp.write(line)
def main():
projects = load_projects()
update_readme(projects)
if __name__ == '__main__':
main()