diff options
| author | Eugene Triguba <hello@eugenetriguba.com> | 2026-05-23 16:37:57 +0000 |
|---|---|---|
| committer | Eugene Triguba <hello@eugenetriguba.com> | 2026-05-23 16:37:57 +0000 |
| commit | 0f3ee8dbfb3affadc83ec7bd3273019c6f48812b (patch) | |
| tree | c14989b9eaf2629b62b468f6d1e7bacfc5a13e0a | |
| parent | Add config module to read pyproject.toml tool.nb config (diff) | |
| download | nb-0f3ee8dbfb3affadc83ec7bd3273019c6f48812b.zip | |
Add pyproject.toml dir relative config paths
| -rw-r--r-- | nb/config.py | 25 | ||||
| -rw-r--r-- | nbs/config.ipynb | 75 |
2 files changed, 71 insertions, 29 deletions
diff --git a/nb/config.py b/nb/config.py index 808f446..151d062 100644 --- a/nb/config.py +++ b/nb/config.py @@ -9,7 +9,7 @@ from pathlib import Path from nb.core import patch # cell: c282bbbc -def _config_path(curdir:str|Path='.')->Path|None: +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): @@ -17,6 +17,10 @@ def _config_path(curdir:str|Path='.')->Path|None: return Path(path + '/pyproject.toml') path = os.path.dirname(path) +# cell: 89f6f072 +def config_dir(curdir:str|Path='.')->Path|None: + return p.parent if config_path(curdir) is not None else None + # cell: 0948469f @dataclasses.dataclass class Config: @@ -24,25 +28,26 @@ class Config: export_dir:str|None # cell: 311895a2 -_CONFIG_DEFAULTS = {'nb_dir': 'nbs', 'export_dir': '.'} +def config_defaults(base_dir:Path)->dict: + return {'nb_dir': base_dir/Path('nbs'), 'export_dir': base_dir/Path('.')} # cell: 6b811084 @patch -def default(cls:Config)->Config: - return cls(**_CONFIG_DEFAULTS) +def default(cls:Config, base_dir:Path)->Config: + return cls(**config_defaults(base_dir)) # cell: d95dafba @patch -def from_dict(cls:Config, d:dict)->Config: - d = _CONFIG_DEFAULTS | d +def from_dict(cls:Config, d:dict, base_dir:Path)->Config: + d = config_defaults(base_dir) | 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() +def get_config(curdir:str|Path='.')->Config|None: + p = config_path(curdir) + if not p: return None nb = tomllib.loads(p.read_text()).get('tool', {}).get('nb', {}) - return Config.from_dict(nb) + return Config.from_dict(nb, base_dir=p.parent) diff --git a/nbs/config.ipynb b/nbs/config.ipynb index 34f8f9f..5897a37 100644 --- a/nbs/config.ipynb +++ b/nbs/config.ipynb @@ -67,7 +67,7 @@ "outputs": [], "source": [ "#| export\n", - "def _config_path(curdir:str|Path='.')->Path|None:\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", @@ -84,7 +84,7 @@ "outputs": [], "source": [ "with TemporaryDirectory() as tmp:\n", - " test.equal(_config_path(tmp), None)" + " test.equal(config_path(tmp), None)" ] }, { @@ -97,7 +97,32 @@ "with TemporaryDirectory() as tmp:\n", " p = Path(tmp)/'pyproject.toml'\n", " p.touch()\n", - " test.equal(_config_path(tmp), p)" + " test.equal(config_path(tmp), p)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "89f6f072", + "metadata": {}, + "outputs": [], + "source": [ + "#| export\n", + "def config_dir(curdir:str|Path='.')->Path|None:\n", + " return p.parent if config_path(curdir) is not None else None" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "73f36540", + "metadata": {}, + "outputs": [], + "source": [ + "with TemporaryDirectory() as tmp:\n", + " p = Path(tmp)/'pyproject.toml'\n", + " p.touch()\n", + " test.equal(config_dir(tmp), Path(tmp))" ] }, { @@ -122,7 +147,19 @@ "outputs": [], "source": [ "#| export\n", - "_CONFIG_DEFAULTS = {'nb_dir': 'nbs', 'export_dir': '.'}" + "def config_defaults(base_dir:Path)->dict:\n", + " return {'nb_dir': base_dir/Path('nbs'), 'export_dir': base_dir/Path('.')}" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b2c35e58", + "metadata": {}, + "outputs": [], + "source": [ + "with TemporaryDirectory() as tmp:\n", + " test.equal(config_defaults(Path(tmp)), {'nb_dir': Path(tmp)/Path('nbs'), 'export_dir': Path(tmp)})" ] }, { @@ -134,8 +171,8 @@ "source": [ "#| export\n", "@patch\n", - "def default(cls:Config)->Config:\n", - " return cls(**_CONFIG_DEFAULTS)" + "def default(cls:Config, base_dir:Path)->Config:\n", + " return cls(**config_defaults(base_dir))" ] }, { @@ -147,8 +184,8 @@ "source": [ "#| export\n", "@patch\n", - "def from_dict(cls:Config, d:dict)->Config:\n", - " d = _CONFIG_DEFAULTS | d\n", + "def from_dict(cls:Config, d:dict, base_dir:Path)->Config:\n", + " d = config_defaults(base_dir) | 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", @@ -163,7 +200,7 @@ "outputs": [], "source": [ "with test.raises(exc=ValueError, contains=\"Unknown config keys: {'abc'}\"):\n", - " Config.from_dict({'abc': 123})" + " Config.from_dict({'abc': 123}, base_dir=Path('.'))" ] }, { @@ -173,7 +210,7 @@ "metadata": {}, "outputs": [], "source": [ - "test.equal(Config.from_dict({}), Config.default())" + "test.equal(Config.from_dict({}, base_dir=Path('.')), Config.default(base_dir=Path('.')))" ] }, { @@ -184,7 +221,7 @@ "outputs": [], "source": [ "_d = {'nb_dir': 'abc'}\n", - "test.equal(Config.from_dict(_d), Config(**(_CONFIG_DEFAULTS | _d)))" + "test.equal(Config.from_dict(_d, base_dir=Path('.')), Config(**(config_defaults(Path('.')) | _d)))" ] }, { @@ -195,11 +232,11 @@ "outputs": [], "source": [ "#| export\n", - "def get_config(curdir:str|Path='.')->Config:\n", - " p = _config_path(curdir)\n", - " if not p: return Config.default()\n", + "def get_config(curdir:str|Path='.')->Config|None:\n", + " p = config_path(curdir)\n", + " if not p: return None\n", " nb = tomllib.loads(p.read_text()).get('tool', {}).get('nb', {})\n", - " return Config.from_dict(nb)" + " return Config.from_dict(nb, base_dir=p.parent)" ] }, { @@ -210,7 +247,7 @@ "outputs": [], "source": [ "with TemporaryDirectory() as tmp:\n", - " test.equal(get_config(tmp), Config.default())" + " test.equal(get_config(tmp), None)" ] }, { @@ -223,7 +260,7 @@ "with TemporaryDirectory() as tmp:\n", " p = Path(tmp)/'pyproject.toml'\n", " p.touch()\n", - " test.equal(get_config(tmp), Config.default())" + " test.equal(get_config(tmp), Config.default(tmp))" ] }, { @@ -236,7 +273,7 @@ "with TemporaryDirectory() as tmp:\n", " p = Path(tmp)/'pyproject.toml'\n", " p.write_text('[tool.nb]')\n", - " test.equal(get_config(tmp), Config.default())" + " test.equal(get_config(tmp), Config.default(tmp))" ] }, { @@ -265,7 +302,7 @@ "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'}))" + " test.equal(get_config(tmp), Config.from_dict({'nb_dir': 'abc/test'}, tmp))" ] }, { |