-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate_md.py
97 lines (64 loc) · 2.71 KB
/
generate_md.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
from pathlib import Path
import pandas as pd
import json
from rich import print
def print_this_test(this_test, input_, output_, transform_, f):
print(f'\n\n### {this_test.name.replace("_", " ")}', file=f)
print_table("\n#### input\n", f, input_)
print(f"\n#### transformation \n", file=f)
print("```json", file=f)
with open(transform_, "r") as t:
print(json.dumps(json.load(t), indent=4), file=f)
print("```", file=f)
print_table("\n#### output \n", f, output_)
def print_table(header, f, table_file):
print(f"{header}", file=f)
result = pd.read_csv(table_file, sep="\t")
print(result.to_markdown(index=False), file=f)
return result
def to_markdown(dir):
transformation_list = pd.read_csv(
Path().joinpath("transformation_list.tsv"), sep="\t"
).sort_values("name")
transformations = transformation_list[transformation_list["type"] == dir].drop(
columns=["type"]
)
input_dir = Path("spec").joinpath(dir)
print(f"\n\n[blue]Listing\n{input_dir}[/blue]")
output_file = Path("spec").joinpath(f"{dir}.md")
with open(output_file, "w+") as f:
print(f"# {dir.upper()} transformations\n", file=f)
print(f"""<!--
This file is automatically generated.
Please do not edit it directly.
Last update: {pd.Timestamp.now()}
-->
""", file=f)
print(transformations.to_markdown(index=False), file=f)
# table of content
print("\n", file=f)
for this_transformation in transformations["name"]:
print(f" - [{this_transformation}](#{this_transformation.lower()})", file=f)
for this_transformation in transformations["name"]:
print(f"\n\n<h2 id='{this_transformation}'>{this_transformation}</h2>", file=f)
print(f" {this_transformation}")
for this_test in input_dir.glob(f"*{this_transformation}*"):
print(f" {this_test}")
input_ = this_test.joinpath("input.tsv")
if not input_.exists():
print(f"[red] input.tsv missing for {this_test}[/red]")
continue
output_ = this_test.joinpath("output.tsv")
if not output_.exists():
print(f"[red] output.tsv missing for {this_test}[/red]")
continue
transform_ = this_test.joinpath("transformation.json")
if not transform_.exists():
print(f"[red] tranform.json missing for {this_test}[/red]")
continue
print_this_test(this_test, input_, output_, transform_, f)
def main():
to_markdown("compute")
to_markdown("munge")
if __name__ == "__main__":
main()