From 0f3ee8dbfb3affadc83ec7bd3273019c6f48812b Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 23 May 2026 16:37:57 +0000 Subject: Add pyproject.toml dir relative config paths --- nbs/config.ipynb | 75 ++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 56 insertions(+), 19 deletions(-) (limited to 'nbs') 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))" ] }, { -- cgit v1.2.3