diff options
| -rw-r--r-- | nb/cli.py | 28 | ||||
| -rw-r--r-- | nb/config.py | 13 | ||||
| -rw-r--r-- | nbs/cli.ipynb | 217 | ||||
| -rw-r--r-- | nbs/config.ipynb | 34 |
4 files changed, 238 insertions, 54 deletions
@@ -1,35 +1,47 @@ # AUTOGENERATED! DO NOT EDIT! # cell: a62b4fb4 -import argparse,json,sys +import argparse,contextlib,json,sys from pathlib import Path import nbformat from nb import export_nb,clean_nb from nb.export import read_nb +from nb.config import get_config,Config # cell: 96737c5a def main(): + cfg = get_config() p = _get_parser() args = p.parse_args() if not args.cmd: p.print_help() raise SystemExit(1) if args.cmd == 'export': - for nb in args.notebooks: - export_nb(nb, args.export_dir) + nbs = _get_nbs(args.notebooks, cfg) + exp_dir = args.export_dir or cfg.export_dir or '.' + if args.clean: + for nb in nbs: nbformat.write(clean_nb(nb), nb) + for nb in nbs: export_nb(nb, exp_dir) if args.cmd == 'clean': - for nb in args.notebooks: - nbformat.write(clean_nb(nb), nb) + nbs = _get_nbs(args.notebooks, cfg) + for nb in nbs: nbformat.write(clean_nb(nb), nb) + +# cell: 09b8407e +def _get_nbs(nbs:list[str], cfg:Config): + if nbs: return nbs + if cfg.nb_dir: return list(Path(cfg.nb_dir).glob('*.ipynb')) + raise SystemExit('No notebooks specified and nb_dir not set in config') # cell: 69b642d9 def _get_parser(): parser = argparse.ArgumentParser(prog='nb') subparsers = parser.add_subparsers(dest='cmd') export_parser = subparsers.add_parser('export', help='Export ipynb notebooks to Python') - export_parser.add_argument('notebooks', nargs='+', help='Notebook files to export') - export_parser.add_argument('--export-dir', default='.', help='Directory to export Python files to') + export_parser.add_argument('notebooks', nargs='*', help='Notebook files to export') + export_parser.add_argument('--export-dir', default=None, help='Directory to export Python files to') + export_parser.add_argument('--clean', action='store_true', help='Clean the notebooks before exporting') clean_parser = subparsers.add_parser('clean', help='Clean notebooks') - clean_parser.add_argument('notebooks', nargs='+', help='Notebook files to clean') + clean_parser.add_argument('notebooks', nargs='*', help='Notebook files to clean') return parser diff --git a/nb/config.py b/nb/config.py index 151d062..9d03fe8 100644 --- a/nb/config.py +++ b/nb/config.py @@ -24,8 +24,8 @@ def config_dir(curdir:str|Path='.')->Path|None: # cell: 0948469f @dataclasses.dataclass class Config: - nb_dir:str|None - export_dir:str|None + nb_dir:str|Path|None + export_dir:str|Path|None # cell: 311895a2 def config_defaults(base_dir:Path)->dict: @@ -36,6 +36,11 @@ def config_defaults(base_dir:Path)->dict: def default(cls:Config, base_dir:Path)->Config: return cls(**config_defaults(base_dir)) +# cell: 667948b3 +@patch +def empty(cls:Config)->Config: + return cls(**{f.name:None for f in dataclasses.fields(cls)}) + # cell: d95dafba @patch def from_dict(cls:Config, d:dict, base_dir:Path)->Config: @@ -46,8 +51,8 @@ def from_dict(cls:Config, d:dict, base_dir:Path)->Config: return cls(**d) # cell: 00ceaa67 -def get_config(curdir:str|Path='.')->Config|None: +def get_config(curdir:str|Path='.')->Config: p = config_path(curdir) - if not p: return None + if not p: return Config.empty() nb = tomllib.loads(p.read_text()).get('tool', {}).get('nb', {}) return Config.from_dict(nb, base_dir=p.parent) 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)" diff --git a/nbs/config.ipynb b/nbs/config.ipynb index 5897a37..9b0be34 100644 --- a/nbs/config.ipynb +++ b/nbs/config.ipynb @@ -135,8 +135,8 @@ "#| export\n", "@dataclasses.dataclass\n", "class Config:\n", - " nb_dir:str|None\n", - " export_dir:str|None" + " nb_dir:str|Path|None\n", + " export_dir:str|Path|None" ] }, { @@ -178,6 +178,30 @@ { "cell_type": "code", "execution_count": null, + "id": "667948b3", + "metadata": {}, + "outputs": [], + "source": [ + "#| export\n", + "@patch\n", + "def empty(cls:Config)->Config:\n", + " return cls(**{f.name:None for f in dataclasses.fields(cls)})" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "97fc77c3", + "metadata": {}, + "outputs": [], + "source": [ + "empty = Config.empty()\n", + "test.equal(empty, Config(nb_dir=None, export_dir=None))" + ] + }, + { + "cell_type": "code", + "execution_count": null, "id": "d95dafba", "metadata": {}, "outputs": [], @@ -232,9 +256,9 @@ "outputs": [], "source": [ "#| export\n", - "def get_config(curdir:str|Path='.')->Config|None:\n", + "def get_config(curdir:str|Path='.')->Config:\n", " p = config_path(curdir)\n", - " if not p: return None\n", + " if not p: return Config.empty()\n", " nb = tomllib.loads(p.read_text()).get('tool', {}).get('nb', {})\n", " return Config.from_dict(nb, base_dir=p.parent)" ] @@ -247,7 +271,7 @@ "outputs": [], "source": [ "with TemporaryDirectory() as tmp:\n", - " test.equal(get_config(tmp), None)" + " test.equal(get_config(tmp), Config.empty())" ] }, { |