Skip to content

Commit

Permalink
WIP improve path discovery of config file
Browse files Browse the repository at this point in the history
improve path discovery of pyproject.toml and
deprecated latexplotlib config to properly emit a
deprecation warning
  • Loading branch information
Constantin Gahr committed Oct 26, 2024
1 parent 2e64010 commit a92d797
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 26 deletions.
47 changes: 25 additions & 22 deletions src/latexplotlib/_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,32 +25,32 @@
DEFAULT_HEIGHT = 412


def find_pyproject_toml() -> Path:
def find_pyproject_toml() -> Path | None:
pyproject = "pyproject.toml"
path = Path().absolute()
while path != Path("/"):
if (path / pyproject).exists():
return path / pyproject
path = path.parent

msg = "Could not find 'pyproject.toml'"
raise FileNotFoundError(msg)
return None


def find_config_ini() -> Path:
msg = f"""
Configuring latexplotlib via {CONFIGPATH} is being deprecated. Please use
the [tool.latexplotlib] section of the 'pyproject.toml' file instead.
def find_config_ini() -> Path | None:
if CONFIGPATH.exists():
msg = f"""
Configuring latexplotlib via '{CONFIGPATH}' is being deprecated. Please use
the [tool.latexplotlib] section of the 'pyproject.toml' file instead. If a
'pyproject.toml' file is present in the current directory or a parent
directory, the configuration at '{CONFIGPATH}' is being ignored.
To silence this warning, please delete the config file {CONFIGPATH}
"""
warnings.warn(msg, DeprecationWarning, stacklevel=2)
To silence this warning, please delete the config file '{CONFIGPATH}'.
"""
warnings.warn(msg, DeprecationWarning, stacklevel=5)

if CONFIGPATH.exists():
return CONFIGPATH

msg = f"No such file: '{CONFIGPATH}'"
raise FileNotFoundError(msg)
return None


class Size:
Expand Down Expand Up @@ -133,13 +133,16 @@ def __str__(self) -> str:
return str(f"{self._width}pt, {self._height}pt")


try:
path = find_pyproject_toml()
size = Size.from_pyproject_toml(path)
except FileNotFoundError:
try:
path = find_config_ini()
def get_size() -> Size:
if (path := find_pyproject_toml()) is not None:
# look for config.ini to emit deprecation warning if it exists
_ = find_config_ini()
return Size.from_pyproject_toml(path)

if (path := find_config_ini()) is not None:
return Size.from_config_ini(path)

return Size(width=DEFAULT_WIDTH, height=DEFAULT_HEIGHT)


size = Size.from_config_ini(path)
except FileNotFoundError:
size = Size(width=DEFAULT_WIDTH, height=DEFAULT_HEIGHT)
size = get_size()
11 changes: 7 additions & 4 deletions tests/test_10_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ def test_pyproject_exists(self, path):
cfg.find_pyproject_toml()

def test_pyproject_not_exists(self, path):
with pytest.raises(FileNotFoundError):
cfg.find_pyproject_toml()
assert cfg.find_pyproject_toml() is None


class TestFindConfigIni:
Expand All @@ -45,8 +44,7 @@ def test_configini_exists(self, path):
cfg.find_config_ini()

def test_configini_not_exists(self, path):
with pytest.warns(DeprecationWarning), pytest.raises(FileNotFoundError):
cfg.find_config_ini()
assert cfg.find_config_ini() is None


class TestSize:
Expand Down Expand Up @@ -127,3 +125,8 @@ def test_from_pyproject_toml_tool_latexplotlib(self, tmp_path):
size = cfg.Size.from_pyproject_toml(path)
assert size._width == cfg.DEFAULT_WIDTH
assert size._height == cfg.DEFAULT_HEIGHT


class TestGetSize:
def test_todo(self):
raise NotImplementedError

0 comments on commit a92d797

Please sign in to comment.