From e879c64cf0e43edc504ddb973f9b1eb2d5a5ff42 Mon Sep 17 00:00:00 2001 From: Valentin Berlier Date: Sun, 29 Oct 2023 19:09:53 +0100 Subject: [PATCH] feat: supported_formats and overlays in config --- beet/core/file.py | 2 +- beet/core/utils.py | 11 ++++- beet/library/base.py | 15 ++++++- beet/toolchain/config.py | 23 ++++++++++ beet/toolchain/project.py | 7 +++ examples/load_overlay/beet.yml | 7 +++ .../extend_everything/beet-base.json | 21 +++++++++ .../extend_everything/beet.json | 22 +++++++++ .../config__config_resolution_basic__0.txt | 4 ++ ...fig__config_resolution_crazy_extend__0.txt | 16 +++++++ .../config__config_resolution_empty__0.txt | 4 ++ ...config_resolution_extend_everything__0.txt | 45 +++++++++++++++++-- ...config__config_resolution_load_pack__0.txt | 4 ++ ..._config_resolution_load_with_output__0.txt | 4 ++ ...nfig_resolution_load_with_templates__0.txt | 4 ++ ...onfig__config_resolution_more_stuff__0.txt | 4 ++ .../config__config_resolution_nested__0.txt | 8 ++++ ...config__config_resolution_overrides__0.txt | 4 ++ ...nfig__config_resolution_pack_filter__0.txt | 4 ++ ...config__config_resolution_whitelist__0.txt | 4 ++ ..._config_resolution_whitelist_nested__0.txt | 16 +++++++ .../pack.mcmeta | 16 ++++++- tests/test_data_pack.py | 43 ++++++++++++++++++ 23 files changed, 279 insertions(+), 9 deletions(-) diff --git a/beet/core/file.py b/beet/core/file.py index cfbf738a..41a6f816 100644 --- a/beet/core/file.py +++ b/beet/core/file.py @@ -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) diff --git a/beet/core/utils.py b/beet/core/utils.py index 58718839..77bb3529 100644 --- a/beet/core/utils.py +++ b/beet/core/utils.py @@ -2,6 +2,7 @@ "JsonDict", "FileSystemPath", "TextComponent", + "FormatsRangeDict", "SupportedFormats", "Sentinel", "SENTINEL_OBJ", @@ -50,6 +51,7 @@ Optional, Protocol, Tuple, + TypedDict, TypeVar, Union, runtime_checkable, @@ -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: diff --git a/beet/library/base.py b/beet/library/base.py index 2ea221ed..c3dc4b84 100644 --- a/beet/library/base.py +++ b/beet/library/base.py @@ -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 @@ -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): @@ -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.""" @@ -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 diff --git a/beet/toolchain/config.py b/beet/toolchain/config.py index 1e1efcf0..e04defd5 100644 --- a/beet/toolchain/config.py +++ b/beet/toolchain/config.py @@ -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, @@ -151,6 +153,13 @@ 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.""" @@ -158,6 +167,8 @@ class PackConfig(BaseModel): 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 @@ -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 diff --git a/beet/toolchain/project.py b/beet/toolchain/project.py index e543c046..d3f1c759 100644 --- a/beet/toolchain/project.py +++ b/beet/toolchain/project.py @@ -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 diff --git a/examples/load_overlay/beet.yml b/examples/load_overlay/beet.yml index 5084bfd8..0619fa3e 100644 --- a/examples/load_overlay/beet.yml +++ b/examples/load_overlay/beet.yml @@ -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" diff --git a/tests/config_examples/extend_everything/beet-base.json b/tests/config_examples/extend_everything/beet-base.json index ad7c3783..3973432b 100644 --- a/tests/config_examples/extend_everything/beet-base.json +++ b/tests/config_examples/extend_everything/beet-base.json @@ -7,6 +7,14 @@ "data_pack": { "name": "base", "description": "base", + "filter": { + "block": [ + { + "namespace": "base", + "path": "base" + } + ] + }, "zipped": false, "load": ["base"] }, @@ -14,6 +22,19 @@ "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"], diff --git a/tests/config_examples/extend_everything/beet.json b/tests/config_examples/extend_everything/beet.json index a1d5ca16..3f439f27 100644 --- a/tests/config_examples/extend_everything/beet.json +++ b/tests/config_examples/extend_everything/beet.json @@ -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"], diff --git a/tests/snapshots/config__config_resolution_basic__0.txt b/tests/snapshots/config__config_resolution_basic__0.txt index 03bb0053..7c46439d 100644 --- a/tests/snapshots/config__config_resolution_basic__0.txt +++ b/tests/snapshots/config__config_resolution_basic__0.txt @@ -18,6 +18,8 @@ "description": "", "pack_format": 0, "filter": null, + "supported_formats": null, + "overlays": null, "zipped": null, "compression": null, "compression_level": null, @@ -35,6 +37,8 @@ "description": "", "pack_format": 0, "filter": null, + "supported_formats": null, + "overlays": null, "zipped": null, "compression": null, "compression_level": null, diff --git a/tests/snapshots/config__config_resolution_crazy_extend__0.txt b/tests/snapshots/config__config_resolution_crazy_extend__0.txt index 079fec22..4d074f50 100644 --- a/tests/snapshots/config__config_resolution_crazy_extend__0.txt +++ b/tests/snapshots/config__config_resolution_crazy_extend__0.txt @@ -18,6 +18,8 @@ "description": "", "pack_format": 0, "filter": null, + "supported_formats": null, + "overlays": null, "zipped": null, "compression": null, "compression_level": null, @@ -29,6 +31,8 @@ "description": "", "pack_format": 0, "filter": null, + "supported_formats": null, + "overlays": null, "zipped": null, "compression": null, "compression_level": null, @@ -56,6 +60,8 @@ "description": "", "pack_format": 0, "filter": null, + "supported_formats": null, + "overlays": null, "zipped": null, "compression": null, "compression_level": null, @@ -72,6 +78,8 @@ "description": "", "pack_format": 0, "filter": null, + "supported_formats": null, + "overlays": null, "zipped": null, "compression": null, "compression_level": null, @@ -103,6 +111,8 @@ "description": "", "pack_format": 0, "filter": null, + "supported_formats": null, + "overlays": null, "zipped": null, "compression": null, "compression_level": null, @@ -122,6 +132,8 @@ "description": "", "pack_format": 0, "filter": null, + "supported_formats": null, + "overlays": null, "zipped": null, "compression": null, "compression_level": null, @@ -153,6 +165,8 @@ "description": "", "pack_format": 0, "filter": null, + "supported_formats": null, + "overlays": null, "zipped": null, "compression": null, "compression_level": null, @@ -166,6 +180,8 @@ "description": "", "pack_format": 0, "filter": null, + "supported_formats": null, + "overlays": null, "zipped": null, "compression": null, "compression_level": null, diff --git a/tests/snapshots/config__config_resolution_empty__0.txt b/tests/snapshots/config__config_resolution_empty__0.txt index 4ec89254..f4636fa2 100644 --- a/tests/snapshots/config__config_resolution_empty__0.txt +++ b/tests/snapshots/config__config_resolution_empty__0.txt @@ -18,6 +18,8 @@ "description": "", "pack_format": 0, "filter": null, + "supported_formats": null, + "overlays": null, "zipped": null, "compression": null, "compression_level": null, @@ -29,6 +31,8 @@ "description": "", "pack_format": 0, "filter": null, + "supported_formats": null, + "overlays": null, "zipped": null, "compression": null, "compression_level": null, diff --git a/tests/snapshots/config__config_resolution_extend_everything__0.txt b/tests/snapshots/config__config_resolution_extend_everything__0.txt index 7e3c8227..2b275590 100644 --- a/tests/snapshots/config__config_resolution_extend_everything__0.txt +++ b/tests/snapshots/config__config_resolution_extend_everything__0.txt @@ -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", @@ -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" diff --git a/tests/snapshots/config__config_resolution_load_pack__0.txt b/tests/snapshots/config__config_resolution_load_pack__0.txt index cde89d28..e8bd2b0f 100644 --- a/tests/snapshots/config__config_resolution_load_pack__0.txt +++ b/tests/snapshots/config__config_resolution_load_pack__0.txt @@ -18,6 +18,8 @@ "description": "", "pack_format": 0, "filter": null, + "supported_formats": null, + "overlays": null, "zipped": null, "compression": null, "compression_level": null, @@ -31,6 +33,8 @@ "description": "", "pack_format": 0, "filter": null, + "supported_formats": null, + "overlays": null, "zipped": null, "compression": null, "compression_level": null, diff --git a/tests/snapshots/config__config_resolution_load_with_output__0.txt b/tests/snapshots/config__config_resolution_load_with_output__0.txt index c3eb33aa..f0743f2b 100644 --- a/tests/snapshots/config__config_resolution_load_with_output__0.txt +++ b/tests/snapshots/config__config_resolution_load_with_output__0.txt @@ -18,6 +18,8 @@ "description": "", "pack_format": 0, "filter": null, + "supported_formats": null, + "overlays": null, "zipped": null, "compression": null, "compression_level": null, @@ -32,6 +34,8 @@ "description": "", "pack_format": 0, "filter": null, + "supported_formats": null, + "overlays": null, "zipped": null, "compression": null, "compression_level": null, diff --git a/tests/snapshots/config__config_resolution_load_with_templates__0.txt b/tests/snapshots/config__config_resolution_load_with_templates__0.txt index ba674d94..495932da 100644 --- a/tests/snapshots/config__config_resolution_load_with_templates__0.txt +++ b/tests/snapshots/config__config_resolution_load_with_templates__0.txt @@ -21,6 +21,8 @@ "description": "", "pack_format": 0, "filter": null, + "supported_formats": null, + "overlays": null, "zipped": null, "compression": null, "compression_level": null, @@ -34,6 +36,8 @@ "description": "", "pack_format": 0, "filter": null, + "supported_formats": null, + "overlays": null, "zipped": null, "compression": null, "compression_level": null, diff --git a/tests/snapshots/config__config_resolution_more_stuff__0.txt b/tests/snapshots/config__config_resolution_more_stuff__0.txt index 60ea854d..c27e9589 100644 --- a/tests/snapshots/config__config_resolution_more_stuff__0.txt +++ b/tests/snapshots/config__config_resolution_more_stuff__0.txt @@ -20,6 +20,8 @@ "description": "The description of my data pack", "pack_format": 6, "filter": null, + "supported_formats": null, + "overlays": null, "zipped": true, "compression": null, "compression_level": null, @@ -38,6 +40,8 @@ "description": "The description of my resource pack", "pack_format": 6, "filter": null, + "supported_formats": null, + "overlays": null, "zipped": true, "compression": null, "compression_level": null, diff --git a/tests/snapshots/config__config_resolution_nested__0.txt b/tests/snapshots/config__config_resolution_nested__0.txt index 8db545c0..edd89b7e 100644 --- a/tests/snapshots/config__config_resolution_nested__0.txt +++ b/tests/snapshots/config__config_resolution_nested__0.txt @@ -18,6 +18,8 @@ "description": "", "pack_format": 0, "filter": null, + "supported_formats": null, + "overlays": null, "zipped": null, "compression": null, "compression_level": null, @@ -31,6 +33,8 @@ "description": "", "pack_format": 0, "filter": null, + "supported_formats": null, + "overlays": null, "zipped": null, "compression": null, "compression_level": null, @@ -58,6 +62,8 @@ "description": "", "pack_format": 0, "filter": null, + "supported_formats": null, + "overlays": null, "zipped": null, "compression": null, "compression_level": null, @@ -71,6 +77,8 @@ "description": "", "pack_format": 0, "filter": null, + "supported_formats": null, + "overlays": null, "zipped": null, "compression": null, "compression_level": null, diff --git a/tests/snapshots/config__config_resolution_overrides__0.txt b/tests/snapshots/config__config_resolution_overrides__0.txt index 27cb32ac..4fc72b6a 100644 --- a/tests/snapshots/config__config_resolution_overrides__0.txt +++ b/tests/snapshots/config__config_resolution_overrides__0.txt @@ -18,6 +18,8 @@ "description": "", "pack_format": 0, "filter": null, + "supported_formats": null, + "overlays": null, "zipped": null, "compression": null, "compression_level": null, @@ -31,6 +33,8 @@ "description": "", "pack_format": 0, "filter": null, + "supported_formats": null, + "overlays": null, "zipped": null, "compression": null, "compression_level": null, diff --git a/tests/snapshots/config__config_resolution_pack_filter__0.txt b/tests/snapshots/config__config_resolution_pack_filter__0.txt index 767795b0..d22f6a3d 100644 --- a/tests/snapshots/config__config_resolution_pack_filter__0.txt +++ b/tests/snapshots/config__config_resolution_pack_filter__0.txt @@ -29,6 +29,8 @@ } ] }, + "supported_formats": null, + "overlays": null, "zipped": null, "compression": null, "compression_level": null, @@ -40,6 +42,8 @@ "description": "", "pack_format": 0, "filter": null, + "supported_formats": null, + "overlays": null, "zipped": null, "compression": null, "compression_level": null, diff --git a/tests/snapshots/config__config_resolution_whitelist__0.txt b/tests/snapshots/config__config_resolution_whitelist__0.txt index 5ae70954..142394eb 100644 --- a/tests/snapshots/config__config_resolution_whitelist__0.txt +++ b/tests/snapshots/config__config_resolution_whitelist__0.txt @@ -21,6 +21,8 @@ "description": "", "pack_format": 0, "filter": null, + "supported_formats": null, + "overlays": null, "zipped": null, "compression": null, "compression_level": null, @@ -32,6 +34,8 @@ "description": "", "pack_format": 0, "filter": null, + "supported_formats": null, + "overlays": null, "zipped": null, "compression": null, "compression_level": null, diff --git a/tests/snapshots/config__config_resolution_whitelist_nested__0.txt b/tests/snapshots/config__config_resolution_whitelist_nested__0.txt index 9381529f..1017aa2c 100644 --- a/tests/snapshots/config__config_resolution_whitelist_nested__0.txt +++ b/tests/snapshots/config__config_resolution_whitelist_nested__0.txt @@ -22,6 +22,8 @@ "description": "", "pack_format": 0, "filter": null, + "supported_formats": null, + "overlays": null, "zipped": null, "compression": null, "compression_level": null, @@ -33,6 +35,8 @@ "description": "", "pack_format": 0, "filter": null, + "supported_formats": null, + "overlays": null, "zipped": null, "compression": null, "compression_level": null, @@ -65,6 +69,8 @@ "description": "", "pack_format": 0, "filter": null, + "supported_formats": null, + "overlays": null, "zipped": null, "compression": null, "compression_level": null, @@ -76,6 +82,8 @@ "description": "", "pack_format": 0, "filter": null, + "supported_formats": null, + "overlays": null, "zipped": null, "compression": null, "compression_level": null, @@ -108,6 +116,8 @@ "description": "", "pack_format": 0, "filter": null, + "supported_formats": null, + "overlays": null, "zipped": null, "compression": null, "compression_level": null, @@ -119,6 +129,8 @@ "description": "", "pack_format": 0, "filter": null, + "supported_formats": null, + "overlays": null, "zipped": null, "compression": null, "compression_level": null, @@ -153,6 +165,8 @@ "description": "", "pack_format": 0, "filter": null, + "supported_formats": null, + "overlays": null, "zipped": null, "compression": null, "compression_level": null, @@ -164,6 +178,8 @@ "description": "", "pack_format": 0, "filter": null, + "supported_formats": null, + "overlays": null, "zipped": null, "compression": null, "compression_level": null, diff --git a/tests/snapshots/examples__build_load_overlay__0.data_pack/pack.mcmeta b/tests/snapshots/examples__build_load_overlay__0.data_pack/pack.mcmeta index 9b525fba..1aa5c97c 100644 --- a/tests/snapshots/examples__build_load_overlay__0.data_pack/pack.mcmeta +++ b/tests/snapshots/examples__build_load_overlay__0.data_pack/pack.mcmeta @@ -1,7 +1,12 @@ { "pack": { - "pack_format": 18, - "description": "" + "pack_format": 20, + "description": "", + "supported_formats": [ + 18, + 19, + 20 + ] }, "overlays": { "entries": [ @@ -22,6 +27,13 @@ { "formats": 19, "directory": "overlay_missing" + }, + { + "formats": { + "min_inclusive": 18, + "max_inclusive": 19 + }, + "directory": "creeper" } ] } diff --git a/tests/test_data_pack.py b/tests/test_data_pack.py index 5cc47cc2..797e6dd0 100644 --- a/tests/test_data_pack.py +++ b/tests/test_data_pack.py @@ -701,3 +701,46 @@ def test_overlay(): f2 = set(select_files(p, match="*")) assert f1 == f2 assert len(f1) == 6 + + +def test_merge_overlays(): + m = Mcmeta() + + m.merge(Mcmeta({"overlays": {"entries": [{"directory": "a", "formats": 18}]}})) + assert m.data == {"overlays": {"entries": [{"directory": "a", "formats": 18}]}} + + m.merge(Mcmeta({"overlays": {"entries": [{"directory": "b", "formats": 19}]}})) + assert m.data == { + "overlays": { + "entries": [ + {"directory": "a", "formats": 18}, + {"directory": "b", "formats": 19}, + ] + } + } + + m.merge( + Mcmeta( + { + "overlays": { + "entries": [ + { + "directory": "a", + "formats": {"min_inclusive": 18, "max_inclusive": 19}, + } + ] + } + } + ) + ) + assert m.data == { + "overlays": { + "entries": [ + { + "directory": "a", + "formats": {"min_inclusive": 18, "max_inclusive": 19}, + }, + {"directory": "b", "formats": 19}, + ] + } + }