Skip to content

Commit

Permalink
feat: use poetry project data when available
Browse files Browse the repository at this point in the history
  • Loading branch information
vberlier committed Jun 10, 2021
1 parent 66946b8 commit aa4c823
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 6 deletions.
22 changes: 16 additions & 6 deletions beet/toolchain/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,20 +158,30 @@ def locate_config(initial_directory: FileSystemPath, *filenames: str) -> List[Pa
def load_config(filename: FileSystemPath) -> ProjectConfig:
"""Load the project config at the specified location."""
path = Path(filename)

with config_error_handler(path):
if path.suffix == ".toml":
config = toml.loads(path.read_text())
elif path.suffix in [".yml", ".yaml"]:
config = yaml.safe_load(path.read_text())
else:
config = json.loads(path.read_text())

if path.name == "pyproject.toml":
try:
config = config["tool"]["beet"]
except KeyError as exc:
raise InvalidProjectConfig(
f"{path}: Missing [tool.beet] section"
) from exc
tool = config.get("tool")
if tool is None or (config := tool.get("beet")) is None:
raise InvalidProjectConfig(f"{path}: Missing [tool.beet] section")

if poetry := tool.get("poetry"):
if name := poetry.get("name"):
config.setdefault("name", name)
if description := poetry.get("description"):
config.setdefault("description", description)
if version := poetry.get("version"):
config.setdefault("version", version)
if authors := poetry.get("authors"):
config.setdefault("author", authors[0])

return ProjectConfig(**config).resolve(path.parent)


Expand Down
30 changes: 30 additions & 0 deletions examples/load_pyproject_inherit/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[tool.poetry]
name = "demo"
version = "0.1.0"
description = "Demo project"
authors = ["Valentin Berlier <berlier.v@gmail.com>"]
license = "MIT"

[tool.poetry.dependencies]
python = "^3.9"
beet = "^0.30.0"

[tool.beet]
pipeline = ["beet.contrib.format_json"]

[tool.beet.data_pack]
name = "{{ project_name | capitalize }} {{ project_version }}"
pack_format = 7
description = [
{ text = "{{ project_name | capitalize }} ", color = "#9273ff" },
{ text = "v{{ project_version }}\n", color = "#66c546" },
{ text = "{{ project_description }}", color = "#00aced" },
]
load = ["src"]

[tool.beet.meta.format_json]
indent = 4

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
say hello
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
say hello
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"pack": {
"pack_format": 7,
"description": [
{
"text": "Demo ",
"color": "#9273ff"
},
{
"text": "v0.1.0\n",
"color": "#66c546"
},
{
"text": "Demo project",
"color": "#00aced"
}
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"pack": {
"pack_format": 6,
"description": "Demo project\nAuthor: Valentin Berlier <berlier.v@gmail.com>\nVersion: 0.1.0"
}
}

0 comments on commit aa4c823

Please sign in to comment.