15
15
Type ,
16
16
TypeVar ,
17
17
Union ,
18
+ cast ,
18
19
)
19
20
20
21
import attr
@@ -125,7 +126,7 @@ class Placeholder:
125
126
Tds = TypeVar ("Tds" , bound = "DataStore" )
126
127
127
128
128
- @attr .s (auto_attribs = True , slots = True , repr = False )
129
+ @attr .s (auto_attribs = True , slots = True , repr = False , eq = False )
129
130
class DataStore (MutableMapping ):
130
131
"""Represents the base class for ImageSet and DataSet.
131
132
@@ -135,10 +136,34 @@ class DataStore(MutableMapping):
135
136
136
137
_data : Dict [str , Union [bytes , Placeholder ]] = attr .ib (factory = dict )
137
138
139
+ _lazy : Optional [bool ] = attr .ib (default = False , kw_only = True , cmp = False , init = False )
138
140
_reader : Optional [UFOReader ] = attr .ib (
139
- default = None , init = False , repr = False , eq = False
141
+ default = None , init = False , repr = False , cmp = False
140
142
)
141
- _scheduledForDeletion : Set [str ] = attr .ib (factory = set , init = False , repr = False )
143
+ _scheduledForDeletion : Set [str ] = attr .ib (
144
+ factory = set , init = False , repr = False , cmp = False
145
+ )
146
+
147
+ def __eq__ (self , other : object ) -> bool :
148
+ # same as attrs-defined __eq__ method, only that it un-lazifies DataStores
149
+ # if needed.
150
+ # NOTE: Avoid isinstance check that mypy recognizes because we don't want to
151
+ # test possible Font subclasses for equality.
152
+ if other .__class__ is not self .__class__ :
153
+ return NotImplemented
154
+ other = cast (DataStore , other )
155
+
156
+ for data_store in (self , other ):
157
+ if data_store ._lazy :
158
+ data_store .unlazify ()
159
+
160
+ return self ._data == other ._data
161
+
162
+ def __ne__ (self , other : object ) -> bool :
163
+ result = self .__eq__ (other )
164
+ if result is NotImplemented :
165
+ return NotImplemented
166
+ return not result
142
167
143
168
@classmethod
144
169
def read (cls : Type [Tds ], reader : UFOReader , lazy : bool = True ) -> Tds :
@@ -149,6 +174,7 @@ def read(cls: Type[Tds], reader: UFOReader, lazy: bool = True) -> Tds:
149
174
self ._data [fileName ] = _NOT_LOADED
150
175
else :
151
176
self ._data [fileName ] = cls .read_data (reader , fileName )
177
+ self ._lazy = lazy
152
178
if lazy :
153
179
self ._reader = reader
154
180
return self
@@ -179,8 +205,11 @@ def remove_data(writer: UFOWriter, filename: str) -> None:
179
205
180
206
def unlazify (self ) -> None :
181
207
"""Load all data into memory."""
182
- for _ in self .items ():
183
- pass
208
+ if self ._lazy :
209
+ assert self ._reader is not None
210
+ for _ in self .items ():
211
+ pass
212
+ self ._lazy = False
184
213
185
214
__deepcopy__ = _deepcopy_unlazify_attrs
186
215
0 commit comments