Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make nbdev_create_config write the black_formatting setting #1235

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions nbdev/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ def _git_repo():
except OSError: return

# %% ../nbs/api/config.ipynb 11
# When adding a named default to the list below, be sure that that name
# is also added to one of the sections in `_nbdev_cfg_sections` as well,
# or it won't get written by `nbdev_create_config`:
def _apply_defaults(
cfg,
lib_name='%(repo)s', # Package name
Expand Down Expand Up @@ -141,7 +144,7 @@ def _cfg2txt(cfg, head, sections, tail=''):
# See https://github.com/fastai/nbdev/blob/master/settings.ini for examples.

'''
_nbdev_cfg_sections = {'Python library': 'repo lib_name version min_python license',
_nbdev_cfg_sections = {'Python library': 'repo lib_name version min_python license black_formatting',
'nbdev': 'doc_path lib_path nbs_path recursive tst_flags put_version_in_init',
'Docs': 'branch custom_sidebar doc_host doc_baseurl git_url title',
'PyPI': 'audience author author_email copyright description keywords language status user'}
Expand Down Expand Up @@ -207,16 +210,16 @@ def config_key(c, default=None, path=True, missing_ok=None):
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/api/config.ipynb 44
# %% ../nbs/api/config.ipynb 46
def create_output(txt, mime):
"Add a cell output containing `txt` of the `mime` text MIME sub-type"
return [{"data": { f"text/{mime}": str(txt).splitlines(True) },
"execution_count": 1, "metadata": {}, "output_type": "execute_result"}]

# %% ../nbs/api/config.ipynb 45
# %% ../nbs/api/config.ipynb 47
def show_src(src, lang='python'): return Markdown(f'```{lang}\n{src}\n```')

# %% ../nbs/api/config.ipynb 48
# %% ../nbs/api/config.ipynb 50
_re_version = re.compile('^__version__\s*=.*$', re.MULTILINE)
_init = '__init__.py'

Expand Down Expand Up @@ -245,13 +248,13 @@ def add_init(path=None):
if _has_py(fs) or any(filter(_has_py, subds)) and not (r/_init).exists(): (r/_init).touch()
if get_config().get('put_version_in_init', True): update_version(path)

# %% ../nbs/api/config.ipynb 51
# %% ../nbs/api/config.ipynb 53
def write_cells(cells, hdr, file, offset=0):
"Write `cells` to `file` along with header `hdr` starting at index `offset` (mainly for nbdev internal use)."
for cell in cells:
if cell.source.strip(): file.write(f'\n\n{hdr} {cell.idx_+offset}\n{cell.source}')

# %% ../nbs/api/config.ipynb 52
# %% ../nbs/api/config.ipynb 54
def _basic_export_nb(fname, name, dest=None):
"Basic exporter to bootstrap nbdev."
if dest is None: dest = get_config().lib_path
Expand Down
39 changes: 38 additions & 1 deletion nbs/api/config.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@
"outputs": [],
"source": [
"#|export\n",
"# When adding a named default to the list below, be sure that that name\n",
"# is also added to one of the sections in `_nbdev_cfg_sections` as well,\n",
"# or it won't get written by `nbdev_create_config`:\n",
"def _apply_defaults(\n",
" cfg,\n",
" lib_name='%(repo)s', # Package name\n",
Expand Down Expand Up @@ -330,7 +333,7 @@
"# See https://github.com/fastai/nbdev/blob/master/settings.ini for examples.\n",
"\n",
"'''\n",
"_nbdev_cfg_sections = {'Python library': 'repo lib_name version min_python license',\n",
"_nbdev_cfg_sections = {'Python library': 'repo lib_name version min_python license black_formatting',\n",
" 'nbdev': 'doc_path lib_path nbs_path recursive tst_flags put_version_in_init',\n",
" 'Docs': 'branch custom_sidebar doc_host doc_baseurl git_url title',\n",
" 'PyPI': 'audience author author_email copyright description keywords language status user'}\n",
Expand Down Expand Up @@ -593,6 +596,40 @@
" return get_config().path(c, default) if path else get_config().get(c, default)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Make sure that the `black_formatting` parameter is written:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"test_settings.ini created.\n"
]
}
],
"source": [
"with tempfile.TemporaryDirectory() as d, working_directory(d):\n",
" nbdev_create_config(repo='my-project', cfg_name='test_settings.ini',\n",
" branch='master', user='fastai', author='fastai',\n",
" author_email='fastai@example.com', description='test black_formatting',\n",
" black_formatting=True)\n",
"\n",
" cfg = get_config('test_settings.ini', '.')\n",
" test_eq(cfg.repo, 'my-project')\n",
" test_eq(cfg.lib_path.name, 'my_project')\n",
" test_eq(cfg.copyright, '2022 onwards, fastai')\n",
" test_eq(cfg.black_formatting, True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down