Skip to content

Commit

Permalink
feat: supported_formats and overlays in config
Browse files Browse the repository at this point in the history
  • Loading branch information
vberlier committed Oct 29, 2023
1 parent 51772a6 commit e879c64
Show file tree
Hide file tree
Showing 23 changed files with 279 additions and 9 deletions.
2 changes: 1 addition & 1 deletion beet/core/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def merge(self: FileType, other: FileType) -> bool:
"""Merge the given file or return False to indicate no special handling."""
return False

def bind(self, pack: Any, path: str) -> Any:
def bind(self, pack: Any, path: str):
"""Handle file binding."""
if self.on_bind:
self.on_bind(self, pack, path)
Expand Down
11 changes: 10 additions & 1 deletion beet/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"JsonDict",
"FileSystemPath",
"TextComponent",
"FormatsRangeDict",
"SupportedFormats",
"Sentinel",
"SENTINEL_OBJ",
Expand Down Expand Up @@ -50,6 +51,7 @@
Optional,
Protocol,
Tuple,
TypedDict,
TypeVar,
Union,
runtime_checkable,
Expand All @@ -70,7 +72,14 @@ def __fspath__(self) -> str:
JsonDict = Dict[str, Any]
FileSystemPath = Union[str, PathLikeFallback]
TextComponent = Union[str, List[Any], JsonDict]
SupportedFormats = Union[int, List[int], JsonDict]


class FormatsRangeDict(TypedDict):
min_inclusive: int
max_inclusive: int


SupportedFormats = Union[int, List[int], FormatsRangeDict]


