Correct notebook tag logic (#317)

This commit is contained in:
Matthew Tancik authored and GitHub committed 2022-08-31 09:38:21 -07:00
1 parent 887d2c4bf6
commit 2a94afd9ec
2 files changed
+47 -19

No files matched your search

+20 -9
View File
@@ -35,19 +35,30 @@ def main(check: bool = False):
for ipath in notebooks:
ntbk = nbf.read(ipath, nbf.NO_CONVERT)
missing_metadata = False
incorrect_metadata = False
for cell in ntbk.cells:
cell_tags = cell.get("metadata", {}).get("tags", [])
found_keys = []
found_tags = []
for key, val in text_search_dict.items():
if key in cell["source"]:
if val not in cell_tags:
missing_metadata = True
cell_tags.append(val)
if len(cell_tags) > 0:
cell["metadata"]["tags"] = cell_tags
if missing_metadata:
if key in cell.source:
found_keys.append(key)
found_tags.append(val)
if len(found_keys) > 1:
print_yellow(f"Found multiple tags {found_keys} for {ipath}")
sys.exit(1)
if len(cell_tags) != len(found_tags):
incorrect_metadata = True
elif len(cell_tags) == 1 and len(found_keys) == 1:
if found_tags[0] != cell_tags[0]:
incorrect_metadata = True
cell["metadata"]["tags"] = found_tags
if incorrect_metadata:
if check:
print_yellow(f"{ipath} is missing metadata. Call `python scripts.docs.add_nb_tags.py` to add it.")
print_yellow(f"{ipath} has incorrect metadata. Call `python scripts.docs.add_nb_tags.py` to add it.")
any_missing = True
else:
print(f"Adding metadata to {ipath}")