Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unlazify data and images on comparison #165

Merged
merged 3 commits into from
Sep 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 34 additions & 5 deletions src/ufoLib2/objects/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
Type,
TypeVar,
Union,
cast,
)

import attr
Expand Down Expand Up @@ -125,7 +126,7 @@ class Placeholder:
Tds = TypeVar("Tds", bound="DataStore")


@attr.s(auto_attribs=True, slots=True, repr=False)
@attr.s(auto_attribs=True, slots=True, repr=False, eq=False)
class DataStore(MutableMapping):
"""Represents the base class for ImageSet and DataSet.

Expand All @@ -135,10 +136,34 @@ class DataStore(MutableMapping):

_data: Dict[str, Union[bytes, Placeholder]] = attr.ib(factory=dict)

_lazy: Optional[bool] = attr.ib(default=False, kw_only=True, cmp=False, init=False)
_reader: Optional[UFOReader] = attr.ib(
default=None, init=False, repr=False, eq=False
default=None, init=False, repr=False, cmp=False
)
_scheduledForDeletion: Set[str] = attr.ib(factory=set, init=False, repr=False)
_scheduledForDeletion: Set[str] = attr.ib(
factory=set, init=False, repr=False, cmp=False
)

def __eq__(self, other: object) -> bool:
# same as attrs-defined __eq__ method, only that it un-lazifies DataStores
# if needed.
# NOTE: Avoid isinstance check that mypy recognizes because we don't want to
# test possible Font subclasses for equality.
if other.__class__ is not self.__class__:
return NotImplemented
other = cast(DataStore, other)

for data_store in (self, other):
if data_store._lazy:
data_store.unlazify()

return self._data == other._data

def __ne__(self, other: object) -> bool:
result = self.__eq__(other)
if result is NotImplemented:
return NotImplemented
return not result

@classmethod
def read(cls: Type[Tds], reader: UFOReader, lazy: bool = True) -> Tds:
Expand All @@ -149,6 +174,7 @@ def read(cls: Type[Tds], reader: UFOReader, lazy: bool = True) -> Tds:
self._data[fileName] = _NOT_LOADED
else:
self._data[fileName] = cls.read_data(reader, fileName)
self._lazy = lazy
if lazy:
self._reader = reader
return self
Expand Down Expand Up @@ -179,8 +205,11 @@ def remove_data(writer: UFOWriter, filename: str) -> None:

def unlazify(self) -> None:
"""Load all data into memory."""
for _ in self.items():
pass
if self._lazy:
assert self._reader is not None
for _ in self.items():
pass
self._lazy = False

__deepcopy__ = _deepcopy_unlazify_attrs

Expand Down
Binary file added tests/data/UbuTestData.ufo/images/image1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions tests/test_ufoLib2.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,27 @@ def test_unlazify(datadir):
assert font._lazy is False


def test_auto_unlazify_font(datadir):
font1 = ufoLib2.Font.open(datadir / "UbuTestData.ufo", lazy=True)
font2 = ufoLib2.Font.open(datadir / "UbuTestData.ufo", lazy=False)

assert font1 == font2


def test_auto_unlazify_data(datadir):
font1 = ufoLib2.Font.open(datadir / "UbuTestData.ufo", lazy=True)
font2 = ufoLib2.Font.open(datadir / "UbuTestData.ufo", lazy=False)

assert font1.data == font2.data


def test_auto_unlazify_images(datadir):
font1 = ufoLib2.Font.open(datadir / "UbuTestData.ufo", lazy=True)
font2 = ufoLib2.Font.open(datadir / "UbuTestData.ufo", lazy=False)

assert font1.images == font2.images


def test_font_eq_and_ne(ufo_UbuTestData):
font1 = ufo_UbuTestData
font2 = deepcopy(font1)
Expand Down