Skip to content

Commit f744d60

Browse files
committed
stubber: enhance configuration handling with dynamic path resolution
Signed-off-by: Jos Verlinde <[email protected]>
1 parent 021ee8c commit f744d60

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

src/stubber/utils/config.py

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""stubber configuration"""
22

33
from pathlib import Path
4-
from typing import List
4+
from typing import List, Optional, Union
55

66
from mpflash.logger import log
77
from mpflash.versions import get_preview_mp_version, get_stable_mp_version, micropython_versions
@@ -13,6 +13,8 @@
1313

1414
@section("micropython-stubber")
1515
class StubberConfig(Config):
16+
_config_path = None
17+
1618
"stubber configuration class"
1719
# relative to stubs folder
1820
fallback_path: Path = key(
@@ -81,6 +83,15 @@ def template_path(self) -> Path:
8183
"return the stubs path in the microypthon-stubs repo"
8284
return self.mpy_stubs_path / "publish" / "template"
8385

86+
@property
87+
def config_path(self) -> Path:
88+
"return the config path"
89+
return self._config_path
90+
91+
@config_path.setter
92+
def config_path(self, value: Path):
93+
self._config_path = value
94+
8495
def post_read_hook(self) -> dict:
8596
config_updates = {}
8697
# relative to stubs
@@ -108,21 +119,27 @@ def post_read_hook(self) -> dict:
108119
return config_updates
109120

110121

111-
def readconfig(filename: str = "pyproject.toml", prefix: str = "tool.", must_exist: bool = True):
122+
def readconfig(
123+
location: Optional[Path] = None,
124+
filename: str = "pyproject.toml",
125+
prefix: str = "tool.",
126+
must_exist: bool = True,
127+
):
112128
"read the configuration from the pyproject.toml file"
113129
# locate the pyproject.toml file
114-
path = Path.cwd()
130+
config_path = location or Path.cwd()
115131
use_toml = True
116-
while not (path / filename).exists():
117-
path = path.parent
118-
if path == path.parent:
132+
while not (config_path / filename).exists():
133+
config_path = config_path.parent
134+
if config_path == config_path.parent:
119135
log.trace(f"Could not find config file: {filename}")
120136
use_toml = False
121137
break
122138

123-
filename = str(path / filename)
139+
filename = str(config_path / filename)
124140

125141
config = StubberConfig()
142+
config.config_path = config_path.absolute()
126143
# add provider sources to the config
127144
config.add_source(EnvironmentConfigSource())
128145
if use_toml:

0 commit comments

Comments
 (0)