This repository has been archived by the owner on Jan 31, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrite_content.py
140 lines (107 loc) · 3.76 KB
/
write_content.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
import json
import re
import sys
import time
from collections import namedtuple
from pathlib import Path
from jinja2 import Template
import markdown_table
PkgEntryPt = namedtuple("PkgEntryPt", ["pkg", "ep"])
DATE = time.strftime("%e %b %Y".lstrip())
EXT_EPS_JSON_PATH = Path("data", "eps_ext.json")
REP_EPS_JSON_PATH = Path("data", "eps_rep.json")
MD_PYPI_LINK = "[{pkg}](https://pypi.org/project/{pkg})"
CORE_TUPLES = [
PkgEntryPt("pyflakes", "F"),
PkgEntryPt("pycodestyle", "E"),
PkgEntryPt("pycodestyle", "W"),
]
P_ERRCODE = re.compile(r"^(?P<alpha>[A-Z]{1,3})(?P<num>\d{0,3})$", re.I)
REP_TEMPLATE = Template(Path("templates", "report.md_t").read_text())
BAD_TEMPLATE = Template(Path("templates", "bad_errorcodes.md_t").read_text())
PKG_SORT_TEMPLATE = Template(Path("templates", "ok_pkgsort.md_t").read_text())
EC_SORT_TEMPLATE = Template(Path("templates", "ok_ecsort.md_t").read_text())
README_TEMPLATE = Template(Path("templates", "README.md_t").read_text())
BUILD_DIR = "mdbuild"
REP_PATH = Path(BUILD_DIR, "report.md")
BAD_CODE_PATH = Path(BUILD_DIR, "bad_errorcodes.md")
PKG_SORT_PATH = Path(BUILD_DIR, "pkg_sort.md")
EC_SORT_PATH = Path(BUILD_DIR, "ec_sort.md")
README_PATH = Path("README.md")
def md_pypi_link(pkg):
return MD_PYPI_LINK.format(pkg=pkg)
def load_data():
with EXT_EPS_JSON_PATH.open() as f:
data_ext = json.load(f)
with REP_EPS_JSON_PATH.open() as f:
data_rep = json.load(f)
return data_ext, data_rep
def construct_tuples(data, *, add_core=True):
result = []
for pkg in data:
for ep in data[pkg]["eps"]:
result.append(PkgEntryPt(pkg, ep))
if add_core:
result.extend(CORE_TUPLES)
return result
def write_report_md(tuples_rep):
table_pkg = markdown_table.render(
("Package", "Entry Point"), ((md_pypi_link(p), e) for p, e in tuples_rep)
)
table_ep = markdown_table.render(
("Entry Point", "Package"),
((e, md_pypi_link(p)) for p, e in sorted(tuples_rep, key=(lambda t: t.ep))),
)
REP_PATH.write_text(
REP_TEMPLATE.render(table_pkg=table_pkg, table_ep=table_ep, date=DATE)
)
def write_ext_md(tuples_ext):
# Split out ones with improperly formatted entry_point names
ok_tups = [t for t in tuples_ext if P_ERRCODE.match(t.ep)]
bad_tups = [t for t in tuples_ext if not P_ERRCODE.match(t.ep)]
PKG_SORT_PATH.write_text(
PKG_SORT_TEMPLATE.render(
table=markdown_table.render(
("Package", "Entry Point"),
(
(md_pypi_link(p), e)
for p, e in sorted(ok_tups, key=(lambda t: t.pkg))
),
),
date=DATE,
)
)
EC_SORT_PATH.write_text(
EC_SORT_TEMPLATE.render(
table=markdown_table.render(
("Entry Point", "Package"),
(
(e, md_pypi_link(p))
for p, e in sorted(ok_tups, key=(lambda t: t.ep))
),
),
date=DATE,
)
)
BAD_CODE_PATH.write_text(
BAD_TEMPLATE.render(
table=markdown_table.render(
("Package", "Entry Point"), ((md_pypi_link(p), e) for p, e in bad_tups)
),
date=DATE,
)
)
def write_readme():
README_PATH.write_text(README_TEMPLATE.render(date=DATE))
def main():
data_ext, data_rep = load_data()
tuples_ext = construct_tuples(data_ext)
tuples_rep = construct_tuples(data_rep, add_core=False)
# Remove base flake8 for extensions; leave for report
tuples_ext = [t for t in tuples_ext if t.pkg != "flake8"]
# Write .md files
write_report_md(tuples_rep)
write_ext_md(tuples_ext)
write_readme()
if __name__ == "__main__":
sys.exit(main())