Skip to content

Commit d3ddfa2

Browse files
authored
add config object and parser (#30)
1 parent 677c5b0 commit d3ddfa2

File tree

2 files changed

+54
-24
lines changed

2 files changed

+54
-24
lines changed

plux/build/config.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
"""
2+
Module for our config wrapper. Currently, this only supports ``pyproject.toml`` files but could be extended in the
3+
future to support ``tox.ini``, ``setup.cfg``, etc.
4+
"""
5+
import dataclasses
6+
import os
7+
from importlib.util import find_spec
8+
import typing as t
9+
10+
11+
@dataclasses.dataclass
12+
class PluxConfiguration:
13+
"""
14+
Configuration object with sane default values.
15+
"""
16+
exclude: t.List[str] = dataclasses.field(default_factory=list)
17+
18+
19+
def parse_pyproject_toml(path: str | os.PathLike[str]) -> PluxConfiguration:
20+
"""
21+
Parses a pyproject.toml file and searches for the ``[tool.plux]`` section. It then creates a ``Configuration``
22+
object from the found values. Uses tomli or tomllib to parse the file.
23+
24+
:param path: Path to the pyproject.toml file.
25+
:return: A ``Configuration`` object containing the parsed values.
26+
:raises FileNotFoundError: If the file does not exist.
27+
"""
28+
if find_spec("tomllib"):
29+
from tomllib import load as load_toml
30+
elif find_spec("tomli"):
31+
from tomli import load as load_toml
32+
else:
33+
raise ImportError("Could not find a TOML parser. Please install either tomllib or tomli.")
34+
35+
if not os.path.exists(path):
36+
raise FileNotFoundError(f"No pyproject.toml found at {path}")
37+
38+
with open(path, "rb") as file:
39+
pyproject_config = load_toml(file)
40+
41+
tool_table = pyproject_config.get("tool", {})
42+
tool_config = tool_table.get("plux", {})
43+
44+
return PluxConfiguration(**tool_config)

plux/build/setuptools.py

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@
1010
import sys
1111
import typing as t
1212
from fnmatch import fnmatchcase
13-
from importlib.util import find_spec
1413
from pathlib import Path
1514

1615
import setuptools
1716
from setuptools.command.egg_info import egg_info
1817

18+
from plux.build import config
19+
1920
try:
2021
from setuptools.command.editable_wheel import editable_wheel
2122
except ImportError:
@@ -61,7 +62,7 @@ def finalize_options(self) -> None:
6162
# we merge the configuration from the CLI arguments with the configuration read from the `pyproject.toml`
6263
# [tool.plux] section
6364
project_config = read_plux_configuration(self.distribution)
64-
file_exclude = project_config.get("exclude")
65+
file_exclude = project_config.exclude
6566
if file_exclude:
6667
self.exclude = set(self.exclude) | set(file_exclude)
6768
self.exclude = [_path_to_module(item) for item in self.exclude]
@@ -198,43 +199,28 @@ def load_plux_entrypoints(cmd, file_name, file_path):
198199
write_entries(cmd, ep_file, ep_path)
199200

200201

201-
def get_plux_json_path(distribution):
202+
def get_plux_json_path(distribution: setuptools.Distribution) -> str:
202203
dirs = distribution.package_dir
203204
egg_base = (dirs or {}).get("", os.curdir)
204205
egg_info_dir = _to_filename(_safe_name(distribution.get_name())) + ".egg-info"
205206
egg_info_dir = os.path.join(egg_base, egg_info_dir)
206207
return os.path.join(egg_info_dir, "plux.json")
207208

208209

209-
def read_plux_configuration(distribution) -> dict:
210+
def read_plux_configuration(distribution) -> config.PluxConfiguration:
210211
"""
211-
Try reading the [tool.plux] section of the `pyproject.toml` TOML file of the Distribution, and returns it as a
212-
dictionary.
212+
Try reading the [tool.plux] section of the `pyproject.toml` TOML file of the Distribution, and parse it using our
213+
config parser.
213214
"""
214-
if find_spec("tomllib"):
215-
# the tomllib library is part of the standard library since 3.11
216-
from tomllib import load as load_toml
217-
elif find_spec("tomli"):
218-
# setuptools vendors the tomli library in 3.10
219-
from tomli import load as load_toml
220-
else:
221-
# if we cannot find a TOML lib, we do not return any configuration
222-
return {}
223-
224215
dirs = distribution.package_dir
225216
pyproject_base = (dirs or {}).get("", os.curdir)
226217
pyproject_file = os.path.join(pyproject_base, "pyproject.toml")
227218
if not os.path.exists(pyproject_file):
228-
return {}
229-
230-
with open(pyproject_file, "rb") as file:
231-
pyproject_config = load_toml(file)
232-
233-
tool_table = pyproject_config.get("tool", {})
234-
return tool_table.get("plux", {})
219+
return config.PluxConfiguration()
235220

221+
return config.parse_pyproject_toml(pyproject_file)
236222

237-
def update_entrypoints(distribution, ep: EntryPointDict):
223+
def update_entrypoints(distribution: setuptools.Distribution, ep: EntryPointDict):
238224
if distribution.entry_points is None:
239225
distribution.entry_points = {}
240226

0 commit comments

Comments
 (0)