Skip to content

Commit

Permalink
fix: overload merge to automatically merge pack extras too
Browse files Browse the repository at this point in the history
  • Loading branch information
vberlier committed Feb 20, 2021
1 parent 4216352 commit a586ee3
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 7 deletions.
27 changes: 26 additions & 1 deletion beet/library/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
Iterator,
List,
Mapping,
MutableMapping,
Optional,
Protocol,
Tuple,
Expand All @@ -40,7 +41,14 @@
)
from zipfile import ZipFile

from beet.core.container import Container, ContainerProxy, MatchMixin, MergeMixin, Pin
from beet.core.container import (
Container,
ContainerProxy,
MatchMixin,
MergeMixin,
Pin,
SupportsMerge,
)
from beet.core.file import File, FileOrigin, JsonFile, PngFile
from beet.core.utils import FileSystemPath, JsonDict, TextComponent, extra_field

Expand All @@ -50,6 +58,7 @@
PackFileType = TypeVar("PackFileType", bound="PackFile")
NamespaceType = TypeVar("NamespaceType", bound="Namespace")
NamespaceFileType = TypeVar("NamespaceFileType", bound="NamespaceFile")
MergeableType = TypeVar("MergeableType", bound=SupportsMerge)

PackFile = File[Any, Any]

Expand Down Expand Up @@ -364,6 +373,22 @@ def process(self, key: str, value: NamespaceType) -> NamespaceType:
def missing(self, key: str) -> NamespaceType:
return self.namespace_type()

@overload
def merge(self: T, other: T) -> bool:
...

@overload
def merge(
self: MutableMapping[T, MergeableType], other: Mapping[T, MergeableType]
) -> bool:
...

def merge(self, other: Any) -> bool:
super().merge(other) # type: ignore
if isinstance(other, Pack):
self.extra.merge(other.extra)
return True

@property
def content(self) -> Iterator[Tuple[str, NamespaceFile]]:
"""Iterator that yields all the files stored in the pack."""
Expand Down
3 changes: 0 additions & 3 deletions beet/toolchain/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,7 @@ def plugin(ctx: Context):
with child_ctx.activate() as pipeline:
pipeline.run(specs)

ctx.assets.extra.merge(child_ctx.assets.extra)
ctx.assets.merge(child_ctx.assets)

ctx.data.extra.merge(child_ctx.data.extra)
ctx.data.merge(child_ctx.data)

return plugin
3 changes: 0 additions & 3 deletions beet/toolchain/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,5 @@ def __call__(self, ctx: Context):
if child_ctx.output_directory:
return

ctx.assets.extra.merge(child_ctx.assets.extra)
ctx.assets.merge(child_ctx.assets)

ctx.data.extra.merge(child_ctx.data.extra)
ctx.data.merge(child_ctx.data)
7 changes: 7 additions & 0 deletions tests/test_data_pack.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,13 @@ def test_merge_block_tags():
}


def test_merge_extra():
pack = DataPack()
pack.merge(DataPack(description="hello", pack_format=88))
assert pack.description == "hello"
assert pack.pack_format == 88


def test_match():
pack = DataPack()
custom = pack["custom"]
Expand Down

0 comments on commit a586ee3

Please sign in to comment.