11# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/02_export.ipynb.
22
33# %% auto 0
4- __all__ = ['extract_comments' , 'NBProcessor' , 'ExportModuleProc' , 'rm_comments_proc' , 'create_modules' , 'nb_export' , 'nbs_export' ]
4+ __all__ = ['extract_comments' , 'NBProcessor' , 'outp_proc' , 'strip_ansi' , 'ExportModuleProc' , 'rm_comments_proc' , 'create_modules' ,
5+ 'nb_export' , 'nbs_export' ]
56
67# %% ../nbs/02_export.ipynb 3
78from .read import *
@@ -36,31 +37,51 @@ def _param_count(f):
3637# %% ../nbs/02_export.ipynb 12
3738class NBProcessor :
3839 "Process cells and nbdev comments in a notebook"
39- def __init__ (self , path = None , procs = None , nb = None , debug = False ):
40+ def __init__ (self , path = None , procs = None , outp_procs = None , nb = None , debug = False ):
4041 self .nb = read_nb (path ) if nb is None else nb
41- self .procs ,self .debug = L (procs ),debug
42+ self .procs ,self .outp_procs , self . debug = L (procs ), L ( outp_procs ),debug
4243
4344 def _process_cell (self , cell ):
4445 self .cell = cell
4546 cell ._comments = extract_comments (cell .source )
4647 for proc in self .procs :
4748 if callable (proc ): proc (cell )
48- if cell .cell_type == 'code' :
49- for comment in cell . _comments : self . _process_comment ( proc , comment )
49+ if cell .cell_type == 'code' : cell . _comments . map ( self . _process_comment , proc )
50+ for proc in self . outp_procs : L ( cell . get ( 'outputs' , [])). map ( proc , cell )
5051
5152 def _process_comment (self , proc , comment ):
5253 cmd ,* args = comment
5354 f = getattr (proc , f'_{ cmd } _' , None )
5455 if not f or _param_count (f )- 1 < len (args ): return True
5556 if self .debug : print (cmd , args , f )
5657 return f (self , * args )
57-
58+
5859 def process (self ):
5960 "Process all cells with `process_cell`"
6061 for i in range_of (self .nb .cells ): self ._process_cell (self .nb .cells [i ])
6162 self .nb .cells = [c for c in self .nb .cells if c .source is not None ]
6263
6364# %% ../nbs/02_export.ipynb 16
65+ class outp_proc :
66+ "Decorator for funcs to pass to `NBProcessor` for processing cell output"
67+ def __init__ (self , * idxs ): self .idxs = idxs
68+ def __call__ (self , func ):
69+ def _inner (cell , outp ):
70+ item = nested_idx (outp , * self .idxs )
71+ if not item : return
72+ res = func (cell , outp , '' .join (item ))
73+ if res is not None : set_nested_idx (outp , res .splitlines (True ), * self .idxs )
74+ return _inner
75+
76+ # %% ../nbs/02_export.ipynb 17
77+ _re_ansi_escape = re .compile (r'\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])' )
78+
79+ @outp_proc ('text' )
80+ def strip_ansi (cell , outp , item ):
81+ "Output processor to strip Ansi Characters"
82+ if outp .get ('name' ) == 'stdout' : return _re_ansi_escape .sub ('' , item )
83+
84+ # %% ../nbs/02_export.ipynb 20
6485class ExportModuleProc :
6586 "A processor which exports code to a module"
6687 def __init__ (self ): self .modules ,self .in_all = defaultdict (L ),defaultdict (L )
@@ -70,12 +91,12 @@ def _export_(self, nbp, exp_to=None):
7091 self ._exporti_ (nbp , exp_to )
7192 self .in_all [ifnone (exp_to , '#' )].append (nbp .cell )
7293
73- # %% ../nbs/02_export.ipynb 19
94+ # %% ../nbs/02_export.ipynb 23
7495def rm_comments_proc (cell ):
7596 "A proc that removes comments from each NB cell source"
7697 cell .source = '' .join (cell .source .splitlines (True )[len (cell ._comments ):])
7798
78- # %% ../nbs/02_export.ipynb 20
99+ # %% ../nbs/02_export.ipynb 24
79100def create_modules (path , dest , procs = None , debug = False , mod_maker = ModuleMaker ):
80101 "Create module(s) from notebook"
81102 exp = ExportModuleProc ()
@@ -87,12 +108,12 @@ def create_modules(path, dest, procs=None, debug=False, mod_maker=ModuleMaker):
87108 mm = mod_maker (dest = dest , name = name , nb_path = path , is_new = mod == '#' )
88109 mm .make (cells , all_cells )
89110
90- # %% ../nbs/02_export.ipynb 27
111+ # %% ../nbs/02_export.ipynb 31
91112def nb_export (nbname , lib_name = None ):
92113 if lib_name is None : lib_name = get_config ().lib_name
93114 create_modules (nbname , lib_name )
94115
95- # %% ../nbs/02_export.ipynb 28
116+ # %% ../nbs/02_export.ipynb 32
96117@call_parse
97118def nbs_export (
98119 path :str = '.' , # path or filename
0 commit comments