Skip to content

Commit

Permalink
feat: add File.copy()
Browse files Browse the repository at this point in the history
  • Loading branch information
vberlier committed Oct 1, 2022
1 parent f7fd2d5 commit d1f5b36
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
5 changes: 5 additions & 0 deletions beet/core/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import io
import json
import shutil
from copy import deepcopy
from dataclasses import dataclass, replace
from pathlib import Path
from typing import Any, Callable, ClassVar, Generic, Optional, Type, TypeVar, Union
Expand Down Expand Up @@ -195,6 +196,10 @@ def default(cls) -> ValueType:
"either a value, serialized data, or a source path."
)

def copy(self: FileType) -> FileType:
"""Copy the file."""
return replace(self, _content=deepcopy(self._content))

def serialize(self, content: Union[ValueType, SerializeType]) -> SerializeType:
"""Serialize file content."""
raise NotImplementedError()
Expand Down
5 changes: 5 additions & 0 deletions tests/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,8 @@ def test_model(data: str):
ab = ABFile(data)
ab.ensure_deserialized()
assert json.loads(data) == json.loads(ab.text)


def test_copy():
assert TextFile(source_path="foo.txt").copy().source_path == "foo.txt"
assert TextFile("hello").copy().text == "hello"

0 comments on commit d1f5b36

Please sign in to comment.