Skip to content

Commit

Permalink
feat: process overrides directly in config file
Browse files Browse the repository at this point in the history
  • Loading branch information
vberlier committed May 21, 2022
1 parent 7c9268b commit 3c49d83
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 11 deletions.
28 changes: 18 additions & 10 deletions beet/toolchain/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

import toml
import yaml
from pydantic import BaseModel, ValidationError, validator
from pydantic import BaseModel, ValidationError, root_validator, validator
from pydantic.generics import GenericModel

from beet.core.error import BubbleException
Expand All @@ -45,7 +45,7 @@
resolve_packageable_path,
)

from .utils import apply_option, eval_option
from .utils import apply_option, eval_option, iter_options

DETECT_CONFIG_FILES: Tuple[str, ...] = (
"beet.json",
Expand Down Expand Up @@ -206,6 +206,19 @@ class ProjectConfig(BaseModel):
class Config:
extra = "forbid"

@root_validator(pre=True)
def apply_overrides(cls, values: JsonDict):
"""Apply config overrides."""
for option in iter_options(values):
try:
values = apply_option(values, eval_option(option))
except Exception:
raise InvalidProjectConfig(
f"Couldn't apply override {option!r}."
) from None
values.pop("overrides", None)
return values

def resolve(self, directory: FileSystemPath) -> "ProjectConfig":
"""Resolve paths relative to the given directory and apply inheritance."""
path = Path(directory)
Expand Down Expand Up @@ -329,17 +342,12 @@ def load_config(
if authors := poetry.get("authors"):
config.setdefault("author", authors[0])

if overrides:
config["overrides"] = [config.get("overrides"), overrides]

config_dir = path.resolve().parent if path else None

with local_import_path(str(config_dir)) if config_dir else nullcontext():
for option in overrides or []:
try:
config = apply_option(config, eval_option(option))
except Exception:
raise InvalidProjectConfig(
f"Couldn't apply override {option!r}."
) from None

return ProjectConfig(**config).resolve(config_dir or Path.cwd())


Expand Down
13 changes: 12 additions & 1 deletion beet/toolchain/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"stable_int_hash",
"stable_hash",
"ensure_builtins",
"iter_options",
"eval_option",
"default_option",
"apply_option",
Expand All @@ -12,7 +13,7 @@
import json
import re
from copy import copy
from typing import Any, Callable, List, Literal, Mapping, Sequence, cast
from typing import Any, Callable, Iterable, List, Literal, Mapping, Sequence, cast

FNV_32_INIT = 0x811C9DC5
FNV_64_INIT = 0xCBF29CE484222325
Expand Down Expand Up @@ -72,6 +73,16 @@ def ensure_builtins(value: Any) -> Any:
raise TypeError(value) from None


def iter_options(options: Any) -> Iterable[str]:
if isinstance(options, str):
yield from options.splitlines()
elif isinstance(options, Mapping):
yield from iter_options(options.get("overrides", [])) # type: ignore
elif isinstance(options, Sequence):
for option in options: # type: ignore
yield from iter_options(option)


def eval_option(option: str) -> Any:
option = option.strip()

Expand Down
8 changes: 8 additions & 0 deletions tests/config_examples/overrides/beet.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"overrides": [
"name = foo",
"output = out",
"data_pack.load = src",
"pipeline[] = beet.contrib.minify_json"
]
}
40 changes: 40 additions & 0 deletions tests/snapshots/config__config_resolution_overrides__0.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"id": "",
"name": "foo",
"description": "",
"author": "",
"version": "",
"directory": "tests/config_examples/overrides",
"extend": [],
"output": "tests/config_examples/overrides/out",
"ignore": [],
"whitelist": null,
"require": [],
"templates": [],
"data_pack": {
"name": "",
"description": "",
"pack_format": 0,
"zipped": null,
"compression": null,
"compression_level": null,
"load": [
"tests/config_examples/overrides/src"
],
"render": {}
},
"resource_pack": {
"name": "",
"description": "",
"pack_format": 0,
"zipped": null,
"compression": null,
"compression_level": null,
"load": [],
"render": {}
},
"pipeline": [
"beet.contrib.minify_json"
],
"meta": {}
}

0 comments on commit 3c49d83

Please sign in to comment.