diff options
Diffstat (limited to 'nbs/cli.ipynb')
| -rw-r--r-- | nbs/cli.ipynb | 217 |
1 files changed, 180 insertions, 37 deletions
diff --git a/nbs/cli.ipynb b/nbs/cli.ipynb index 58878eb..59f794e 100644 --- a/nbs/cli.ipynb +++ b/nbs/cli.ipynb @@ -40,13 +40,14 @@ "outputs": [], "source": [ "#| export\n", - "import argparse,json,sys\n", + "import argparse,contextlib,json,sys\n", "from pathlib import Path\n", "\n", "import nbformat\n", "\n", "from nb import export_nb,clean_nb\n", - "from nb.export import read_nb" + "from nb.export import read_nb\n", + "from nb.config import get_config,Config" ] }, { @@ -57,6 +58,7 @@ "outputs": [], "source": [ "import tempfile\n", + "import uuid\n", "from nb import test" ] }, @@ -69,17 +71,35 @@ "source": [ "#| export\n", "def main():\n", + " cfg = get_config()\n", " p = _get_parser()\n", " args = p.parse_args()\n", " if not args.cmd:\n", " p.print_help()\n", " raise SystemExit(1)\n", " if args.cmd == 'export':\n", - " for nb in args.notebooks:\n", - " export_nb(nb, args.export_dir)\n", + " nbs = _get_nbs(args.notebooks, cfg)\n", + " exp_dir = args.export_dir or cfg.export_dir or '.'\n", + " if args.clean:\n", + " for nb in nbs: nbformat.write(clean_nb(nb), nb)\n", + " for nb in nbs: export_nb(nb, exp_dir)\n", " if args.cmd == 'clean':\n", - " for nb in args.notebooks:\n", - " nbformat.write(clean_nb(nb), nb)" + " nbs = _get_nbs(args.notebooks, cfg)\n", + " for nb in nbs: nbformat.write(clean_nb(nb), nb)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "09b8407e", + "metadata": {}, + "outputs": [], + "source": [ + "#| export\n", + "def _get_nbs(nbs:list[str], cfg:Config):\n", + " if nbs: return nbs\n", + " if cfg.nb_dir: return list(Path(cfg.nb_dir).glob('*.ipynb'))\n", + " raise SystemExit('No notebooks specified and nb_dir not set in config')" ] }, { @@ -94,16 +114,91 @@ " parser = argparse.ArgumentParser(prog='nb')\n", " subparsers = parser.add_subparsers(dest='cmd')\n", " export_parser = subparsers.add_parser('export', help='Export ipynb notebooks to Python')\n", - " export_parser.add_argument('notebooks', nargs='+', help='Notebook files to export')\n", - " export_parser.add_argument('--export-dir', default='.', help='Directory to export Python files to')\n", + " export_parser.add_argument('notebooks', nargs='*', help='Notebook files to export')\n", + " export_parser.add_argument('--export-dir', default=None, help='Directory to export Python files to')\n", + " export_parser.add_argument('--clean', action='store_true', help='Clean the notebooks before exporting')\n", " clean_parser = subparsers.add_parser('clean', help='Clean notebooks')\n", - " clean_parser.add_argument('notebooks', nargs='+', help='Notebook files to clean')\n", + " clean_parser.add_argument('notebooks', nargs='*', help='Notebook files to clean')\n", " return parser" ] }, { "cell_type": "code", "execution_count": null, + "id": "ce775b53", + "metadata": {}, + "outputs": [], + "source": [ + "def mk_nb(nb_path, **nb_kwargs):\n", + " nb = nbformat.v4.new_notebook(**nb_kwargs)\n", + " with open(nb_path, mode='w+') as f: nbformat.write(nb, f)\n", + " return nb" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "19889cd7", + "metadata": {}, + "outputs": [], + "source": [ + "def mk_nb_cfg(cfg_dir:str|Path, nb_dir:str|Path='', export_dir:str|Path=''):\n", + " pyproj = Path(cfg_dir)/'pyproject.toml'\n", + " lines = ['[tool.nb]']\n", + " if nb_dir: lines.append(f'nb_dir=\"{str(nb_dir)}\"')\n", + " if export_dir: lines.append(f'export_dir=\"{str(export_dir)}\"')\n", + " pyproj.write_text('\\n'.join(lines))\n", + " return pyproj" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4f17e8d6", + "metadata": {}, + "outputs": [], + "source": [ + "def run_cli(*args):\n", + " sys.argv = ['nb', *args]\n", + " main()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c93ffa7c", + "metadata": {}, + "outputs": [], + "source": [ + "@contextlib.contextmanager\n", + "def tmp_nb(cells, nbkwargs={}):\n", + " with tempfile.TemporaryDirectory() as tmp:\n", + " nb_path = tmp + f'/tmp-{uuid.uuid4().hex}.ipynb'\n", + " nb = mk_nb(nb_path, cells=cells, **nbkwargs)\n", + " with contextlib.chdir(tmp): yield (tmp, nb_path)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "25e8195c", + "metadata": {}, + "outputs": [], + "source": [ + "def mk_nb_src(cells):\n", + " cells = cells if isinstance(cells, list) else [cells]\n", + " lines = ['# AUTOGENERATED! DO NOT EDIT!\\n']\n", + " for cell in cells:\n", + " if cell['source'].startswith('#| export'):\n", + " lines.append(f'# cell: {cell[\"id\"]}')\n", + " lines.extend([l for l in cell['source'].splitlines() if not l.startswith('#|')])\n", + " lines.append('')\n", + " return '\\n'.join(lines)" + ] + }, + { + "cell_type": "code", + "execution_count": null, "id": "7b95c2d4", "metadata": {}, "outputs": [ @@ -124,31 +219,73 @@ } ], "source": [ - "sys.argv = ['script']\n", - "with test.raises(exc=SystemExit, contains='1'): main()" + "with test.raises(exc=SystemExit, contains='1'): run_cli()" ] }, { "cell_type": "code", "execution_count": null, - "id": "5da41c17", + "id": "2a23b5af", "metadata": {}, "outputs": [], "source": [ - "with tempfile.TemporaryDirectory() as tmp:\n", - " nb = nbformat.v4.new_notebook()\n", - " nb.cells = [\n", - " nbformat.v4.new_code_cell('#| default_exp: mymod'),\n", - " nbformat.v4.new_code_cell('#| export\\ndef hello(): return \"hi\"'),\n", - " ]\n", - " nb_path = tmp + '/tmp.ipynb'\n", - " with open(nb_path, mode='w+') as f: nbformat.write(nb, f)\n", - " sys.argv = ['script', 'export', '--export-dir', tmp, nb_path]\n", - " main()\n", - " test.equal(\n", - " Path(tmp + '/mymod.py').read_text(),\n", - " f'# AUTOGENERATED! DO NOT EDIT!\\n\\n# cell: {nb.cells[1]['id']}\\ndef hello(): return \"hi\"\\n',\n", - " )" + "CLEAN_VALID_EXPORTED_CELLS = [\n", + " nbformat.v4.new_code_cell(f'#| default_exp: tmp'),\n", + " nbformat.v4.new_code_cell(f'#| export\\ndef hello(): return \"hi\"'),\n", + " nbformat.v4.new_code_cell('def goodbye(): return \"goodbye\"'),\n", + "]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "fc918ca3", + "metadata": {}, + "outputs": [], + "source": [ + "with tmp_nb(CLEAN_VALID_EXPORTED_CELLS) as (tmp, nb_path):\n", + " run_cli('export', '--export-dir', tmp, nb_path)\n", + " test.equal((Path(tmp)/'tmp.py').read_text(), mk_nb_src(CLEAN_VALID_EXPORTED_CELLS))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "422d92ad", + "metadata": {}, + "outputs": [], + "source": [ + "with tmp_nb(CLEAN_VALID_EXPORTED_CELLS) as (tmp, nb_path):\n", + " mk_nb_cfg(tmp, nb_dir=tmp, export_dir=tmp)\n", + " run_cli('export')\n", + " test.equal((Path(tmp)/'tmp.py').read_text(), mk_nb_src(CLEAN_VALID_EXPORTED_CELLS))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e6bbc5ad", + "metadata": {}, + "outputs": [], + "source": [ + "DIRTY_VALID_EXPORTED_CELLS = [\n", + " nbformat.v4.new_code_cell('#| default_exp: tmp'),\n", + " nbformat.v4.new_code_cell('#| export\\nprint(\"hi\")', execution_count=42),\n", + "]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7fedfaff", + "metadata": {}, + "outputs": [], + "source": [ + "with tmp_nb(DIRTY_VALID_EXPORTED_CELLS) as (tmp, nb_path):\n", + " mk_nb_cfg(tmp, nb_dir=tmp, export_dir=tmp)\n", + " run_cli('export', '--clean')\n", + " test.equal((Path(tmp)/'tmp.py').read_text(), mk_nb_src(DIRTY_VALID_EXPORTED_CELLS))\n", + " test.equal(cleaned.cells[1].execution_count, None)" ] }, { @@ -158,17 +295,23 @@ "metadata": {}, "outputs": [], "source": [ - "with tempfile.TemporaryDirectory() as tmp:\n", - " nb = nbformat.v4.new_notebook()\n", - " nb.metadata['bogus'] = 'should be removed'\n", - " nb.cells = [\n", - " nbformat.v4.new_code_cell('#| default_exp: mymod'),\n", - " nbformat.v4.new_code_cell('print(\"hi\")', execution_count=42),\n", - " ]\n", - " nb_path = tmp + '/tmp.ipynb'\n", - " with open(nb_path, mode='w+') as f: nbformat.write(nb, f)\n", - " sys.argv = ['script', 'clean', nb_path]\n", - " main()\n", + "with tmp_nb(DIRTY_VALID_EXPORTED_CELLS, nbkwargs={'metadata': {'bogus': 'should be removed'}}) as (tmp, nb_path):\n", + " run_cli('clean', nb_path)\n", + " cleaned = read_nb(nb_path)\n", + " test.equal(cleaned.cells[1].execution_count, None)\n", + " test.equal('bogus' not in cleaned.metadata, True)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "61f3911b", + "metadata": {}, + "outputs": [], + "source": [ + "with tmp_nb(DIRTY_VALID_EXPORTED_CELLS, nbkwargs={'metadata': {'bogus': 'should be removed'}}) as (tmp, nb_path):\n", + " mk_nb_cfg(tmp, nb_dir=tmp, export_dir=tmp)\n", + " run_cli('clean')\n", " cleaned = read_nb(nb_path)\n", " test.equal(cleaned.cells[1].execution_count, None)\n", " test.equal('bogus' not in cleaned.metadata, True)" |