-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FEAT: remove tags from notebook cell if empty (#350)
* FIX: remove `slideshow` from config cell
- Loading branch information
Showing
4 changed files
with
55 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
"""Remove tags metadata from notebook cells if they are empty.""" | ||
|
||
from __future__ import annotations | ||
|
||
import argparse | ||
import sys | ||
from typing import Sequence | ||
|
||
import nbformat | ||
|
||
from compwa_policy.errors import PrecommitError | ||
from compwa_policy.utilities.executor import Executor | ||
from compwa_policy.utilities.notebook import load_notebook | ||
|
||
|
||
def main(argv: Sequence[str] | None = None) -> int: | ||
parser = argparse.ArgumentParser(__doc__) | ||
parser.add_argument("filenames", nargs="*", help="Filenames to check.") | ||
args = parser.parse_args(argv) | ||
|
||
with Executor(raise_exception=False) as do: | ||
for filename in args.filenames: | ||
do(_remove_cells, filename) | ||
return 1 if do.error_messages else 0 | ||
|
||
|
||
def _remove_cells(filename: str) -> None: | ||
notebook = load_notebook(filename) | ||
updated = False | ||
for cell in notebook["cells"]: | ||
metadata = cell["metadata"] | ||
tags = metadata.get("tags") | ||
if tags is None: | ||
continue | ||
if not tags: | ||
metadata.pop("tags") | ||
updated = True | ||
if updated: | ||
nbformat.write(notebook, filename) | ||
msg = f'Removed empty tags cell metadata from "{filename}"' | ||
raise PrecommitError(msg) | ||
|
||
|
||
if __name__ == "__main__": | ||
sys.exit(main()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters