|
90 | 90 | from argparse import ArgumentParser, RawDescriptionHelpFormatter
|
91 | 91 | import io
|
92 | 92 | import sys
|
| 93 | +from .utils import pop_recursive, strip_output |
| 94 | +__all__ = ["install", "uninstall", "status", "main"] |
93 | 95 |
|
94 | 96 | input_stream = None
|
95 | 97 | if sys.version_info < (3, 0):
|
@@ -132,106 +134,6 @@ def write(nb, f):
|
132 | 134 | return current.write(nb, f, 'json')
|
133 | 135 |
|
134 | 136 |
|
135 |
| -def _cells(nb): |
136 |
| - """Yield all cells in an nbformat-insensitive manner""" |
137 |
| - if nb.nbformat < 4: |
138 |
| - for ws in nb.worksheets: |
139 |
| - for cell in ws.cells: |
140 |
| - yield cell |
141 |
| - else: |
142 |
| - for cell in nb.cells: |
143 |
| - yield cell |
144 |
| - |
145 |
| - |
146 |
| -def pop_recursive(d, key, default=None): |
147 |
| - """dict.pop(key) where `key` is a `.`-delimited list of nested keys. |
148 |
| -
|
149 |
| - >>> d = {'a': {'b': 1, 'c': 2}} |
150 |
| - >>> pop_recursive(d, 'a.c') |
151 |
| - 2 |
152 |
| - >>> d |
153 |
| - {'a': {'b': 1}} |
154 |
| - """ |
155 |
| - nested = key.split('.') |
156 |
| - current = d |
157 |
| - for k in nested[:-1]: |
158 |
| - if hasattr(current, 'get'): |
159 |
| - current = current.get(k, {}) |
160 |
| - else: |
161 |
| - return default |
162 |
| - if not hasattr(current, 'pop'): |
163 |
| - return default |
164 |
| - return current.pop(nested[-1], default) |
165 |
| - |
166 |
| - |
167 |
| -def strip_output(nb, keep_output, keep_count, extra_keys=''): |
168 |
| - """ |
169 |
| - Strip the outputs, execution count/prompt number and miscellaneous |
170 |
| - metadata from a notebook object, unless specified to keep either the outputs |
171 |
| - or counts. |
172 |
| -
|
173 |
| - `extra_keys` could be 'metadata.foo cell.metadata.bar metadata.baz' |
174 |
| - """ |
175 |
| - extra_keys = extra_keys.split() |
176 |
| - keys = {'metadata': [], 'cell': {'metadata': []}} |
177 |
| - for key in extra_keys: |
178 |
| - if key.startswith('metadata.'): |
179 |
| - keys['metadata'].append(key[len('metadata.'):]) |
180 |
| - elif key.startswith('cell.metadata.'): |
181 |
| - keys['cell']['metadata'].append(key[len('cell.metadata.'):]) |
182 |
| - else: |
183 |
| - sys.stderr.write('ignoring extra key `%s`' % key) |
184 |
| - |
185 |
| - nb.metadata.pop('signature', None) |
186 |
| - nb.metadata.pop('widgets', None) |
187 |
| - for field in keys['metadata']: |
188 |
| - pop_recursive(nb.metadata, field) |
189 |
| - |
190 |
| - for cell in _cells(nb): |
191 |
| - |
192 |
| - keep_output_this_cell = keep_output |
193 |
| - |
194 |
| - # Keep the output for these cells, but strip count and metadata |
195 |
| - if cell.metadata.get('init_cell') or cell.metadata.get('keep_output'): |
196 |
| - keep_output_this_cell = True |
197 |
| - |
198 |
| - # Remove the outputs, unless directed otherwise |
199 |
| - if 'outputs' in cell: |
200 |
| - |
201 |
| - # Default behavior strips outputs. With all outputs stripped, |
202 |
| - # there are no counts to keep and keep_count is ignored. |
203 |
| - if not keep_output_this_cell: |
204 |
| - cell['outputs'] = [] |
205 |
| - |
206 |
| - # If keep_output_this_cell, but not keep_count, strip the counts |
207 |
| - # from the output. |
208 |
| - if keep_output_this_cell and not keep_count: |
209 |
| - for output in cell['outputs']: |
210 |
| - if 'execution_count' in output: |
211 |
| - output['execution_count'] = None |
212 |
| - |
213 |
| - # If keep_output_this_cell and keep_count, do nothing. |
214 |
| - |
215 |
| - # Remove the prompt_number/execution_count, unless directed otherwise |
216 |
| - if 'prompt_number' in cell and not keep_count: |
217 |
| - cell['prompt_number'] = None |
218 |
| - if 'execution_count' in cell and not keep_count: |
219 |
| - cell['execution_count'] = None |
220 |
| - |
221 |
| - # Always remove this metadata |
222 |
| - for output_style in ['collapsed', 'scrolled']: |
223 |
| - if output_style in cell.metadata: |
224 |
| - cell.metadata[output_style] = False |
225 |
| - if 'metadata' in cell: |
226 |
| - for field in ['collapsed', 'scrolled', 'ExecuteTime']: |
227 |
| - cell.metadata.pop(field, None) |
228 |
| - for (extra, fields) in keys['cell'].items(): |
229 |
| - if extra in cell: |
230 |
| - for field in fields: |
231 |
| - pop_recursive(getattr(cell, extra), field) |
232 |
| - return nb |
233 |
| - |
234 |
| - |
235 | 137 | def install(attrfile=None):
|
236 | 138 | """Install the git filter and set the git attributes."""
|
237 | 139 | from os import name, path
|
|
0 commit comments