@@ -43,35 +43,43 @@ def patch_name(o):
43
43
return _sym_nm (a ,o )
44
44
45
45
# %% ../nbs/api/doclinks.ipynb 9
46
- def _nbpath2html (p ): return p .with_name (re .sub (r'\d+[a-zA-Z0-9]*_' , '' , p .name .lower ())).with_suffix ('.html' )
46
+ def _iter_py_cells (p ):
47
+ "Yield cells from an exported Python file."
48
+ p = Path (p )
49
+ cells = p .read_text ().split ("\n # %% " )
50
+ for cell in cells [1 :]:
51
+ top ,code = cell .split ('\n ' , 1 )
52
+ nb ,idx = top .split ()
53
+ nb_path = None if nb == 'auto' else (p .parent / nb ).resolve () # NB paths are stored relative to .py file
54
+ if code .endswith ('\n ' ): code = code [:- 1 ]
55
+ yield AttrDict (nb = nb , idx = int (idx ), code = code , nb_path = nb_path , py_path = p .resolve ())
47
56
48
57
# %% ../nbs/api/doclinks.ipynb 11
49
- def _get_modidx (pyfile , code_root , nbs_path ):
58
+ def _nbpath2html (p ): return p .with_name (re .sub (r'\d+[a-zA-Z0-9]*_' , '' , p .name .lower ())).with_suffix ('.html' )
59
+
60
+ # %% ../nbs/api/doclinks.ipynb 13
61
+ def _get_modidx (py_path , code_root , nbs_path ):
50
62
"Get module symbol index for a Python source file"
51
63
cfg = get_config ()
52
- rel_name = pyfile .resolve ().relative_to (code_root ).as_posix ()
53
- mod_name = '.' .join (rel_name .rpartition ('.' )[0 ].split ('/' )) # module name created by pyfile
54
- cells = Path (pyfile ).read_text ().split ("\n # %% " )
64
+ rel_name = py_path .resolve ().relative_to (code_root ).as_posix ()
65
+ mod_name = '.' .join (rel_name .rpartition ('.' )[0 ].split ('/' )) # module name created by py_path
55
66
56
67
_def_types = ast .FunctionDef ,ast .AsyncFunctionDef ,ast .ClassDef
57
68
d = {}
58
- for cell in cells [1 :]: # First cell is autogenerated header
59
- top ,* rest = cell .splitlines () # First line is cell header
60
- nb = top .split ()[0 ]
61
- if nb != 'auto' :
62
- nbpath = (pyfile .parent / nb ).resolve () # NB paths are stored relative to .py file
63
- loc = _nbpath2html (nbpath .relative_to (nbs_path ))
64
-
65
- def _stor (nm ):
66
- for n in L (nm ): d [f'{ mod_name } .{ n } ' ] = f'{ loc .as_posix ()} #{ n .lower ()} ' ,rel_name
67
- for tree in ast .parse ('\n ' .join (rest )).body :
68
- if isinstance (tree , _def_types ): _stor (patch_name (tree ))
69
- if isinstance (tree , ast .ClassDef ):
70
- for t2 in tree .body :
71
- if isinstance (t2 , _def_types ): _stor (f'{ tree .name } .{ t2 .name } ' )
69
+ for cell in _iter_py_cells (py_path ):
70
+ if cell .nb == 'auto' : continue
71
+ loc = _nbpath2html (cell .nb_path .relative_to (nbs_path ))
72
+
73
+ def _stor (nm ):
74
+ for n in L (nm ): d [f'{ mod_name } .{ n } ' ] = f'{ loc .as_posix ()} #{ n .lower ()} ' ,rel_name
75
+ for tree in ast .parse (cell .code ).body :
76
+ if isinstance (tree , _def_types ): _stor (patch_name (tree ))
77
+ if isinstance (tree , ast .ClassDef ):
78
+ for t2 in tree .body :
79
+ if isinstance (t2 , _def_types ): _stor (f'{ tree .name } .{ t2 .name } ' )
72
80
return {mod_name : d }
73
81
74
- # %% ../nbs/api/doclinks.ipynb 12
82
+ # %% ../nbs/api/doclinks.ipynb 15
75
83
def _build_modidx (dest = None , nbs_path = None , skip_exists = False ):
76
84
"Create _modidx.py"
77
85
if dest is None : dest = get_config ().lib_path
@@ -89,7 +97,7 @@ def _build_modidx(dest=None, nbs_path=None, skip_exists=False):
89
97
res ['syms' ].update (_get_modidx ((dest .parent / file ).resolve (), code_root , nbs_path = nbs_path ))
90
98
idxfile .write_text ("# Autogenerated by nbdev\n \n d = " + pformat (res , width = 140 , indent = 2 , compact = True ))
91
99
92
- # %% ../nbs/api/doclinks.ipynb 17
100
+ # %% ../nbs/api/doclinks.ipynb 20
93
101
@delegates (globtastic )
94
102
def nbglob (path = None , skip_folder_re = '^[_.]' , file_glob = '*.ipynb' , skip_file_re = '^[_.]' , key = 'nbs_path' , as_path = False , ** kwargs ):
95
103
"Find all files in a directory matching an extension given a config key."
@@ -99,7 +107,7 @@ def nbglob(path=None, skip_folder_re = '^[_.]', file_glob='*.ipynb', skip_file_r
99
107
skip_file_re = skip_file_re , recursive = recursive , ** kwargs )
100
108
return res .map (Path ) if as_path else res
101
109
102
- # %% ../nbs/api/doclinks.ipynb 18
110
+ # %% ../nbs/api/doclinks.ipynb 21
103
111
def nbglob_cli (
104
112
path :str = None , # Path to notebooks
105
113
symlinks :bool = False , # Follow symlinks?
@@ -113,7 +121,7 @@ def nbglob_cli(
113
121
return nbglob (path , symlinks = symlinks , file_glob = file_glob , file_re = file_re , folder_re = folder_re ,
114
122
skip_file_glob = skip_file_glob , skip_file_re = skip_file_re , skip_folder_re = skip_folder_re )
115
123
116
- # %% ../nbs/api/doclinks.ipynb 19
124
+ # %% ../nbs/api/doclinks.ipynb 22
117
125
@call_parse
118
126
@delegates (nbglob_cli )
119
127
def nbdev_export (
@@ -126,11 +134,11 @@ def nbdev_export(
126
134
add_init (get_config ().lib_path )
127
135
_build_modidx ()
128
136
129
- # %% ../nbs/api/doclinks.ipynb 21
137
+ # %% ../nbs/api/doclinks.ipynb 24
130
138
import importlib ,ast
131
139
from functools import lru_cache
132
140
133
- # %% ../nbs/api/doclinks.ipynb 22
141
+ # %% ../nbs/api/doclinks.ipynb 25
134
142
def _find_mod (mod ):
135
143
mp ,_ ,mr = mod .partition ('/' )
136
144
spec = importlib .util .find_spec (mp )
@@ -153,7 +161,7 @@ def _get_exps(mod):
153
161
154
162
def _lineno (sym , fname ): return _get_exps (fname ).get (sym , None ) if fname else None
155
163
156
- # %% ../nbs/api/doclinks.ipynb 24
164
+ # %% ../nbs/api/doclinks.ipynb 27
157
165
def _qual_sym (s , settings ):
158
166
if not isinstance (s ,tuple ): return s
159
167
nb ,py = s
@@ -168,10 +176,10 @@ def _qual_syms(entries):
168
176
if 'doc_host' not in settings : return entries
169
177
return {'syms' : {mod :_qual_mod (d , settings ) for mod ,d in entries ['syms' ].items ()}, 'settings' :settings }
170
178
171
- # %% ../nbs/api/doclinks.ipynb 25
179
+ # %% ../nbs/api/doclinks.ipynb 28
172
180
_re_backticks = re .compile (r'`([^`\s]+)`' )
173
181
174
- # %% ../nbs/api/doclinks.ipynb 26
182
+ # %% ../nbs/api/doclinks.ipynb 29
175
183
@lru_cache (None )
176
184
class NbdevLookup :
177
185
"Mapping from symbol names to docs and source URLs"
0 commit comments