-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_md_files.py
309 lines (280 loc) · 11.4 KB
/
generate_md_files.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import os
from airtable_table import (
get_table_data_list,
AllInstrumentTableCols as Cols,
)
from pyairtable.api.types import RecordDict, Fields
from templates import (
get_device_template,
get_category_template,
get_unit_device_template,
INS_BY_CAT_TEMPLATE,
INS_BY_LIBS_TEMPLATE,
INS_BY_VENDOR_TEMPLATE,
get_category_page_template,
get_py_code_tabs,
)
from utils import (
construct_slugs_by_cat,
ensure_dirs,
get_keywords,
add_space_after_angle,
validate_image_url,
)
from log import logger
from generate_snippets import generate_snippets, get_lib_desc
from load_data import get_libs_cats_map
from typing import Literal
def generate_md_files(base_path: str):
"""
Entry point of the script
Args:
base_path (str): path to instruments-database folder of docs
"""
# Generate all instruments pages by category, library and manufacturer
for cat in ["category", "library", "manufacturer"]:
generate_instruments_pages(by=cat, base_path=base_path) # type: ignore
# Get table from Airtable in list format
table_list = get_table_data_list()
logger.info(f"Loaded table in list format from Airtable: {len(table_list)} records")
for record in table_list:
process_record(record, base_path)
logger.info("Finished processing all records")
def process_record(record: RecordDict, base_path: str):
fields = record["fields"]
fields["id"] = record["id"]
categories = fields.get(Cols.category, [])
slug = (
fields[Cols.docs_wiki].replace("/instruments-wiki", "instruments-database")
if Cols.docs_wiki in fields and fields.get(Cols.docs_wiki, None)
else f"instruments-database/{categories[0]}/{fields[Cols.vendor]}/{fields.get(Cols.device_name,'').replace(' ', '-')}"
)
constructed_slugs = construct_slugs_by_cat(categories, slug)
title = f"Connecting to {fields[Cols.correct_device_name]} by {fields[Cols.vendor]} in Python"
vendor = fields[Cols.vendor]
image_url = validate_image_url(fields)
for file_path, category, slug_ in constructed_slugs:
ensure_dirs(base_path, file_path + ".mdx")
device_name = fields[Cols.device_name]
corrected = fields[Cols.correct_device_name]
device_name = corrected if corrected and corrected != "" else device_name
tabs = get_py_code_tabs(
device_name=device_name,
lib=fields[Cols.library],
category=category,
manufacturer=vendor,
)
keywords = (
"keywords: ["
+ get_keywords(
category=category,
lib=fields.get(Cols.library, ""),
vendor=fields[Cols.vendor],
)
+ "]"
)
template = get_device_template(
title=title,
sidebar_label=fields.get(Cols.correct_device_name, ""),
description=fields.get(Cols.description, ""),
category=category,
device_spec=fields.get(Cols.device_specification, ""),
meta_image=image_url,
slug=slug_,
vendor=fields.get(Cols.vendor, ""),
vendor_description=fields.get(Cols.vendor_desc, ""),
vendor_headquarter=fields.get(Cols.headquarters, ""),
vendor_logo_url=fields.get(Cols.vendor_logo_url, ""),
vendor_revennue=fields.get(Cols.revenue, ""),
vendor_website=fields.get(Cols.vendor_website, ""),
keywords=keywords,
second_tab_item=tabs,
)
template = add_space_after_angle(template)
with open(os.path.join(base_path, file_path.strip() + ".mdx"), "w") as f:
f.write(template.strip())
def write_category_overview(instrument_path: str, cat_name: str, cat_template: str):
category_path = os.path.join(instrument_path, cat_name)
if not os.path.exists(category_path):
os.mkdir(category_path)
overview_file_name = f"{cat_name.lower().replace(' ','-')}-overview.mdx"
overview_file_path = os.path.join(category_path, overview_file_name)
temp = get_category_page_template(
cat_name=cat_name,
slug=f"instruments-database/{cat_name.lower().replace(' ', '-')}/{overview_file_name.replace('.mdx','')}",
)
mdx_content = temp + cat_template
with open(overview_file_path, "w") as f:
f.write(mdx_content.strip())
templates = {
"category": INS_BY_CAT_TEMPLATE,
"library": INS_BY_LIBS_TEMPLATE,
"manufacturer": INS_BY_VENDOR_TEMPLATE,
}
def all_instruments_by_category(
cat: str, fields: list[Fields], template: str, ins_db_path: str
):
device_templates = ""
for field in fields:
slug = (
field[Cols.docs_wiki].replace("/instruments-wiki", "")
if Cols.docs_wiki in field and field.get(Cols.docs_wiki, None)
else f"{cat}/{field[Cols.vendor]}/{field.get(Cols.device_name,'').replace(' ', '-')}"
)
constructed_slugs = construct_slugs_by_cat([cat], slug)
_, _, slug = constructed_slugs[0]
image_url = validate_image_url(field)
unit_device_template = get_unit_device_template(
slug=slug.replace("instruments-database/", ""),
img=image_url,
device_name=field.get(Cols.correct_device_name, field[Cols.device_name]),
)
device_templates += unit_device_template
return device_templates
def all_instruments_by_lib(
lib: str, fields: list[Fields], template: str, ins_db_path: str
):
device_templates = ""
for field in fields:
categories = field.get(Cols.category, [])
slug = (
field[Cols.docs_wiki].replace("/instruments-wiki", "")
if Cols.docs_wiki in field and field.get(Cols.docs_wiki, None)
else f"{categories[0]}/{field[Cols.vendor]}/{field.get(Cols.device_name,'').replace(' ', '-')}"
)
image_url = validate_image_url(field)
constructed_slugs = construct_slugs_by_cat(categories, slug)
for _, _, slug_ in constructed_slugs:
unit_device_template = get_unit_device_template(
slug=slug_.replace("instruments-database/", ""),
img=image_url,
device_name=field.get(
Cols.correct_device_name, field[Cols.device_name]
),
)
device_templates += unit_device_template
return device_templates
def all_instruments_by_vendor(
vendor: str, fields: list[Fields], template: str, ins_db_path: str
):
device_templates = ""
for field in fields:
categories = field.get(Cols.category, [])
slug = (
field[Cols.docs_wiki].replace("/instruments-wiki", "")
if Cols.docs_wiki in field and field.get(Cols.docs_wiki, None)
else f"{categories[0]}/{field[Cols.vendor]}/{field.get(Cols.device_name,'').replace(' ', '-')}"
)
image_url = validate_image_url(field)
constructed_slugs = construct_slugs_by_cat(categories, slug)
for _, _, slug_ in constructed_slugs:
unit_device_template = get_unit_device_template(
slug=slug_.replace("instruments-database/", ""),
img=image_url,
device_name=field.get(
Cols.correct_device_name, field[Cols.device_name]
),
)
device_templates += unit_device_template
return device_templates
def process_with_mapping(
by: Literal["category", "library", "manufacturer"],
map_obj: dict[str, list[Fields]],
template: str,
base_path: str,
):
ins_db_path = os.path.join(base_path, "instruments-database")
# Ensure instruments-database folder
ensure_dirs(base_path=base_path, dir_path="instruments-database")
match by:
case "category":
for cat, fields in map_obj.items():
device_templates = all_instruments_by_category(
cat=cat, fields=fields, template=template, ins_db_path=ins_db_path
)
cat_template = get_category_template(
cat_name="Category",
cat_title=cat,
cat_desc=fields[0].get(Cols.category_description, ""),
device_templates=device_templates,
)
write_category_overview(
instrument_path=ins_db_path, cat_name=cat, cat_template=cat_template
)
template += cat_template
with open(os.path.join(ins_db_path, "all-instruments.mdx"), "w") as f:
f.write(template.strip())
return
case "library":
for lib, fields in map_obj.items():
device_templates = all_instruments_by_lib(
lib=lib, fields=fields, template=template, ins_db_path=ins_db_path
)
cat_template = get_category_template(
cat_name="Library",
cat_title=lib,
cat_desc=get_lib_desc(lib),
device_templates=device_templates,
)
template += cat_template
with open(
os.path.join(ins_db_path, "python-daq-library.mdx"), "w"
) as f:
f.write(template.strip())
return
case "manufacturer":
for vendor, fields in map_obj.items():
device_templates = all_instruments_by_vendor(
vendor=vendor,
fields=fields,
template=template,
ins_db_path=ins_db_path,
)
cat_template = get_category_template(
cat_name="Manufacturer",
cat_title=vendor,
cat_desc=fields[0].get(Cols.vendor_desc, ""),
device_templates=device_templates,
)
template += cat_template
with open(os.path.join(ins_db_path, "vendors.mdx"), "w") as f:
f.write(template.strip())
return
def generate_instruments_pages(
by: Literal["category", "library", "manufacturer"], base_path: str
):
"""
Generate all instruments page for given category, library or manufacturer
Args:
by (Literal['category', 'library', 'manufacturer']): category, library or manufacturer
base_path (str): path to instruments-database folder of docs"
"""
# Get mapping object for given category
mapping = get_libs_cats_map()
map_obj = mapping[by]
core_template = templates[by]
try:
process_with_mapping(
by=by, map_obj=map_obj, template=core_template, base_path=base_path
)
logger.info(f"Generated all instruments page by {by}.")
except Exception as e:
logger.error(f"Failed to generate all instruments page by {by}: {e}")
if __name__ == "__main__":
import argparse
# Create a parser object
parser = argparse.ArgumentParser(
description="A simple command-line argument parser"
)
# Define command-line arguments
parser.add_argument(
"-d", "--dir", type=str, help="Instruments database directory path"
)
# Parse the command-line arguments
args = parser.parse_args()
# Access the parsed arguments
docs_dir = args.dir if args.dir != "" else os.path.join(os.curdir, "docs")
generate_snippets()
generate_md_files(
base_path=docs_dir.replace("\\", "/").replace("/instruments-database", ""),
)