Code cleanup

This commit is contained in:
Joel Collins 2020-10-14 14:56:29 +00:00
parent 2bfb988460
commit 994e83dbeb
46 changed files with 261 additions and 318 deletions

View file

@ -39,7 +39,7 @@ def read_exif_from_file(filename):
head = data[2:6]
HEAD_LENGTH = 4
exif = None
while 1:
while len(head) == HEAD_LENGTH:
length = struct.unpack(">H", head[2:4])[0]
if head[:2] == b"\xff\xe1":

View file

@ -2,8 +2,8 @@ import copy
import numbers
import struct
from ._common import *
from ._exif import *
from ._common import split_into_segments
from ._exif import ExifIFD, ImageIFD, TAGS, TYPES
TIFF_HEADER_LENGTH = 8
@ -243,11 +243,11 @@ def _value_to_bytes(raw_value, value_type, offset):
elif value_type == TYPES.Ascii:
try:
new_value = raw_value.encode("latin1") + b"\x00"
except:
except: # pylint: disable=W0702
try:
new_value = raw_value + b"\x00"
except TypeError:
raise ValueError("Got invalid type to convert.")
except TypeError as e:
raise ValueError("Got invalid type to convert.") from e
length = len(new_value)
if length > 4:
value_str = struct.pack(">I", offset)
@ -262,7 +262,7 @@ def _value_to_bytes(raw_value, value_type, offset):
elif isinstance(raw_value[0], tuple):
length = len(raw_value)
new_value = b""
for n, val in enumerate(raw_value):
for _, val in enumerate(raw_value):
num, den = val
new_value += struct.pack(">L", num) + struct.pack(">L", den)
value_str = struct.pack(">I", offset)
@ -275,7 +275,7 @@ def _value_to_bytes(raw_value, value_type, offset):
elif isinstance(raw_value[0], tuple):
length = len(raw_value)
new_value = b""
for n, val in enumerate(raw_value):
for _, val in enumerate(raw_value):
num, den = val
new_value += struct.pack(">l", num) + struct.pack(">l", den)
value_str = struct.pack(">I", offset)
@ -286,13 +286,13 @@ def _value_to_bytes(raw_value, value_type, offset):
value_str = struct.pack(">I", offset)
try:
four_bytes_over = b"" + raw_value
except TypeError:
raise ValueError("Got invalid type to convert.")
except TypeError as e:
raise ValueError("Got invalid type to convert.") from e
else:
try:
value_str = raw_value + b"\x00" * (4 - length)
except TypeError:
raise ValueError("Got invalid type to convert.")
except TypeError as e:
raise ValueError("Got invalid type to convert.") from e
elif value_type == TYPES.SByte: # Signed Byte
length = len(raw_value)
if length <= 4:
@ -333,7 +333,7 @@ def _dict_to_bytes(ifd_dict, ifd, ifd_offset):
entries = b""
values = b""
for n, key in enumerate(sorted(ifd_dict)):
for _, key in enumerate(sorted(ifd_dict)):
if (ifd == "0th") and (key in (ImageIFD.ExifTag, ImageIFD.GPSTag)):
continue
elif (ifd == "Exif") and (key == ExifIFD.InteroperabilityTag):
@ -358,11 +358,11 @@ def _dict_to_bytes(ifd_dict, ifd, ifd_offset):
length_str, value_str, four_bytes_over = _value_to_bytes(
raw_value, value_type, offset
)
except ValueError:
except ValueError as e:
raise ValueError(
'"dump" got wrong type of exif value.\n'
+ "{0} in {1} IFD. Got as {2}.".format(key, ifd, type(ifd_dict[key]))
)
) from e
entries += key_str + type_str + length_str + value_str
values += four_bytes_over

View file

@ -3,7 +3,7 @@ import struct
import sys
from . import _webp
from ._common import *
from ._common import merge_segments, split_into_segments
from ._exceptions import InvalidImageDataError

View file

@ -2,9 +2,9 @@ import struct
import sys
from . import _webp
from ._common import *
from ._common import get_exif_seg, read_exif_from_file, split_into_segments
from ._exceptions import InvalidImageDataError
from ._exif import *
from ._exif import ExifIFD, ImageIFD, TAGS, TYPES
LITTLE_ENDIAN = b"\x49\x49"
@ -109,6 +109,8 @@ class _ExifReader(object):
else:
raise InvalidImageDataError("Given file is neither JPEG nor TIFF.")
self.endian_mark = None
def get_ifd_dict(self, pointer, ifd_name, read_unknown=False):
ifd_dict = {}
tag_count = struct.unpack(

View file

@ -1,7 +1,7 @@
import io
from . import _webp
from ._common import *
from ._common import get_exif_seg, split_into_segments
def remove(src, new_file=None):
@ -42,7 +42,7 @@ def remove(src, new_file=None):
new_data = src_data
except Exception as e:
print(e)
raise ValueError("Error occurred.")
raise ValueError("Error occurred.") from e
if isinstance(new_file, io.BytesIO):
new_file.write(new_data)

View file

@ -1,6 +1,6 @@
import io
from ._common import *
from ._common import get_exif_seg, merge_segments, split_into_segments
def transplant(exif_src, image, new_file=None):

View file

@ -204,8 +204,6 @@ def get_exif(data):
CHUNK_FOURCC_LENGTH = 4
LENGTH_BYTES_LENGTH = 4
chunks = []
exif = b""
while pointer < file_size:
fourcc = data[pointer : pointer + CHUNK_FOURCC_LENGTH]
pointer += CHUNK_FOURCC_LENGTH

View file

@ -46,8 +46,8 @@ class UserComment:
cls._JIS_PREFIX: cls._JIS,
cls._UNICODE_PREFIX: cls._UNICODE,
}[prefix]
except KeyError:
raise ValueError("unable to determine appropriate encoding")
except KeyError as e:
raise ValueError("unable to determine appropriate encoding") from e
return body.decode(encoding, errors="replace")
@classmethod