diff options
| author | Eugene Triguba <hello@eugenetriguba.com> | 2026-05-23 16:09:39 +0000 |
|---|---|---|
| committer | Eugene Triguba <hello@eugenetriguba.com> | 2026-05-23 16:09:39 +0000 |
| commit | 9ad4c009c6f8ce223be6d6a983462390a60af064 (patch) | |
| tree | 8b681c41d4890fcc2013ab326e785dfbf1a447a9 | |
| parent | Support classmethod, staticmethod, and properties with patch_to/patch (diff) | |
| download | nb-9ad4c009c6f8ce223be6d6a983462390a60af064.zip | |
Add config module to read pyproject.toml tool.nb config
| -rw-r--r-- | nb/config.py | 48 | ||||
| -rw-r--r-- | nbs/config.ipynb | 285 |
2 files changed, 333 insertions, 0 deletions
diff --git a/nb/config.py b/nb/config.py new file mode 100644 index 0000000..808f446 --- /dev/null +++ b/nb/config.py @@ -0,0 +1,48 @@ +# AUTOGENERATED! DO NOT EDIT! + +# cell: 5006bc03 +import dataclasses +import os +import tomllib +from pathlib import Path + +from nb.core import patch + +# cell: c282bbbc +def _config_path(curdir:str|Path='.')->Path|None: + "Find the pyproject.toml configuration file by traversing dirs until the root of the file system." + path = os.path.abspath(curdir) + while path != os.path.dirname(path): + if 'pyproject.toml' in os.listdir(path): + return Path(path + '/pyproject.toml') + path = os.path.dirname(path) + +# cell: 0948469f +@dataclasses.dataclass +class Config: + nb_dir:str|None + export_dir:str|None + +# cell: 311895a2 +_CONFIG_DEFAULTS = {'nb_dir': 'nbs', 'export_dir': '.'} + +# cell: 6b811084 +@patch +def default(cls:Config)->Config: + return cls(**_CONFIG_DEFAULTS) + +# cell: d95dafba +@patch +def from_dict(cls:Config, d:dict)->Config: + d = _CONFIG_DEFAULTS | d + fields = {f.name for f in dataclasses.fields(cls)} + extra = d.keys() - fields + if extra: raise ValueError(f"Unknown config keys: {extra}") + return cls(**d) + +# cell: 00ceaa67 +def get_config(curdir:str|Path='.')->Config: + p = _config_path(curdir) + if not p: return Config.default() + nb = tomllib.loads(p.read_text()).get('tool', {}).get('nb', {}) + return Config.from_dict(nb) diff --git a/nbs/config.ipynb b/nbs/config.ipynb new file mode 100644 index 0000000..34f8f9f --- /dev/null +++ b/nbs/config.ipynb @@ -0,0 +1,285 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "f0b47680", + "metadata": {}, + "outputs": [], + "source": [ + "%%capture\n", + "%load_ext autoreload\n", + "%autoreload 2" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "989dbd8c", + "metadata": {}, + "outputs": [], + "source": [ + "#| default_exp: config" + ] + }, + { + "cell_type": "markdown", + "id": "983c3e1a", + "metadata": {}, + "source": [ + "# Config\n", + "\n", + "> Reading configuration for `nb`" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5006bc03", + "metadata": {}, + "outputs": [], + "source": [ + "#| export\n", + "import dataclasses\n", + "import os\n", + "import tomllib\n", + "from pathlib import Path\n", + "\n", + "from nb.core import patch" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "645d4349", + "metadata": {}, + "outputs": [], + "source": [ + "from tempfile import TemporaryDirectory\n", + "from nb import test" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c282bbbc", + "metadata": {}, + "outputs": [], + "source": [ + "#| export\n", + "def _config_path(curdir:str|Path='.')->Path|None:\n", + " \"Find the pyproject.toml configuration file by traversing dirs until the root of the file system.\"\n", + " path = os.path.abspath(curdir)\n", + " while path != os.path.dirname(path):\n", + " if 'pyproject.toml' in os.listdir(path):\n", + " return Path(path + '/pyproject.toml')\n", + " path = os.path.dirname(path)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6c5c3460", + "metadata": {}, + "outputs": [], + "source": [ + "with TemporaryDirectory() as tmp:\n", + " test.equal(_config_path(tmp), None)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7363f5ed", + "metadata": {}, + "outputs": [], + "source": [ + "with TemporaryDirectory() as tmp:\n", + " p = Path(tmp)/'pyproject.toml'\n", + " p.touch()\n", + " test.equal(_config_path(tmp), p)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "0948469f", + "metadata": {}, + "outputs": [], + "source": [ + "#| export\n", + "@dataclasses.dataclass\n", + "class Config:\n", + " nb_dir:str|None\n", + " export_dir:str|None" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "311895a2", + "metadata": {}, + "outputs": [], + "source": [ + "#| export\n", + "_CONFIG_DEFAULTS = {'nb_dir': 'nbs', 'export_dir': '.'}" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6b811084", + "metadata": {}, + "outputs": [], + "source": [ + "#| export\n", + "@patch\n", + "def default(cls:Config)->Config:\n", + " return cls(**_CONFIG_DEFAULTS)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d95dafba", + "metadata": {}, + "outputs": [], + "source": [ + "#| export\n", + "@patch\n", + "def from_dict(cls:Config, d:dict)->Config:\n", + " d = _CONFIG_DEFAULTS | d\n", + " fields = {f.name for f in dataclasses.fields(cls)}\n", + " extra = d.keys() - fields\n", + " if extra: raise ValueError(f\"Unknown config keys: {extra}\")\n", + " return cls(**d)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2217ca78", + "metadata": {}, + "outputs": [], + "source": [ + "with test.raises(exc=ValueError, contains=\"Unknown config keys: {'abc'}\"):\n", + " Config.from_dict({'abc': 123})" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ed87dda5", + "metadata": {}, + "outputs": [], + "source": [ + "test.equal(Config.from_dict({}), Config.default())" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7b2800d5", + "metadata": {}, + "outputs": [], + "source": [ + "_d = {'nb_dir': 'abc'}\n", + "test.equal(Config.from_dict(_d), Config(**(_CONFIG_DEFAULTS | _d)))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "00ceaa67", + "metadata": {}, + "outputs": [], + "source": [ + "#| export\n", + "def get_config(curdir:str|Path='.')->Config:\n", + " p = _config_path(curdir)\n", + " if not p: return Config.default()\n", + " nb = tomllib.loads(p.read_text()).get('tool', {}).get('nb', {})\n", + " return Config.from_dict(nb)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "bee6bc66", + "metadata": {}, + "outputs": [], + "source": [ + "with TemporaryDirectory() as tmp:\n", + " test.equal(get_config(tmp), Config.default())" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f0084313", + "metadata": {}, + "outputs": [], + "source": [ + "with TemporaryDirectory() as tmp:\n", + " p = Path(tmp)/'pyproject.toml'\n", + " p.touch()\n", + " test.equal(get_config(tmp), Config.default())" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "502bec22", + "metadata": {}, + "outputs": [], + "source": [ + "with TemporaryDirectory() as tmp:\n", + " p = Path(tmp)/'pyproject.toml'\n", + " p.write_text('[tool.nb]')\n", + " test.equal(get_config(tmp), Config.default())" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2a47c918", + "metadata": {}, + "outputs": [], + "source": [ + "with (\n", + " test.raises(exc=ValueError, contains=\"Unknown config keys: {'dir'}\"),\n", + " TemporaryDirectory() as tmp\n", + "):\n", + " p = Path(tmp)/'pyproject.toml'\n", + " p.write_text('[tool.nb]\\ndir=1')\n", + " get_config(tmp)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "552b4e70", + "metadata": {}, + "outputs": [], + "source": [ + "with TemporaryDirectory() as tmp:\n", + " p = Path(tmp)/'pyproject.toml'\n", + " p.write_text('[tool.nb]\\nnb_dir=\"abc/test\"')\n", + " test.equal(get_config(tmp), Config.from_dict({'nb_dir': 'abc/test'}))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7d6ee6fa", + "metadata": {}, + "outputs": [], + "source": [ + "import nb; nb.export_nb('config.ipynb', src_dir='../nb')" + ] + } + ], + "metadata": {}, + "nbformat": 4, + "nbformat_minor": 5 +} |