convert load_* functions to pure functions with no state

This commit is contained in:
wiredfool 2015-09-13 11:07:43 +01:00
parent 156972874d
commit a9396ab412

View File

@ -319,7 +319,7 @@ class ImageFileDirectory_v2(collections.MutableMapping):
data = self._tagdata[tag] data = self._tagdata[tag]
typ = self.tagtype[tag] typ = self.tagtype[tag]
size, handler = self._load_dispatch[typ] size, handler = self._load_dispatch[typ]
self[tag] = handler(self, data) # check type self[tag] = handler(self, self.legacy_api, data) # check type
val = self._tags[tag] val = self._tags[tag]
if self.legacy_api and not isinstance(val, (tuple, bytes)): if self.legacy_api and not isinstance(val, (tuple, bytes)):
val = val, val = val,
@ -404,7 +404,7 @@ class ImageFileDirectory_v2(collections.MutableMapping):
idx, fmt, name = idx_fmt_name idx, fmt, name = idx_fmt_name
TYPES[idx] = name TYPES[idx] = name
size = struct.calcsize("=" + fmt) size = struct.calcsize("=" + fmt)
_load_dispatch[idx] = size, lambda self, data: ( _load_dispatch[idx] = size, lambda self, legacy_api, data: (
self._unpack("{0}{1}".format(len(data) // size, fmt), data)) self._unpack("{0}{1}".format(len(data) // size, fmt), data))
_write_dispatch[idx] = lambda self, *values: ( _write_dispatch[idx] = lambda self, *values: (
b"".join(self._pack(fmt, value) for value in values)) b"".join(self._pack(fmt, value) for value in values))
@ -415,8 +415,8 @@ class ImageFileDirectory_v2(collections.MutableMapping):
(9, "l", "signed long"), (11, "f", "float"), (12, "d", "double")])) (9, "l", "signed long"), (11, "f", "float"), (12, "d", "double")]))
@_register_loader(1, 1) # Basic type, except for the legacy API. @_register_loader(1, 1) # Basic type, except for the legacy API.
def load_byte(self, data): def load_byte(self, legacy_api, data):
return (data if self.legacy_api else return (data if legacy_api else
tuple(map(ord, data) if bytes is str else data)) tuple(map(ord, data) if bytes is str else data))
@_register_writer(1) # Basic type, except for the legacy API. @_register_writer(1) # Basic type, except for the legacy API.
@ -424,7 +424,7 @@ class ImageFileDirectory_v2(collections.MutableMapping):
return data return data
@_register_loader(2, 1) @_register_loader(2, 1)
def load_string(self, data): def load_string(self, legacy_api, data):
if data.endswith(b"\0"): if data.endswith(b"\0"):
data = data[:-1] data = data[:-1]
return data.decode("latin-1", "replace") return data.decode("latin-1", "replace")
@ -437,9 +437,9 @@ class ImageFileDirectory_v2(collections.MutableMapping):
return b"" + value.encode('ascii', 'replace') + b"\0" return b"" + value.encode('ascii', 'replace') + b"\0"
@_register_loader(5, 8) @_register_loader(5, 8)
def load_rational(self, data): def load_rational(self, legacy_api, data):
vals = self._unpack("{0}L".format(len(data) // 4), data) vals = self._unpack("{0}L".format(len(data) // 4), data)
combine = lambda a, b: (a, b) if self.legacy_api else a / b combine = lambda a, b: (a, b) if legacy_api else a / b
return tuple(combine(num, denom) return tuple(combine(num, denom)
for num, denom in zip(vals[::2], vals[1::2])) for num, denom in zip(vals[::2], vals[1::2]))
@ -449,7 +449,7 @@ class ImageFileDirectory_v2(collections.MutableMapping):
for frac in values) for frac in values)
@_register_loader(7, 1) @_register_loader(7, 1)
def load_undefined(self, data): def load_undefined(self, legacy_api, data):
return data return data
@_register_writer(7) @_register_writer(7)
@ -457,9 +457,9 @@ class ImageFileDirectory_v2(collections.MutableMapping):
return value return value
@_register_loader(10, 8) @_register_loader(10, 8)
def load_signed_rational(self, data): def load_signed_rational(self, legacy_api, data):
vals = self._unpack("{0}l".format(len(data) // 4), data) vals = self._unpack("{0}l".format(len(data) // 4), data)
combine = lambda a, b: (a, b) if self.legacy_api else a / b combine = lambda a, b: (a, b) if legacy_api else a / b
return tuple(combine(num, denom) return tuple(combine(num, denom)
for num, denom in zip(vals[::2], vals[1::2])) for num, denom in zip(vals[::2], vals[1::2]))