Skip to content

Commit

Permalink
deprecate config_key in favor of get_config
Browse files Browse the repository at this point in the history
  • Loading branch information
seeM committed Aug 14, 2022
1 parent 5bb8f8e commit be84ccc
Show file tree
Hide file tree
Showing 16 changed files with 63 additions and 66 deletions.
1 change: 1 addition & 0 deletions nbdev/_modidx.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
'nbdev_release_both=nbdev.release:release_both\n'
'nbdev_help=nbdev.shortcuts:chelp',
'copyright': '2020 onwards, Jeremy Howard',
'custom_quarto_yml': 'False',
'custom_sidebar': 'True',
'description': 'Create delightful software with Jupyter Notebooks',
'dev_requirements': 'nbdev-numpy nbdev-stdlib pandas matplotlib black',
Expand Down
4 changes: 2 additions & 2 deletions nbdev/clean.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def nbdev_trust(
warnings.warn("Please install jupyter and try again")
return

fname = Path(fname if fname else config_key("nbs_path"))
fname = Path(fname if fname else get_config().path('nbs_path'))
path = fname if fname.is_dir() else fname.parent
check_fname = path/".last_checked"
last_checked = os.path.getmtime(check_fname) if check_fname.exists() else None
Expand Down Expand Up @@ -133,7 +133,7 @@ def nbdev_clean(
_write = partial(process_write, warn_msg='Failed to clean notebook', proc_nb=_clean)
if stdin: return _write(f_in=sys.stdin, f_out=sys.stdout)

if fname is None: fname = config_key("nbs_path")
if fname is None: fname = get_config().path('nbs_path')
for f in globtastic(fname, file_glob='*.ipynb', skip_folder_re='^[_.]'): _write(f_in=f, disp=disp)

# %% ../nbs/11_clean.ipynb 30
Expand Down
14 changes: 7 additions & 7 deletions nbdev/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def nbdev_ghp_deploy():
except:
warnings.warn('Please install ghp-import with `pip install ghp-import`')
return
ghp_import(config_key('doc_path'), push=True, stderr=True, no_history=True)
ghp_import(get_config().path('doc_path'), push=True, stderr=True, no_history=True)

# %% ../nbs/10_cli.ipynb 7
_def_file_re = '\.(?:ipynb|qmd|html)$'
Expand All @@ -60,8 +60,8 @@ def nbdev_sidebar(
returnit:bool=False # Return list of files found
):
"Create sidebar.yml"
if not force and str2bool(config_key('custom_sidebar', path=False)): return
path = config_key("nbs_path") if not path else Path(path)
if not force and str2bool(get_config().custom_sidebar): return
path = get_config().path('nbs_path') if not path else Path(path)
files = nbglob(path, func=_f, symlinks=symlinks, file_re=file_re, folder_re=folder_re, file_glob=file_glob,
skip_file_glob=skip_file_glob, skip_file_re=skip_file_re, skip_folder_re=skip_folder_re).sorted(key=_sort)
lastd,res = Path(),[]
Expand Down Expand Up @@ -271,7 +271,7 @@ def refresh_quarto_yml():
p = cfg.path('nbs_path')/'_quarto.yml'
vals = {k:cfg.get(k) for k in ['doc_path', 'title', 'description', 'branch', 'git_url', 'doc_host', 'doc_baseurl']}
# Do not build _quarto_yml if custom_quarto_yml is set to True
if str2bool(config_key('custom_quarto_yml', default="False", path=False)): return
if str2bool(get_config().custom_quarto_yml): return
if 'title' not in vals: vals['title'] = vals['lib_name']
yml=_quarto_yml.format(**vals)
p.write_text(yml)
Expand Down Expand Up @@ -318,14 +318,14 @@ def _sprun(cmd):
def _doc_paths(path:str=None, doc_path:str=None):
cfg = get_config()
cfg_path = cfg.config_path
path = config_key("nbs_path") if not path else Path(path)
doc_path = config_key("doc_path") if not doc_path else Path(doc_path)
path = cfg.path('nbs_path') if not path else Path(path)
doc_path = cfg.path('doc_path') if not doc_path else Path(doc_path)
tmp_doc_path = path/f"{cfg['doc_path']}"
return cfg,cfg_path,path,doc_path,tmp_doc_path

# %% ../nbs/10_cli.ipynb 28
def _render_readme(path):
idx_path = path/config_key('readme_nb', path=False)
idx_path = path/get_config().readme_nb
if not idx_path.exists(): return

yml_path = path/'sidebar.yml'
Expand Down
10 changes: 5 additions & 5 deletions nbdev/doclinks.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,10 @@ def _doc_link(url, mod, sym=None):
# %% ../nbs/04b_doclinks.ipynb 27
def build_modidx():
"Create _modidx.py"
dest = config_key('lib_path')
dest = get_config().path('lib_path')
if os.environ.get('IN_TEST',0): return
_fn = dest/'_modidx.py'
nbs_path = config_key('nbs_path')
nbs_path = get_config().path('nbs_path')
with contextlib.suppress(FileNotFoundError): _fn.unlink()
cfg = get_config()
doc_func = partial(_doc_link, urljoin(cfg.doc_host,cfg.doc_baseurl))
Expand All @@ -122,9 +122,9 @@ def build_modidx():
@delegates(globtastic, but=['file_glob', 'skip_folder_re'])
def nbglob(path=None, skip_folder_re = '^[_.]', file_glob='*.ipynb', recursive=True, key='nbs_path',
as_path=False, **kwargs):
"Find all files in a directory matching an extension given a `config_key`."
path = Path(path or config_key(key))
if recursive is None: recursive=str2bool(config_key('recursive', path=False))
"Find all files in a directory matching an extension given a config key."
path = Path(path or get_config().path(key))
if recursive is None: recursive=str2bool(get_config().recursive)
res = globtastic(path, file_glob=file_glob, skip_folder_re=skip_folder_re, **kwargs)
return res.map(Path) if as_path else res

Expand Down
6 changes: 3 additions & 3 deletions nbdev/migrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# %% ../nbs/15_migrate.ipynb 2
from .process import first_code_ln
from .processors import nb_fmdict, construct_fm, insert_frontmatter, is_frontmatter, yml2dict, filter_fm
from .read import read_nb, config_key
from .read import get_config, read_nb
from .sync import write_nb
from .clean import process_write
from .showdoc import show_doc
Expand Down Expand Up @@ -75,7 +75,7 @@ def _subv1(s): return _alias.get(s, s)
def _re_v1():
d = ['default_exp', 'export', 'exports', 'exporti', 'hide', 'hide_input', 'collapse_show', 'collapse',
'collapse_hide', 'collapse_input', 'hide_output', 'default_cls_lvl']
d += L(config_key('tst_flags', path=False)).filter()
d += L(get_config().tst_flags).filter()
d += [s.replace('_', '-') for s in d] # allow for hyphenated version of old directives
_tmp = '|'.join(list(set(d)))
return re.compile(f"^[ \f\v\t]*?(#)\s*({_tmp})(?!\S)", re.MULTILINE)
Expand Down Expand Up @@ -123,5 +123,5 @@ def nbdev_migrate(
_write = partial(process_write, warn_msg='Failed to replace directives', proc_nb=_migrate)
if stdin: _write(f_in=sys.stdin, f_out=sys.stdout)
_skip_re = None if no_skip else '^[_.]'
if fname is None: fname = config_key("nbs_path")
if fname is None: fname = get_config().path('nbs_path')
for f in globtastic(fname, file_glob='*.ipynb', skip_folder_re=_skip_re): _write(f_in=f, disp=disp)
15 changes: 6 additions & 9 deletions nbdev/read.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def apply_defaults(
lib_name:str=None, # Package name, defaults to local repo folder name
branch='master', # Repo default branch
git_url='https://github.com/%(user)s/%(lib_name)s', # Repo URL
custom_sidebar:bool_arg=False, # Create custom sidebar?
custom_sidebar:bool_arg=False, # Use a custom sidebar.yml?
nbs_path='.', # Path to notebooks
lib_path='%(lib_name)s', # Path to package root
doc_path='_docs', # Path to rendered docs
Expand All @@ -63,6 +63,7 @@ def apply_defaults(
allowed_cell_metadata_keys='', # Preserve the list of keys in cell level metadata
jupyter_hooks=True, # Run Jupyter hooks?
clean_ids=True, # Remove ids from plaintext reprs?
custom_quarto_yml=False, # Use a custom _quarto.yml?
):
"Apply default settings where missing in `cfg`"
if lib_name is None:
Expand Down Expand Up @@ -114,13 +115,9 @@ def get_config(cfg_name=_nbdev_config_name, path=None):

# %% ../nbs/01_read.ipynb 21
def config_key(c, default=None, path=True, missing_ok=None):
"Look for key `c` in settings.ini and fail gracefully if not found and no default provided"
if missing_ok is not None:
warn("`missing_ok` is no longer used. Don't pass it to `config_key` to silence this warning.")
cfg = get_config()
res = cfg.path(c, default) if path else cfg.get(c, default)
if res is None: raise ValueError(f'`{c}` not specified in {_nbdev_config_name}')
return res
"Deprecated: use `get_config().get` or `get_config().path` instead."
warn("`config_key` is deprecated. Use `get_config().get` or `get_config().path` instead.", DeprecationWarning)
return get_config().path(c, default) if path else get_config().get(c, default)

# %% ../nbs/01_read.ipynb 24
_init = '__init__.py'
Expand All @@ -147,7 +144,7 @@ def write_cells(cells, hdr, file, offset=0):
# %% ../nbs/01_read.ipynb 29
def basic_export_nb(fname, name, dest=None):
"Basic exporter to bootstrap nbdev"
if dest is None: dest = config_key('lib_path')
if dest is None: dest = get_config().path('lib_path')
fname,dest = Path(fname),Path(dest)
nb = read_nb(fname)

Expand Down
6 changes: 3 additions & 3 deletions nbdev/release.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ def release_conda(
upload_user:str=None # Optional user to upload package to
):
"Create a `meta.yaml` file ready to be built into a package, and optionally build and upload it"
name = config_key('lib_name', path=False)
name = get_config().lib_name
write_conda_meta(path)
out = f"Done. Next steps:\n```\ncd {path}\n"""
os.chdir(path)
Expand All @@ -268,7 +268,7 @@ def release_conda(
print(f"conda {build} --no-anaconda-upload {build_args} {name}")
res = _run(f"conda {build} --no-anaconda-upload {build_args} {name}")
if skip_upload: return
if not upload_user: upload_user = config_key('conda_user', path=False)
if not upload_user: upload_user = get_config().conda_user
if not upload_user: return print("`conda_user` not in settings.ini and no `upload_user` passed. Cannot upload")
if 'anaconda upload' not in res: return print(f"{res}\n\Failed. Check auto-upload not set in .condarc. Try `--do_build False`.")
return anaconda_upload(name, loc)
Expand All @@ -293,7 +293,7 @@ def release_pypi(
repository:str="pypi" # Respository to upload to (defined in ~/.pypirc)
):
"Create and upload Python package to PyPI"
_dir = config_key("lib_path").parent
_dir = get_config().path('lib_path').parent
system(f'cd {_dir} && rm -rf dist && python setup.py sdist bdist_wheel')
system(f'twine upload --repository {repository} {_dir}/dist/*')

Expand Down
4 changes: 2 additions & 2 deletions nbdev/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,15 @@ def nbdev_test(
ignore_fname:str='.notest' # Filename that will result in siblings being ignored
):
"Test in parallel notebooks matching `fname`, passing along `flags`"
skip_flags = config_key('tst_flags', path=False).split()
skip_flags = get_config().tst_flags.split()
force_flags = flags.split()
files = nbglob(fname, recursive=recursive, file_re=file_re, folder_re=folder_re,
skip_file_glob=skip_file_glob, skip_file_re=skip_file_re, as_path=True, symlinks=symlinks)
files = [f.absolute() for f in sorted(files) if _keep_file(f, ignore_fname)]
if len(files)==0: return print('No files were eligible for testing')

if n_workers is None: n_workers = 0 if len(files)==1 else min(num_cpus(), 8)
os.chdir(config_key("nbs_path"))
os.chdir(get_config().path('nbs_path'))
if IN_NOTEBOOK: kwargs = {'method':'spawn'} if os.name=='nt' else {'method':'forkserver'}
else: kwargs = {}
results = parallel(test_nb, files, skip_flags=skip_flags, force_flags=force_flags, n_workers=n_workers,
Expand Down
24 changes: 11 additions & 13 deletions nbs/01_read.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@
" lib_name:str=None, # Package name, defaults to local repo folder name\n",
" branch='master', # Repo default branch\n",
" git_url='https://github.com/%(user)s/%(lib_name)s', # Repo URL\n",
" custom_sidebar:bool_arg=False, # Create custom sidebar?\n",
" custom_sidebar:bool_arg=False, # Use a custom sidebar.yml?\n",
" nbs_path='.', # Path to notebooks\n",
" lib_path='%(lib_name)s', # Path to package root\n",
" doc_path='_docs', # Path to rendered docs\n",
Expand All @@ -172,6 +172,7 @@
" allowed_cell_metadata_keys='', # Preserve the list of keys in cell level metadata\n",
" jupyter_hooks=True, # Run Jupyter hooks?\n",
" clean_ids=True, # Remove ids from plaintext reprs?\n",
" custom_quarto_yml=False, # Use a custom _quarto.yml?\n",
"):\n",
" \"Apply default settings where missing in `cfg`\"\n",
" if lib_name is None:\n",
Expand Down Expand Up @@ -302,7 +303,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"{'lib_name': 'T', 'branch': 'master', 'git_url': 'https://github.com/%(user)s/%(lib_name)s', 'custom_sidebar': 'False', 'nbs_path': '.', 'lib_path': '%(lib_name)s', 'doc_path': '_docs', 'tst_flags': '', 'version': '0.0.1', 'doc_host': 'https://%(user)s.github.io', 'doc_baseurl': '/%(lib_name)s', 'keywords': 'nbdev jupyter notebook python', 'license': 'apache2', 'copyright': 'None', 'status': '3', 'min_python': '3.7', 'audience': 'Developers', 'language': 'English', 'recursive': 'False', 'black_formatting': 'False', 'readme_nb': 'index.ipynb', 'title': '%(lib_name)s', 'allowed_metadata_keys': '', 'allowed_cell_metadata_keys': ''}\n"
"{'lib_name': 'T', 'branch': 'master', 'git_url': 'https://github.com/%(user)s/%(lib_name)s', 'custom_sidebar': 'False', 'nbs_path': '.', 'lib_path': '%(lib_name)s', 'doc_path': '_docs', 'tst_flags': '', 'version': '0.0.1', 'doc_host': 'https://%(user)s.github.io', 'doc_baseurl': '/%(lib_name)s', 'keywords': 'nbdev jupyter notebook python', 'license': 'apache2', 'copyright': 'None', 'status': '3', 'min_python': '3.7', 'audience': 'Developers', 'language': 'English', 'recursive': 'False', 'black_formatting': 'False', 'readme_nb': 'index.ipynb', 'title': '%(lib_name)s', 'allowed_metadata_keys': '', 'allowed_cell_metadata_keys': '', 'jupyter_hooks': 'True', 'clean_ids': 'True', 'custom_quarto_yml': 'False'}\n"
]
}
],
Expand All @@ -321,13 +322,9 @@
"source": [
"#|export\n",
"def config_key(c, default=None, path=True, missing_ok=None):\n",
" \"Look for key `c` in settings.ini and fail gracefully if not found and no default provided\"\n",
" if missing_ok is not None:\n",
" warn(\"`missing_ok` is no longer used. Don't pass it to `config_key` to silence this warning.\")\n",
" cfg = get_config()\n",
" res = cfg.path(c, default) if path else cfg.get(c, default)\n",
" if res is None: raise ValueError(f'`{c}` not specified in {_nbdev_config_name}')\n",
" return res"
" \"Deprecated: use `get_config().get` or `get_config().path` instead.\"\n",
" warn(\"`config_key` is deprecated. Use `get_config().get` or `get_config().path` instead.\", DeprecationWarning)\n",
" return get_config().path(c, default) if path else get_config().get(c, default)"
]
},
{
Expand All @@ -336,17 +333,18 @@
"metadata": {},
"outputs": [
{
"name": "stdout",
"name": "stderr",
"output_type": "stream",
"text": [
"T\n"
"/var/folders/ft/0gnvc3ts5jz4ddqtttp6tjvm0000gn/T/ipykernel_5473/2105843470.py:4: DeprecationWarning: `config_key` is deprecated. Use `get_config().get` or `get_config().path` instead.\n",
" warn(\"`config_key` is deprecated. Use `get_config().get` or `get_config().path` instead.\", DeprecationWarning)\n"
]
}
],
"source": [
"#|hide\n",
"# `config_key` returns defaults if no config file exists\n",
"with tempfile.TemporaryDirectory() as d, working_directory(d): print(config_key('lib_path', path=False))\n",
"with tempfile.TemporaryDirectory() as d, working_directory(d): assert config_key('lib_path', path=False) in ('T','tmp')\n",
"get_config.cache_clear()"
]
},
Expand Down Expand Up @@ -432,7 +430,7 @@
"#|export\n",
"def basic_export_nb(fname, name, dest=None):\n",
" \"Basic exporter to bootstrap nbdev\"\n",
" if dest is None: dest = config_key('lib_path')\n",
" if dest is None: dest = get_config().path('lib_path')\n",
" fname,dest = Path(fname),Path(dest)\n",
" nb = read_nb(fname)\n",
"\n",
Expand Down
10 changes: 5 additions & 5 deletions nbs/04b_doclinks.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -392,10 +392,10 @@
"#|export\n",
"def build_modidx():\n",
" \"Create _modidx.py\"\n",
" dest = config_key('lib_path')\n",
" dest = get_config().path('lib_path')\n",
" if os.environ.get('IN_TEST',0): return\n",
" _fn = dest/'_modidx.py'\n",
" nbs_path = config_key('nbs_path')\n",
" nbs_path = get_config().path('nbs_path')\n",
" with contextlib.suppress(FileNotFoundError): _fn.unlink()\n",
" cfg = get_config()\n",
" doc_func = partial(_doc_link, urljoin(cfg.doc_host,cfg.doc_baseurl))\n",
Expand All @@ -413,9 +413,9 @@
"@delegates(globtastic, but=['file_glob', 'skip_folder_re'])\n",
"def nbglob(path=None, skip_folder_re = '^[_.]', file_glob='*.ipynb', recursive=True, key='nbs_path',\n",
" as_path=False, **kwargs):\n",
" \"Find all files in a directory matching an extension given a `config_key`.\"\n",
" path = Path(path or config_key(key))\n",
" if recursive is None: recursive=str2bool(config_key('recursive', path=False))\n",
" \"Find all files in a directory matching an extension given a config key.\"\n",
" path = Path(path or get_config().path(key))\n",
" if recursive is None: recursive=str2bool(get_config().recursive)\n",
" res = globtastic(path, file_glob=file_glob, skip_folder_re=skip_folder_re, **kwargs)\n",
" return res.map(Path) if as_path else res"
]
Expand Down
14 changes: 7 additions & 7 deletions nbs/10_cli.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
" except:\n",
" warnings.warn('Please install ghp-import with `pip install ghp-import`')\n",
" return\n",
" ghp_import(config_key('doc_path'), push=True, stderr=True, no_history=True)"
" ghp_import(get_config().path('doc_path'), push=True, stderr=True, no_history=True)"
]
},
{
Expand Down Expand Up @@ -116,8 +116,8 @@
" returnit:bool=False # Return list of files found\n",
"):\n",
" \"Create sidebar.yml\"\n",
" if not force and str2bool(config_key('custom_sidebar', path=False)): return\n",
" path = config_key(\"nbs_path\") if not path else Path(path)\n",
" if not force and str2bool(get_config().custom_sidebar): return\n",
" path = get_config().path('nbs_path') if not path else Path(path)\n",
" files = nbglob(path, func=_f, symlinks=symlinks, file_re=file_re, folder_re=folder_re, file_glob=file_glob,\n",
" skip_file_glob=skip_file_glob, skip_file_re=skip_file_re, skip_folder_re=skip_folder_re).sorted(key=_sort)\n",
" lastd,res = Path(),[]\n",
Expand Down Expand Up @@ -470,7 +470,7 @@
" p = cfg.path('nbs_path')/'_quarto.yml'\n",
" vals = {k:cfg.get(k) for k in ['doc_path', 'title', 'description', 'branch', 'git_url', 'doc_host', 'doc_baseurl']}\n",
" # Do not build _quarto_yml if custom_quarto_yml is set to True\n",
" if str2bool(config_key('custom_quarto_yml', default=\"False\", path=False)): return\n",
" if str2bool(get_config().custom_quarto_yml): return\n",
" if 'title' not in vals: vals['title'] = vals['lib_name']\n",
" yml=_quarto_yml.format(**vals)\n",
" p.write_text(yml)"
Expand Down Expand Up @@ -549,8 +549,8 @@
"def _doc_paths(path:str=None, doc_path:str=None):\n",
" cfg = get_config()\n",
" cfg_path = cfg.config_path\n",
" path = config_key(\"nbs_path\") if not path else Path(path)\n",
" doc_path = config_key(\"doc_path\") if not doc_path else Path(doc_path)\n",
" path = cfg.path('nbs_path') if not path else Path(path)\n",
" doc_path = cfg.path('doc_path') if not doc_path else Path(doc_path)\n",
" tmp_doc_path = path/f\"{cfg['doc_path']}\"\n",
" return cfg,cfg_path,path,doc_path,tmp_doc_path"
]
Expand All @@ -564,7 +564,7 @@
"source": [
"#|export\n",
"def _render_readme(path):\n",
" idx_path = path/config_key('readme_nb', path=False)\n",
" idx_path = path/get_config().readme_nb\n",
" if not idx_path.exists(): return\n",
"\n",
" yml_path = path/'sidebar.yml'\n",
Expand Down
Loading

0 comments on commit be84ccc

Please sign in to comment.