mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-01-28 02:04:36 +03:00
py3k: Rewrite dictionary support for Tiff ImageFileDictionary
This commit brings in the collections.MutableMapping mixin to provide full dictionary support for ImageFileDictionary.
This commit is contained in:
parent
09f1081c95
commit
e514912378
|
@ -45,6 +45,8 @@ import Image, ImageFile
|
||||||
import ImagePalette
|
import ImagePalette
|
||||||
|
|
||||||
import array, sys
|
import array, sys
|
||||||
|
import collections
|
||||||
|
import itertools
|
||||||
|
|
||||||
II = "II" # little-endian (intel-style)
|
II = "II" # little-endian (intel-style)
|
||||||
MM = "MM" # big-endian (motorola-style)
|
MM = "MM" # big-endian (motorola-style)
|
||||||
|
@ -204,7 +206,7 @@ def _accept(prefix):
|
||||||
##
|
##
|
||||||
# Wrapper for TIFF IFDs.
|
# Wrapper for TIFF IFDs.
|
||||||
|
|
||||||
class ImageFileDirectory:
|
class ImageFileDirectory(collections.MutableMapping):
|
||||||
|
|
||||||
# represents a TIFF tag directory. to speed things up,
|
# represents a TIFF tag directory. to speed things up,
|
||||||
# we don't decode tags unless they're asked for.
|
# we don't decode tags unless they're asked for.
|
||||||
|
@ -227,16 +229,7 @@ class ImageFileDirectory:
|
||||||
self.tagtype = {} # added 2008-06-05 by Florian Hoech
|
self.tagtype = {} # added 2008-06-05 by Florian Hoech
|
||||||
self.next = None
|
self.next = None
|
||||||
|
|
||||||
# dictionary API (sort of)
|
# dictionary API
|
||||||
|
|
||||||
def keys(self):
|
|
||||||
return self.tagdata.keys() + self.tags.keys()
|
|
||||||
|
|
||||||
def items(self):
|
|
||||||
items = self.tags.items()
|
|
||||||
for tag in self.tagdata.keys():
|
|
||||||
items.append((tag, self[tag]))
|
|
||||||
return items
|
|
||||||
|
|
||||||
def __len__(self):
|
def __len__(self):
|
||||||
return len(self.tagdata) + len(self.tags)
|
return len(self.tagdata) + len(self.tags)
|
||||||
|
@ -251,12 +244,6 @@ class ImageFileDirectory:
|
||||||
del self.tagdata[tag]
|
del self.tagdata[tag]
|
||||||
return data
|
return data
|
||||||
|
|
||||||
def get(self, tag, default=None):
|
|
||||||
try:
|
|
||||||
return self[tag]
|
|
||||||
except KeyError:
|
|
||||||
return default
|
|
||||||
|
|
||||||
def getscalar(self, tag, default=None):
|
def getscalar(self, tag, default=None):
|
||||||
try:
|
try:
|
||||||
value = self[tag]
|
value = self[tag]
|
||||||
|
@ -272,14 +259,24 @@ class ImageFileDirectory:
|
||||||
raise
|
raise
|
||||||
return default
|
return default
|
||||||
|
|
||||||
def has_key(self, tag):
|
def __contains__(self, tag):
|
||||||
return self.tags.has_key(tag) or self.tagdata.has_key(tag)
|
return tag in self.tags or tag in self.tagdata
|
||||||
|
|
||||||
|
if sys.version_info < (3,0):
|
||||||
|
def has_key(self, tag):
|
||||||
|
return tag in self
|
||||||
|
|
||||||
def __setitem__(self, tag, value):
|
def __setitem__(self, tag, value):
|
||||||
if type(value) is not type(()):
|
if not isinstance(value, tuple):
|
||||||
value = (value,)
|
value = (value,)
|
||||||
self.tags[tag] = value
|
self.tags[tag] = value
|
||||||
|
|
||||||
|
def __delitem__(self, tag):
|
||||||
|
self.tags.pop(tag, self.tagdata.pop(tag, None))
|
||||||
|
|
||||||
|
def __iter__(self):
|
||||||
|
return itertools.chain(self.tags.__iter__(), self.tagdata.__iter__())
|
||||||
|
|
||||||
# load primitives
|
# load primitives
|
||||||
|
|
||||||
load_dispatch = {}
|
load_dispatch = {}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user