1
+ """Strip superfluous metadata from notebooks"""
2
+
1
3
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/api/11_clean.ipynb.
2
4
3
5
# %% auto 0
4
6
__all__ = ['nbdev_trust' , 'clean_nb' , 'process_write' , 'nbdev_clean' , 'clean_jupyter' , 'nbdev_install_hooks' ]
5
7
6
- # %% ../nbs/api/11_clean.ipynb 2
8
+ # %% ../nbs/api/11_clean.ipynb
7
9
import ast ,warnings ,stat
8
10
from astunparse import unparse
9
11
from textwrap import indent
18
20
from .sync import *
19
21
from .process import first_code_ln
20
22
21
- # %% ../nbs/api/11_clean.ipynb 6
23
+ # %% ../nbs/api/11_clean.ipynb
22
24
@call_parse
23
25
def nbdev_trust (
24
26
fname :str = None , # A notebook name or glob to trust
@@ -45,7 +47,7 @@ def nbdev_trust(
45
47
if not NotebookNotary ().check_signature (nb ): NotebookNotary ().sign (nb )
46
48
check_fname .touch (exist_ok = True )
47
49
48
- # %% ../nbs/api/11_clean.ipynb 9
50
+ # %% ../nbs/api/11_clean.ipynb
49
51
_repr_id_re = re .compile ('(<.*?)( at 0x[0-9a-fA-F]+)(>)' )
50
52
51
53
_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
55
57
def _clean_cell_output_id (lines ):
56
58
return _skip_or_sub (lines ) if isinstance (lines ,str ) else [_skip_or_sub (o ) for o in lines ]
57
59
58
- # %% ../nbs/api/11_clean.ipynb 11
60
+ # %% ../nbs/api/11_clean.ipynb
59
61
def _clean_cell_output (cell , clean_ids ):
60
62
"Remove `cell` output execution count and optionally ids from text reprs"
61
63
outputs = cell .get ('outputs' , [])
@@ -69,7 +71,7 @@ def _clean_cell_output(cell, clean_ids):
69
71
if 'text' in o and clean_ids : o ['text' ] = _clean_cell_output_id (o ['text' ])
70
72
o .get ('metadata' , {}).pop ('tags' , None )
71
73
72
- # %% ../nbs/api/11_clean.ipynb 12
74
+ # %% ../nbs/api/11_clean.ipynb
73
75
def _clean_cell (cell , clear_all , allowed_metadata_keys , clean_ids ):
74
76
"Clean `cell` by removing superfluous metadata or everything except the input if `clear_all`"
75
77
if 'execution_count' in cell : cell ['execution_count' ] = None
@@ -80,7 +82,7 @@ def _clean_cell(cell, clear_all, allowed_metadata_keys, clean_ids):
80
82
cell ['metadata' ] = {} if clear_all else {
81
83
k :v for k ,v in cell ['metadata' ].items () if k in allowed_metadata_keys }
82
84
83
- # %% ../nbs/api/11_clean.ipynb 13
85
+ # %% ../nbs/api/11_clean.ipynb
84
86
def clean_nb (
85
87
nb , # The notebook to clean
86
88
clear_all = False , # Remove all cell metadata and cell outputs?
@@ -98,12 +100,12 @@ def clean_nb(
98
100
nb ['metadata' ]['kernelspec' ]['display_name' ] = nb ["metadata" ]["kernelspec" ]["name" ]
99
101
nb ['metadata' ] = {k :v for k ,v in nb ['metadata' ].items () if k in metadata_keys }
100
102
101
- # %% ../nbs/api/11_clean.ipynb 26
103
+ # %% ../nbs/api/11_clean.ipynb
102
104
def _reconfigure (* strms ):
103
105
for s in strms :
104
106
if hasattr (s ,'reconfigure' ): s .reconfigure (encoding = 'utf-8' )
105
107
106
- # %% ../nbs/api/11_clean.ipynb 27
108
+ # %% ../nbs/api/11_clean.ipynb
107
109
def process_write (warn_msg , proc_nb , f_in , f_out = None , disp = False ):
108
110
if not f_out : f_out = f_in
109
111
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):
116
118
warn (f'{ warn_msg } ' )
117
119
warn (e )
118
120
119
- # %% ../nbs/api/11_clean.ipynb 28
121
+ # %% ../nbs/api/11_clean.ipynb
120
122
def _nbdev_clean (nb , path = None , clear_all = None ):
121
123
cfg = get_config (path = path )
122
124
clear_all = clear_all or cfg .clear_all
@@ -125,7 +127,7 @@ def _nbdev_clean(nb, path=None, clear_all=None):
125
127
clean_nb (nb , clear_all , allowed_metadata_keys , allowed_cell_metadata_keys , cfg .clean_ids )
126
128
if path : nbdev_trust .__wrapped__ (path )
127
129
128
- # %% ../nbs/api/11_clean.ipynb 29
130
+ # %% ../nbs/api/11_clean.ipynb
129
131
@call_parse
130
132
def nbdev_clean (
131
133
fname :str = None , # A notebook name or glob to clean
@@ -141,15 +143,15 @@ def nbdev_clean(
141
143
if fname is None : fname = get_config ().nbs_path
142
144
for f in globtastic (fname , file_glob = '*.ipynb' , skip_folder_re = '^[_.]' ): _write (f_in = f , disp = disp )
143
145
144
- # %% ../nbs/api/11_clean.ipynb 32
146
+ # %% ../nbs/api/11_clean.ipynb
145
147
def clean_jupyter (path , model , ** kwargs ):
146
148
"Clean Jupyter `model` pre save to `path`"
147
149
if not (model ['type' ]== 'notebook' and model ['content' ]['nbformat' ]== 4 ): return
148
150
get_config .cache_clear () # Allow config changes without restarting Jupyter
149
151
jupyter_hooks = get_config (path = path ).jupyter_hooks
150
152
if jupyter_hooks : _nbdev_clean (model ['content' ], path = path )
151
153
152
- # %% ../nbs/api/11_clean.ipynb 35
154
+ # %% ../nbs/api/11_clean.ipynb
153
155
_pre_save_hook_src = '''
154
156
def nbdev_clean_jupyter(**kwargs):
155
157
try: from nbdev.clean import clean_jupyter
@@ -159,7 +161,7 @@ def nbdev_clean_jupyter(**kwargs):
159
161
c.ContentsManager.pre_save_hook = nbdev_clean_jupyter''' .strip ()
160
162
_pre_save_hook_re = re .compile (r'c\.(File)?ContentsManager\.pre_save_hook' )
161
163
162
- # %% ../nbs/api/11_clean.ipynb 36
164
+ # %% ../nbs/api/11_clean.ipynb
163
165
def _add_jupyter_hooks (src , path ):
164
166
if _pre_save_hook_src in src : return
165
167
mod = ast .parse (src )
@@ -177,12 +179,12 @@ def _add_jupyter_hooks(src, path):
177
179
if src : src += '\n \n '
178
180
return src + _pre_save_hook_src
179
181
180
- # %% ../nbs/api/11_clean.ipynb 40
182
+ # %% ../nbs/api/11_clean.ipynb
181
183
def _git_root ():
182
184
try : return Path (run ('git rev-parse --show-toplevel' ))
183
185
except OSError : return None
184
186
185
- # %% ../nbs/api/11_clean.ipynb 43
187
+ # %% ../nbs/api/11_clean.ipynb
186
188
@call_parse
187
189
def nbdev_install_hooks ():
188
190
"Install Jupyter and git hooks to automatically clean, trust, and fix merge conflicts in notebooks"
0 commit comments