Skip to content

Commit

Permalink
feat: add pack copy
Browse files Browse the repository at this point in the history
  • Loading branch information
vberlier committed Feb 16, 2024
1 parent 01ffc4e commit 8f380d5
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
23 changes: 23 additions & 0 deletions beet/library/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ def ensure_deserialized(
@classmethod
def default(cls) -> Any: ...

def copy(self: T) -> T: ...

@classmethod
def load(cls: Type[T], origin: FileOrigin, path: FileSystemPath) -> T: ...

Expand Down Expand Up @@ -1151,8 +1153,29 @@ def merge(

return True

def copy(self, *, shallow: bool = False) -> Self:
pack_copy = type(self)().configure(self)

for key, f in self.extra.items():
pack_copy.extra[key] = f if shallow else f.copy()

if self.overlay_parent is None:
for key, value in self.overlays.items():
pack_copy.overlays[key] = value.copy(shallow=shallow)

for key, namespace in self.items():
namespace_copy = pack_copy[key]
for key, f in namespace.extra.items():
namespace_copy.extra[key] = f if shallow else f.copy()
for key, f in namespace.content:
namespace_copy[key] = f if shallow else f.copy()

return pack_copy

def clear(self):
self.extra.clear()
if self.overlay_parent is None:
self.overlays.clear()
super().clear()
if not self.pack_format:
self.pack_format = self.latest_pack_format
Expand Down
45 changes: 45 additions & 0 deletions tests/test_data_pack.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
PackQuery,
Structure,
)
from beet.core.file import TextFile


def test_equality():
Expand Down Expand Up @@ -799,3 +800,47 @@ def test_merge_overlays():
]
}
}


def test_copy():
p = DataPack()
p.extra["thing.txt"] = TextFile("wow")
p.overlays["a"]["demo:foo"] = Function(["say 1"])
p["demo:foo"] = Function(["say 2"])
p["demo"].extra["dank.txt"] = TextFile("ok")

p_copy = p.copy()
assert p_copy == p
assert p_copy.extra["thing.txt"] is not p.extra["thing.txt"]
assert (
p_copy.overlays["a"].functions["demo:foo"]
is not p.overlays["a"].functions["demo:foo"]
)
assert p_copy.functions["demo:foo"] is not p.functions["demo:foo"]
assert p_copy["demo"].extra["dank.txt"] is not p["demo"].extra["dank.txt"]

p.clear()
assert not p

assert p_copy.extra["thing.txt"] == TextFile("wow")
assert p_copy.overlays["a"].functions["demo:foo"] == Function(["say 1"])
assert p_copy.functions["demo:foo"] == Function(["say 2"])
assert p_copy["demo"].extra["dank.txt"] == TextFile("ok")

p_shallow = p_copy.copy(shallow=True)
assert p_shallow == p_copy
assert p_shallow.extra["thing.txt"] is p_copy.extra["thing.txt"]
assert (
p_shallow.overlays["a"].functions["demo:foo"]
is p_copy.overlays["a"].functions["demo:foo"]
)
assert p_shallow.functions["demo:foo"] is p_copy.functions["demo:foo"]
assert p_shallow["demo"].extra["dank.txt"] is p_copy["demo"].extra["dank.txt"]

p_copy.clear()
assert not p_copy

assert p_shallow.extra["thing.txt"] == TextFile("wow")
assert p_shallow.overlays["a"].functions["demo:foo"] == Function(["say 1"])
assert p_shallow.functions["demo:foo"] == Function(["say 2"])
assert p_shallow["demo"].extra["dank.txt"] == TextFile("ok")

0 comments on commit 8f380d5

Please sign in to comment.