class Sentinel:
Expand Down
15 changes: 14 additions & 1 deletion beet/library/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import shutil
from collections import defaultdict
from contextlib import nullcontext
from copy import deepcopy
from dataclasses import dataclass, field
from functools import partial
from itertools import count
Expand Down Expand Up @@ -120,7 +121,7 @@ def __init__(
def merge(self, other: Any) -> bool:
...

def bind(self, pack: Any, path: str) -> Any:
def bind(self, pack: Any, path: str):
...

def set_content(self, content: Any):
Expand Down Expand Up @@ -155,6 +156,9 @@ def load(cls: Type[T], origin: FileOrigin, path: FileSystemPath) -> T:
def dump(self, origin: FileOrigin, path: FileSystemPath):
...

def convert(self, file_type: Type[PackFileType]) -> PackFileType:
...


class MergeCallback(Protocol):
"""Protocol for detecting merge callbacks."""
Expand Down Expand Up @@ -764,6 +768,15 @@ def merge(self, other: "Mcmeta") -> bool: # type: ignore
for item in value.get("block", []):
if item not in block:
block.append(item)
elif key == "overlays":
for other_entry in value.get("entries"):
overlays: Any = self.data.setdefault("overlays", {})
for entry in overlays.setdefault("entries", []):
if entry.get("directory") == other_entry.get("directory"):
entry["formats"] = deepcopy(other_entry.get("formats"))
break
else:
overlays["entries"].append(deepcopy(other_entry))
else:
self.data[key] = value
return True
Expand Down
23 changes: 23 additions & 0 deletions beet/toolchain/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
from beet.core.error import BubbleException
from beet.core.utils import (
FileSystemPath,
FormatsRangeDict,
JsonDict,
SupportedFormats,
TextComponent,
format_validation_error,
local_import_path,
Expand Down Expand Up @@ -151,13 +153,22 @@ def with_defaults(self, other: "PackFilterConfig") -> "PackFilterConfig":
)


class PackOverlayConfig(BaseModel):
"""Overlay entry configuration."""

formats: FormatsRangeDict
directory: str


class PackConfig(BaseModel):
"""Data pack and resource pack configuration."""

name: str = ""
description: TextComponent = ""
pack_format: int = 0
filter: Optional[PackFilterConfig] = None
supported_formats: Optional[SupportedFormats] = None
overlays: Optional[ListOption[PackOverlayConfig]] = None
zipped: Optional[bool] = None
compression: Optional[Literal["none", "deflate", "bzip2", "lzma"]] = None
compression_level: Optional[int] = None
Expand All @@ -180,6 +191,18 @@ def with_defaults(self, other: "PackConfig") -> "PackConfig":
if self.filter and other.filter
else self.filter or other.filter
),
"supported_formats": (
other.supported_formats
if self.supported_formats is None
else self.supported_formats
),
"overlays": (
other.overlays
if self.overlays is None
else self.overlays
if other.overlays is None
else other.overlays.entries() + self.overlays.entries()
),
"zipped": other.zipped if self.zipped is None else self.zipped,
"compression": (
other.compression if self.compression is None else self.compression
Expand Down
7 changes: 7 additions & 0 deletions beet/toolchain/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,13 @@ def bootstrap(self, ctx: Context):
pack.mcmeta.merge(
Mcmeta({"filter": config.filter.dict(exclude_none=True)})
)
if config.supported_formats:
pack.supported_formats = config.supported_formats
if config.overlays:
for overlay in config.overlays.entries():
pack.overlays[overlay.directory].supported_formats = deepcopy(
overlay.formats
)
pack.zipped = bool(config.zipped)
pack.compression = config.compression
pack.compression_level = config.compression_level
Expand Down
7 changes: 7 additions & 0 deletions examples/load_overlay/beet.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
data_pack:
load: "src"
pack_format: 20
supported_formats: [18, 19, 20]
overlays:
- formats:
min_inclusive: 18
max_inclusive: 19
directory: "creeper"
21 changes: 21 additions & 0 deletions tests/config_examples/extend_everything/beet-base.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,34 @@
"data_pack": {
"name": "base",
"description": "base",
"filter": {
"block": [
{
"namespace": "base",
"path": "base"
}
]
},
"zipped": false,
"load": ["base"]
},
"resource_pack": {
"name": "base",
"description": "base",
"pack_format": 88,
"supported_formats": {
"min_inclusive": 88,
"max_inclusive": 99
},
"overlays": [
{
"directory": "base",
"formats": {
"min_inclusive": 88,
"max_inclusive": 99
}
}
],
"load": ["base"]
},
"pipeline": ["base"],
Expand Down
22 changes: 22 additions & 0 deletions tests/config_examples/extend_everything/beet.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,31 @@
"templates": ["thing"],
"data_pack": {
"name": "thing",
"supported_formats": 18,
"overlays": [
{
"directory": "abc",
"formats": {
"min_inclusive": 18,
"max_inclusive": 18
}
}
],
"compression": "none",
"load": ["thing"]
},
"resource_pack": {
"overlays": [
{
"directory": "123",
"formats": {
"min_inclusive": 18,
"max_inclusive": 18
}
}
],
"compression": "bzip2",
"compression_level": 9,
"load": ["thing"]
},
"pipeline": ["thing"],
Expand Down
4 changes: 4 additions & 0 deletions tests/snapshots/config__config_resolution_basic__0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
"description": "",
"pack_format": 0,
"filter": null,
"supported_formats": null,
"overlays": null,
"zipped": null,
"compression": null,
"compression_level": null,
Expand All @@ -35,6 +37,8 @@
"description": "",
"pack_format": 0,
"filter": null,
"supported_formats": null,
"overlays": null,
"zipped": null,
"compression": null,
"compression_level": null,
Expand Down
16 changes: 16 additions & 0 deletions tests/snapshots/config__config_resolution_crazy_extend__0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
"description": "",
"pack_format": 0,
"filter": null,
"supported_formats": null,
"overlays": null,
"zipped": null,
"compression": null,
"compression_level": null,
Expand All @@ -29,6 +31,8 @@
"description": "",
"pack_format": 0,
"filter": null,
"supported_formats": null,
"overlays": null,
"zipped": null,
"compression": null,
"compression_level": null,
Expand Down Expand Up @@ -56,6 +60,8 @@
"description": "",
"pack_format": 0,
"filter": null,
"supported_formats": null,
"overlays": null,
"zipped": null,
"compression": null,
"compression_level": null,
Expand All @@ -72,6 +78,8 @@
"description": "",
"pack_format": 0,
"filter": null,
"supported_formats": null,
"overlays": null,
"zipped": null,
"compression": null,
"compression_level": null,
Expand Down Expand Up @@ -103,6 +111,8 @@
"description": "",
"pack_format": 0,
"filter": null,
"supported_formats": null,
"overlays": null,
"zipped": null,
"compression": null,
"compression_level": null,
Expand All @@ -122,6 +132,8 @@
"description": "",
"pack_format": 0,
"filter": null,
"supported_formats": null,
"overlays": null,
"zipped": null,
"compression": null,
"compression_level": null,
Expand Down Expand Up @@ -153,6 +165,8 @@
"description": "",
"pack_format": 0,
"filter": null,
"supported_formats": null,
"overlays": null,
"zipped": null,
"compression": null,
"compression_level": null,
Expand All @@ -166,6 +180,8 @@
"description": "",
"pack_format": 0,
"filter": null,
"supported_formats": null,
"overlays": null,
"zipped": null,
"compression": null,
"compression_level": null,
Expand Down
4 changes: 4 additions & 0 deletions tests/snapshots/config__config_resolution_empty__0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
"description": "",
"pack_format": 0,
"filter": null,
"supported_formats": null,
"overlays": null,
"zipped": null,
"compression": null,
"compression_level": null,
Expand All @@ -29,6 +31,8 @@
"description": "",
"pack_format": 0,
"filter": null,
"supported_formats": null,
"overlays": null,
"zipped": null,
"compression": null,
"compression_level": null,
Expand Down
45 changes: 41 additions & 4 deletions tests/snapshots/config__config_resolution_extend_everything__0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,26 @@
"name": "thing",
"description": "base",
"pack_format": 4,
"filter": null,
"filter": {
"block": [
{
"namespace": "base",
"path": "base"
}
]
},
"supported_formats": 18,
"overlays": [
{
"formats": {
"min_inclusive": 18,
"max_inclusive": 18
},
"directory": "abc"
}
],
"zipped": false,
"compression": null,
"compression": "none",
"compression_level": null,
"load": [
"tests/config_examples/extend_everything/subdir/base",
Expand All @@ -44,9 +61,29 @@
"description": "base",
"pack_format": 88,
"filter": null,
"supported_formats": {
"min_inclusive": 88,
"max_inclusive": 99
},
"overlays": [
{
"formats": {
"min_inclusive": 88,
"max_inclusive": 99
},
"directory": "base"
},
{
"formats": {
"min_inclusive": 18,
"max_inclusive": 18
},
"directory": "123"
}
],
"zipped": true,
"compression": null,
"compression_level": null,
"compression": "bzip2",
"compression_level": 9,
"load": [
"tests/config_examples/extend_everything/subdir/base",
"tests/config_examples/extend_everything/thing"
Expand Down
Loading

0 comments on commit e879c64

Please sign in to comment.