-
-
Notifications
You must be signed in to change notification settings - Fork 619
Expand file tree
/
Copy pathgen_table.py
More file actions
53 lines (44 loc) · 2.15 KB
/
gen_table.py
File metadata and controls
53 lines (44 loc) · 2.15 KB
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
#/bin/env python3
import os
import argparse
def generate_table(directory):
table = []
for filename in os.listdir(directory):
if filename.startswith("nvidia-"):
with open(os.path.join(directory, filename), 'r') as file:
lines = file.readlines()
table.append(f"## {filename}\n")
table.append("| NVE0 | NVE4 | GK104 | Description |\n")
table.append("|------|------|-------|-------------|\n")
for line in lines:
table.append(f"| {' | '.join(line.strip().split('|'))} |\n")
table.append("\n")
return table
def write_table_to_file(table, output_file, start_marker, end_marker):
with open(output_file, 'r') as file:
existing_content = file.readlines()
try:
start_index = existing_content.index(start_marker) + 1
end_index = existing_content.index(end_marker)
new_content = existing_content[:start_index] + table + existing_content[end_index:]
except ValueError:
new_content = existing_content + [start_marker] + table + [end_marker]
with open(output_file, 'w') as file:
file.writelines(new_content)
def generate_table_of_contents(directory):
table_of_contents = []
table_of_contents.append(f"# Table of Contents\n")
for filename in os.listdir(directory):
if filename.startswith("nvidia-"):
table_of_contents.append(f"- [{filename}](#{filename})\n")
return table_of_contents
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Generate Nvidia driver tables and table of contents.")
parser.add_argument("-f", "--file", required=True, help="The output file to write the tables to.")
args = parser.parse_args()
directory = "."
output_file = args.file
nvidia_table = generate_table(directory)
write_table_to_file(nvidia_table, output_file, "<!-- START NVIDIA TABLE -->\n", "<!-- END NVIDIA TABLE -->\n")
table_of_contents = generate_table_of_contents(directory)
write_table_to_file(table_of_contents, output_file, "<!-- START TABLE OF CONTENTS -->\n", "<!-- END TABLE OF CONTENTS -->\n")