1+ """Strip superfluous metadata from notebooks"""
2+
13# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/api/11_clean.ipynb.
24
35# %% auto 0
46__all__ = ['nbdev_trust' , 'clean_nb' , 'process_write' , 'nbdev_clean' , 'clean_jupyter' , 'nbdev_install_hooks' ]
57
6- # %% ../nbs/api/11_clean.ipynb 2
8+ # %% ../nbs/api/11_clean.ipynb
79import ast ,warnings ,stat
810from astunparse import unparse
911from textwrap import indent
1820from .sync import *
1921from .process import first_code_ln
2022
21- # %% ../nbs/api/11_clean.ipynb 6
23+ # %% ../nbs/api/11_clean.ipynb
2224@call_parse
2325def nbdev_trust (
2426 fname :str = None , # A notebook name or glob to trust
@@ -45,7 +47,7 @@ def nbdev_trust(
4547 if not NotebookNotary ().check_signature (nb ): NotebookNotary ().sign (nb )
4648 check_fname .touch (exist_ok = True )
4749
48- # %% ../nbs/api/11_clean.ipynb 9
50+ # %% ../nbs/api/11_clean.ipynb
4951_repr_id_re = re .compile ('(<.*?)( at 0x[0-9a-fA-F]+)(>)' )
5052
5153_sub = partial (_repr_id_re .sub , r'\1\3' )
@@ -55,7 +57,7 @@ def _skip_or_sub(x): return _sub(x) if "at 0x" in x else x
5557def _clean_cell_output_id (lines ):
5658 return _skip_or_sub (lines ) if isinstance (lines ,str ) else [_skip_or_sub (o ) for o in lines ]
5759
58- # %% ../nbs/api/11_clean.ipynb 11
60+ # %% ../nbs/api/11_clean.ipynb
5961def _clean_cell_output (cell , clean_ids ):
6062 "Remove `cell` output execution count and optionally ids from text reprs"
6163 outputs = cell .get ('outputs' , [])
@@ -69,7 +71,7 @@ def _clean_cell_output(cell, clean_ids):
6971 if 'text' in o and clean_ids : o ['text' ] = _clean_cell_output_id (o ['text' ])
7072 o .get ('metadata' , {}).pop ('tags' , None )
7173
72- # %% ../nbs/api/11_clean.ipynb 12
74+ # %% ../nbs/api/11_clean.ipynb
7375def _clean_cell (cell , clear_all , allowed_metadata_keys , clean_ids ):
7476 "Clean `cell` by removing superfluous metadata or everything except the input if `clear_all`"
7577 if 'execution_count' in cell : cell ['execution_count' ] = None
@@ -80,7 +82,7 @@ def _clean_cell(cell, clear_all, allowed_metadata_keys, clean_ids):
8082 cell ['metadata' ] = {} if clear_all else {
8183 k :v for k ,v in cell ['metadata' ].items () if k in allowed_metadata_keys }
8284
83- # %% ../nbs/api/11_clean.ipynb 13
85+ # %% ../nbs/api/11_clean.ipynb
8486def clean_nb (
8587 nb , # The notebook to clean
8688 clear_all = False , # Remove all cell metadata and cell outputs?
@@ -98,12 +100,12 @@ def clean_nb(
98100 nb ['metadata' ]['kernelspec' ]['display_name' ] = nb ["metadata" ]["kernelspec" ]["name" ]
99101 nb ['metadata' ] = {k :v for k ,v in nb ['metadata' ].items () if k in metadata_keys }
100102
101- # %% ../nbs/api/11_clean.ipynb 26
103+ # %% ../nbs/api/11_clean.ipynb
102104def _reconfigure (* strms ):
103105 for s in strms :
104106 if hasattr (s ,'reconfigure' ): s .reconfigure (encoding = 'utf-8' )
105107
106- # %% ../nbs/api/11_clean.ipynb 27
108+ # %% ../nbs/api/11_clean.ipynb
107109def process_write (warn_msg , proc_nb , f_in , f_out = None , disp = False ):
108110 if not f_out : f_out = f_in
109111 if isinstance (f_in , (str ,Path )): f_in = Path (f_in ).open (encoding = "utf-8" )
@@ -116,7 +118,7 @@ def process_write(warn_msg, proc_nb, f_in, f_out=None, disp=False):
116118 warn (f'{ warn_msg } ' )
117119 warn (e )
118120
119- # %% ../nbs/api/11_clean.ipynb 28
121+ # %% ../nbs/api/11_clean.ipynb
120122def _nbdev_clean (nb , path = None , clear_all = None ):
121123 cfg = get_config (path = path )
122124 clear_all = clear_all or cfg .clear_all
@@ -125,7 +127,7 @@ def _nbdev_clean(nb, path=None, clear_all=None):
125127 clean_nb (nb , clear_all , allowed_metadata_keys , allowed_cell_metadata_keys , cfg .clean_ids )
126128 if path : nbdev_trust .__wrapped__ (path )
127129
128- # %% ../nbs/api/11_clean.ipynb 29
130+ # %% ../nbs/api/11_clean.ipynb
129131@call_parse
130132def nbdev_clean (
131133 fname :str = None , # A notebook name or glob to clean
@@ -141,15 +143,15 @@ def nbdev_clean(
141143 if fname is None : fname = get_config ().nbs_path
142144 for f in globtastic (fname , file_glob = '*.ipynb' , skip_folder_re = '^[_.]' ): _write (f_in = f , disp = disp )
143145
144- # %% ../nbs/api/11_clean.ipynb 32
146+ # %% ../nbs/api/11_clean.ipynb
145147def clean_jupyter (path , model , ** kwargs ):
146148 "Clean Jupyter `model` pre save to `path`"
147149 if not (model ['type' ]== 'notebook' and model ['content' ]['nbformat' ]== 4 ): return
148150 get_config .cache_clear () # Allow config changes without restarting Jupyter
149151 jupyter_hooks = get_config (path = path ).jupyter_hooks
150152 if jupyter_hooks : _nbdev_clean (model ['content' ], path = path )
151153
152- # %% ../nbs/api/11_clean.ipynb 35
154+ # %% ../nbs/api/11_clean.ipynb
153155_pre_save_hook_src = '''
154156def nbdev_clean_jupyter(**kwargs):
155157 try: from nbdev.clean import clean_jupyter
@@ -159,7 +161,7 @@ def nbdev_clean_jupyter(**kwargs):
159161c.ContentsManager.pre_save_hook = nbdev_clean_jupyter''' .strip ()
160162_pre_save_hook_re = re .compile (r'c\.(File)?ContentsManager\.pre_save_hook' )
161163
162- # %% ../nbs/api/11_clean.ipynb 36
164+ # %% ../nbs/api/11_clean.ipynb
163165def _add_jupyter_hooks (src , path ):
164166 if _pre_save_hook_src in src : return
165167 mod = ast .parse (src )
@@ -177,12 +179,12 @@ def _add_jupyter_hooks(src, path):
177179 if src : src += '\n \n '
178180 return src + _pre_save_hook_src
179181
180- # %% ../nbs/api/11_clean.ipynb 40
182+ # %% ../nbs/api/11_clean.ipynb
181183def _git_root ():
182184 try : return Path (run ('git rev-parse --show-toplevel' ))
183185 except OSError : return None
184186
185- # %% ../nbs/api/11_clean.ipynb 43
187+ # %% ../nbs/api/11_clean.ipynb
186188@call_parse
187189def nbdev_install_hooks ():
188190 "Install Jupyter and git hooks to automatically clean, trust, and fix merge conflicts in notebooks"
0 commit comments