Skip to content

Commit 7688f92

Browse files
committed
fixes #951
1 parent 6eaa4a8 commit 7688f92

File tree

9 files changed

+62
-90
lines changed

9 files changed

+62
-90
lines changed

nbdev/cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def nbdev_filter(
5454
):
5555
"A notebook filter for Quarto"
5656
os.environ["IN_TEST"] = "1"
57-
try: filt = get_config().get('exporter', FilterDefaults)()
57+
try: filt = globals()[get_config().get('exporter', 'FilterDefaults')]()
5858
except FileNotFoundError: filt = FilterDefaults()
5959
if fname: nb_txt = Path(fname).read_text()
6060
elif not nb_txt: nb_txt = sys.stdin.read()

nbdev/config.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -169,49 +169,52 @@ def nbdev_create_config(
169169
cfg_fn = Path(path)/cfg_name
170170
print(f'{cfg_fn} created.')
171171

172-
# %% ../nbs/09_API/01_config.ipynb 26
172+
# %% ../nbs/09_API/01_config.ipynb 23
173173
def _nbdev_config_file(cfg_name=_nbdev_cfg_name, path=None):
174174
cfg_path = path = Path.cwd() if path is None else Path(path)
175175
while cfg_path != cfg_path.parent and not (cfg_path/cfg_name).exists(): cfg_path = cfg_path.parent
176176
if not (cfg_path/cfg_name).exists(): cfg_path = path
177177
return cfg_path/cfg_name
178178

179-
# %% ../nbs/09_API/01_config.ipynb 28
179+
# %% ../nbs/09_API/01_config.ipynb 25
180180
def _xdg_config_paths(cfg_name=_nbdev_cfg_name):
181181
xdg_config_paths = reversed([xdg_config_home()]+xdg_config_dirs())
182182
return [o/_nbdev_home_dir/cfg_name for o in xdg_config_paths]
183183

184-
# %% ../nbs/09_API/01_config.ipynb 29
184+
# %% ../nbs/09_API/01_config.ipynb 26
185+
_types = dict(custom_sidebar=bool, nbs_path=Path, lib_path=Path, doc_path=Path, recursive=bool,
186+
black_formatting=bool, jupyter_hooks=bool, clean_ids=bool, custom_quarto_yml=bool)
187+
185188
@functools.lru_cache(maxsize=None)
186189
def get_config(cfg_name=_nbdev_cfg_name, path=None):
187190
"Return nbdev config."
188191
cfg_file = _nbdev_config_file(cfg_name, path)
189192
extra_files = _xdg_config_paths(cfg_name)
190-
cfg = Config(cfg_file.parent, cfg_file.name, extra_files=extra_files)
193+
cfg = Config(cfg_file.parent, cfg_file.name, extra_files=extra_files, types=_types)
191194
return _apply_defaults(cfg)
192195

193-
# %% ../nbs/09_API/01_config.ipynb 45
196+
# %% ../nbs/09_API/01_config.ipynb 41
194197
def config_key(c, default=None, path=True, missing_ok=None):
195198
"Deprecated: use `get_config().get` or `get_config().path` instead."
196199
warn("`config_key` is deprecated. Use `get_config().get` or `get_config().path` instead.", DeprecationWarning)
197200
return get_config().path(c, default) if path else get_config().get(c, default)
198201

199-
# %% ../nbs/09_API/01_config.ipynb 47
202+
# %% ../nbs/09_API/01_config.ipynb 43
200203
def create_output(txt, mime):
201204
"Add a cell output containing `txt` of the `mime` text MIME sub-type"
202205
return [{"data": { f"text/{mime}": str(txt).splitlines(True) },
203206
"execution_count": 1, "metadata": {}, "output_type": "execute_result"}]
204207

205-
# %% ../nbs/09_API/01_config.ipynb 48
208+
# %% ../nbs/09_API/01_config.ipynb 44
206209
def show_src(src, lang='python'): return Markdown(f'```{lang}\n{src}\n```')
207210

208-
# %% ../nbs/09_API/01_config.ipynb 51
211+
# %% ../nbs/09_API/01_config.ipynb 47
209212
_re_version = re.compile('^__version__\s*=.*$', re.MULTILINE)
210213
_init = '__init__.py'
211214

212215
def update_version(path=None):
213216
"Add or update `__version__` in the main `__init__.py` of the library."
214-
path = Path(path or get_config().path("lib_path"))
217+
path = Path(path or get_config().lib_path)
215218
fname = path/_init
216219
if not fname.exists(): fname.touch()
217220
version = f'__version__ = "{get_config().version}"'
@@ -225,7 +228,7 @@ def _has_py(fs): return any(1 for f in fs if f.endswith('.py'))
225228
def add_init(path=None):
226229
"Add `__init__.py` in all subdirs of `path` containing python files if it's not there already."
227230
# we add the lowest-level `__init__.py` files first, which ensures _has_py succeeds for parent modules
228-
path = Path(path or get_config().path("lib_path"))
231+
path = Path(path or get_config().lib_path)
229232
path.mkdir(exist_ok=True)
230233
if not (path/_init).exists(): (path/_init).touch()
231234
for r,ds,fs in os.walk(path, topdown=False):
@@ -234,16 +237,16 @@ def add_init(path=None):
234237
if _has_py(fs) or any(filter(_has_py, subds)) and not (r/_init).exists(): (r/_init).touch()
235238
update_version(path)
236239

237-
# %% ../nbs/09_API/01_config.ipynb 54
240+
# %% ../nbs/09_API/01_config.ipynb 50
238241
def write_cells(cells, hdr, file, offset=0):
239242
"Write `cells` to `file` along with header `hdr` starting at index `offset` (mainly for nbdev internal use)."
240243
for cell in cells:
241244
if cell.source.strip(): file.write(f'\n\n{hdr} {cell.idx_+offset}\n{cell.source}')
242245

243-
# %% ../nbs/09_API/01_config.ipynb 55
246+
# %% ../nbs/09_API/01_config.ipynb 51
244247
def _basic_export_nb(fname, name, dest=None):
245248
"Basic exporter to bootstrap nbdev."
246-
if dest is None: dest = get_config().path('lib_path')
249+
if dest is None: dest = get_config().lib_path
247250
add_init()
248251
fname,dest = Path(fname),Path(dest)
249252
nb = read_nb(fname)

nbdev/doclinks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ class NbdevLookup:
174174
def __init__(self, strip_libs=None, incl_libs=None, skip_mods=None):
175175
cfg = get_config()
176176
if strip_libs is None:
177-
try: strip_libs = cfg.get('strip_libs', cfg.get('lib_path', 'nbdev')).split()
177+
try: strip_libs = cfg.get('strip_libs', cfg.get('lib_path', 'nbdev').name).split()
178178
except FileNotFoundError: strip_libs = 'nbdev'
179179
skip_mods = setify(skip_mods)
180180
strip_libs = L(strip_libs)

nbdev/quarto.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
from fastcore.utils import *
1111
from fastcore.script import call_parse
12-
from fastcore.shutil import rmtree,move
12+
from fastcore.shutil import rmtree,move,copytree
1313
from fastcore.meta import delegates
1414

1515
from os import system
@@ -150,7 +150,7 @@ def nbdev_readme(
150150
_rdmi = tmp_doc_path/(Path(get_config().readme_nb).stem + '_files')
151151
if _rdm.exists(): _rdm.unlink() # py37 doesn't have arg missing_ok so have to check first
152152
move(tmp_doc_path/'README.md', cfg_path) # README.md is temporarily in nbs/_docs
153-
if _rdmi.exists(): move(_rdmi, cfg_path) # Move Supporting files for README
153+
if _rdmi.exists(): copytree(_rdmi, cfg_path/_rdmi.name) # Move Supporting files for README
154154

155155
# %% ../nbs/09_API/13_quarto.ipynb 19
156156
@call_parse
@@ -215,7 +215,7 @@ def refresh_quarto_yml():
215215
p = cfg.path('nbs_path')/'_quarto.yml'
216216
vals = {k:cfg.get(k) for k in ['doc_path', 'title', 'description', 'branch', 'git_url', 'doc_host', 'doc_baseurl']}
217217
# Do not build _quarto_yml if custom_quarto_yml is set to True
218-
if str2bool(get_config().custom_quarto_yml): return
218+
if str2bool(cfg.get('custom_quarto_yml', False)): return
219219
if 'title' not in vals: vals['title'] = vals['lib_name']
220220
yml=_quarto_yml.format(**vals)
221221
p.write_text(yml)

nbs/09_API/01_config.ipynb

Lines changed: 33 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
"nbdev is heavily customizeable, thanks to the configuration system defined in this module. There are 2 ways to interact with nbdev's config:\n",
6565
"\n",
6666
"- **In the terminal:** `nbdev_create_config` creates a config file (if you're starting a new project use `nbdev_new` instead)\n",
67-
"- **In your library:** `get_config` returns a [`Config`](https://fastcore.fast.ai/foundation.html#config) object.\n",
67+
"- **In your library:** `get_config` returns a `fastcore.foundation.Config` object.\n",
6868
"\n",
6969
"Read on for more about how these work."
7070
]
@@ -189,7 +189,16 @@
189189
"cell_type": "code",
190190
"execution_count": null,
191191
"metadata": {},
192-
"outputs": [],
192+
"outputs": [
193+
{
194+
"name": "stderr",
195+
"output_type": "stream",
196+
"text": [
197+
"/Users/jhoward/git/ghapi/ghapi/core.py:99: UserWarning: Neither GITHUB_TOKEN nor GITHUB_JWT_TOKEN found: running as unauthenticated\n",
198+
" else: warn('Neither GITHUB_TOKEN nor GITHUB_JWT_TOKEN found: running as unauthenticated')\n"
199+
]
200+
}
201+
],
193202
"source": [
194203
"#|hide\n",
195204
"if os.getenv('GITHUB_ACTIONS') != 'true': # GITHUB_TOKEN in actions has limited scope.\n",
@@ -365,30 +374,15 @@
365374
"cell_type": "markdown",
366375
"metadata": {},
367376
"source": [
368-
"The table above also serves as a full reference of nbdev's settings (excluding the `path` and `cfg_name` parameters which decide where the config file is saved). For more about PyPI classifiers, see [_Classifiers_](https://pypi.org/classifiers/)."
369-
]
370-
},
371-
{
372-
"cell_type": "markdown",
373-
"metadata": {},
374-
"source": [
375-
"You can create a config file by passing all of the required settings via the command line, as well as any optional settings you'd like to override, for example:"
376-
]
377-
},
378-
{
379-
"cell_type": "markdown",
380-
"metadata": {},
381-
"source": [
377+
"The table above also serves as a full reference of nbdev's settings (excluding the `path` and `cfg_name` parameters which decide where the config file is saved). For more about PyPI classifiers, see [_Classifiers_](https://pypi.org/classifiers/).\n",
378+
"\n",
379+
"You can create a config file by passing all of the required settings via the command line, as well as any optional settings you'd like to override, for example:\n",
380+
"\n",
382381
"```sh\n",
383382
"nbdev_create_config --repo nbdev --user fastai --author fastai \\\n",
384383
" --author_email [email protected] --description 'A test project'\n",
385-
"```"
386-
]
387-
},
388-
{
389-
"cell_type": "markdown",
390-
"metadata": {},
391-
"source": [
384+
"```\n",
385+
"\n",
392386
"If you don't provide required settings from the command line, we'll try to to infer them from git and GitHub. Finally, you'll be asked to manually input any required settings that we couldn't automatically fill in."
393387
]
394388
},
@@ -442,26 +436,24 @@
442436
"outputs": [],
443437
"source": [
444438
"#|export\n",
439+
"_types = dict(custom_sidebar=bool, nbs_path=Path, lib_path=Path, doc_path=Path, recursive=bool, \n",
440+
" black_formatting=bool, jupyter_hooks=bool, clean_ids=bool, custom_quarto_yml=bool)\n",
441+
"\n",
445442
"@functools.lru_cache(maxsize=None)\n",
446443
"def get_config(cfg_name=_nbdev_cfg_name, path=None):\n",
447444
" \"Return nbdev config.\"\n",
448445
" cfg_file = _nbdev_config_file(cfg_name, path)\n",
449446
" extra_files = _xdg_config_paths(cfg_name)\n",
450-
" cfg = Config(cfg_file.parent, cfg_file.name, extra_files=extra_files)\n",
447+
" cfg = Config(cfg_file.parent, cfg_file.name, extra_files=extra_files, types=_types)\n",
451448
" return _apply_defaults(cfg)"
452449
]
453450
},
454451
{
455452
"cell_type": "markdown",
456453
"metadata": {},
457454
"source": [
458-
"Searches up from `path` until `cfg_name` is found. User settings are loaded from `~/.config/nbdev/{cfg_name}`. Unspecified optional settings return defaults."
459-
]
460-
},
461-
{
462-
"cell_type": "markdown",
463-
"metadata": {},
464-
"source": [
455+
"Searches up from `path` until `cfg_name` is found. User settings are loaded from `~/.config/nbdev/{cfg_name}`. Unspecified optional settings return defaults.\n",
456+
"\n",
465457
"See `nbdev_create_config` for a full reference of nbdev's settings."
466458
]
467459
},
@@ -524,7 +516,7 @@
524516
"cell_type": "markdown",
525517
"metadata": {},
526518
"source": [
527-
"For convenience, use `path` to resolve settings that specify project-relative paths:"
519+
"Paths are relative to the project:"
528520
]
529521
},
530522
{
@@ -533,9 +525,9 @@
533525
"metadata": {},
534526
"outputs": [],
535527
"source": [
536-
"test_eq(cfg.path('doc_path'), p/'_docs')\n",
537-
"test_eq(cfg.path('lib_path'), p/'nbdev')\n",
538-
"test_eq(cfg.path('nbs_path'), p/'nbs')"
528+
"test_eq(cfg.doc_path, p/'_docs')\n",
529+
"test_eq(cfg.lib_path, p/'nbdev')\n",
530+
"test_eq(cfg.nbs_path, p/'nbs')"
539531
]
540532
},
541533
{
@@ -555,7 +547,7 @@
555547
" Config('.', 'test_settings.ini', {'repo': 'my-project', 'author': 'fastai', 'nbs_path': 'nbs'});\n",
556548
" cfg = get_config('test_settings.ini', '.')\n",
557549
" test_eq(cfg.repo, 'my-project')\n",
558-
" test_eq(cfg.lib_path, 'my_project')\n",
550+
" test_eq(cfg.lib_path.name, 'my_project')\n",
559551
" test_eq(cfg.copyright, '2022 ownwards, fastai')"
560552
]
561553
},
@@ -573,8 +565,8 @@
573565
"outputs": [],
574566
"source": [
575567
"cfg = get_config('test_settings.ini', '.')\n",
576-
"test_eq(cfg.lib_path, 'nbdev')\n",
577-
"test_eq(cfg.nbs_path, '.')"
568+
"test_eq(cfg.lib_path, Path('nbdev').resolve())\n",
569+
"test_eq(cfg.nbs_path, Path('.').resolve())"
578570
]
579571
},
580572
{
@@ -671,7 +663,7 @@
671663
"\n",
672664
"def update_version(path=None):\n",
673665
" \"Add or update `__version__` in the main `__init__.py` of the library.\"\n",
674-
" path = Path(path or get_config().path(\"lib_path\"))\n",
666+
" path = Path(path or get_config().lib_path)\n",
675667
" fname = path/_init\n",
676668
" if not fname.exists(): fname.touch()\n",
677669
" version = f'__version__ = \"{get_config().version}\"'\n",
@@ -685,7 +677,7 @@
685677
"def add_init(path=None):\n",
686678
" \"Add `__init__.py` in all subdirs of `path` containing python files if it's not there already.\"\n",
687679
" # we add the lowest-level `__init__.py` files first, which ensures _has_py succeeds for parent modules\n",
688-
" path = Path(path or get_config().path(\"lib_path\"))\n",
680+
" path = Path(path or get_config().lib_path)\n",
689681
" path.mkdir(exist_ok=True)\n",
690682
" if not (path/_init).exists(): (path/_init).touch()\n",
691683
" for r,ds,fs in os.walk(path, topdown=False):\n",
@@ -740,7 +732,7 @@
740732
"#|export\n",
741733
"def _basic_export_nb(fname, name, dest=None):\n",
742734
" \"Basic exporter to bootstrap nbdev.\"\n",
743-
" if dest is None: dest = get_config().path('lib_path')\n",
735+
" if dest is None: dest = get_config().lib_path\n",
744736
" add_init()\n",
745737
" fname,dest = Path(fname),Path(dest)\n",
746738
" nb = read_nb(fname)\n",

nbs/09_API/04b_doclinks.ipynb

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -243,18 +243,7 @@
243243
"cell_type": "code",
244244
"execution_count": null,
245245
"metadata": {},
246-
"outputs": [
247-
{
248-
"data": {
249-
"text/plain": [
250-
"Path('/home/jhoward/git/nbdev')"
251-
]
252-
},
253-
"execution_count": null,
254-
"metadata": {},
255-
"output_type": "execute_result"
256-
}
257-
],
246+
"outputs": [],
258247
"source": [
259248
"#|export\n",
260249
"@delegates(globtastic, but=('file_glob', 'skip_folder_re', 'skip_file_re'))\n",
@@ -440,7 +429,7 @@
440429
" def __init__(self, strip_libs=None, incl_libs=None, skip_mods=None):\n",
441430
" cfg = get_config()\n",
442431
" if strip_libs is None:\n",
443-
" try: strip_libs = cfg.get('strip_libs', cfg.get('lib_path', 'nbdev')).split()\n",
432+
" try: strip_libs = cfg.get('strip_libs', cfg.get('lib_path', 'nbdev').name).split()\n",
444433
" except FileNotFoundError: strip_libs = 'nbdev'\n",
445434
" skip_mods = setify(skip_mods)\n",
446435
" strip_libs = L(strip_libs)\n",
@@ -532,8 +521,6 @@
532521
"text/markdown": [
533522
"---\n",
534523
"\n",
535-
"[source](https://github.com/fastai/nbdev/blob/master/nbdev/doclinks.py#LNone){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
536-
"\n",
537524
"### NbdevLookup.doc\n",
538525
"\n",
539526
"> NbdevLookup.doc (sym)\n",
@@ -543,8 +530,6 @@
543530
"text/plain": [
544531
"---\n",
545532
"\n",
546-
"[source](https://github.com/fastai/nbdev/blob/master/nbdev/doclinks.py#LNone){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
547-
"\n",
548533
"### NbdevLookup.doc\n",
549534
"\n",
550535
"> NbdevLookup.doc (sym)\n",
@@ -626,8 +611,6 @@
626611
"text/markdown": [
627612
"---\n",
628613
"\n",
629-
"[source](https://github.com/fastai/nbdev/blob/master/nbdev/doclinks.py#LNone){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
630-
"\n",
631614
"### NbdevLookup.code\n",
632615
"\n",
633616
"> NbdevLookup.code (sym)\n",
@@ -637,8 +620,6 @@
637620
"text/plain": [
638621
"---\n",
639622
"\n",
640-
"[source](https://github.com/fastai/nbdev/blob/master/nbdev/doclinks.py#LNone){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
641-
"\n",
642623
"### NbdevLookup.code\n",
643624
"\n",
644625
"> NbdevLookup.code (sym)\n",
@@ -685,17 +666,13 @@
685666
"text/markdown": [
686667
"---\n",
687668
"\n",
688-
"[source](https://github.com/fastai/nbdev/blob/master/nbdev/doclinks.py#LNone){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
689-
"\n",
690669
"### NbdevLookup.linkify\n",
691670
"\n",
692671
"> NbdevLookup.linkify (md)"
693672
],
694673
"text/plain": [
695674
"---\n",
696675
"\n",
697-
"[source](https://github.com/fastai/nbdev/blob/master/nbdev/doclinks.py#LNone){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
698-
"\n",
699676
"### NbdevLookup.linkify\n",
700677
"\n",
701678
"> NbdevLookup.linkify (md)"

0 commit comments

Comments
 (0)