avoid match/case for now until python 3.10 is available on more platforms

This commit is contained in:
Johannes Schauer Marin Rodrigues 2022-06-28 14:21:01 +01:00 committed by Johannes Schauer Marin Rodrigues
parent bad6fcae39
commit af6fe27d53

View file

@ -1536,7 +1536,6 @@ def parse_png(rawdata):
return pngidat, palette return pngidat, palette
miff_re = re.compile( miff_re = re.compile(
r""" r"""
[^\x00-\x20\x7f-\x9f] # the field name must not start with a control char or space [^\x00-\x20\x7f-\x9f] # the field name must not start with a control char or space
@ -1550,6 +1549,9 @@ miff_re = re.compile(
) )
# https://imagemagick.org/script/miff.php # https://imagemagick.org/script/miff.php
# turn off black formatting until python 3.10 is available on more platforms
# and we can use match/case
# fmt: off
def parse_miff(data): def parse_miff(data):
results = [] results = []
header, rest = data.split(b":\x1a", 1) header, rest = data.split(b":\x1a", 1)
@ -1563,63 +1565,85 @@ def parse_miff(data):
if i == 0: if i == 0:
assert k.lower() == "id" assert k.lower() == "id"
assert v.lower() == "imagemagick" assert v.lower() == "imagemagick"
match k.lower(): #match k.lower():
case "class": # case "class":
match v: if k.lower() == "class":
case "DirectClass" | "PseudoClass": #match v:
# case "DirectClass" | "PseudoClass":
if v in ["DirectClass", "PseudoClass"]:
hdata["class"] = v hdata["class"] = v
case _: # case _:
else:
print("cannot understand class", v) print("cannot understand class", v)
case "colorspace": # case "colorspace":
elif k.lower() == "colorspace":
# theoretically RGBA and CMYKA should be supported as well # theoretically RGBA and CMYKA should be supported as well
# please teach me how to create such a MIFF file # please teach me how to create such a MIFF file
match v: #match v:
case "sRGB" | "CMYK" | "Gray": # case "sRGB" | "CMYK" | "Gray":
if v in ["sRGB", "CMYK", "Gray"]:
hdata["colorspace"] = v hdata["colorspace"] = v
case _: # case _:
else:
print("cannot understand colorspace", v) print("cannot understand colorspace", v)
case "depth": # case "depth":
match v: elif k.lower() == "depth":
case "8" | "16" | "32": #match v:
# case "8" | "16" | "32":
if v in ["8", "16", "32"]:
hdata["depth"] = int(v) hdata["depth"] = int(v)
case _: # case _:
else:
print("cannot understand depth", v) print("cannot understand depth", v)
case "colors": # case "colors":
elif k.lower() == "colors":
hdata["colors"] = int(v) hdata["colors"] = int(v)
case "matte": # case "matte":
match v: elif k.lower() == "matte":
case "True": #match v:
# case "True":
if v == "True":
hdata["matte"] = True hdata["matte"] = True
case "False": # case "False":
elif v == "False":
hdata["matte"] = False hdata["matte"] = False
case _: # case _:
else:
print("cannot understand matte", v) print("cannot understand matte", v)
case "columns" | "rows": # case "columns" | "rows":
elif k.lower() in ["columns", "rows"]:
hdata[k.lower()] = int(v) hdata[k.lower()] = int(v)
case "compression": # case "compression":
elif k.lower() == "compression":
print("compression not yet supported") print("compression not yet supported")
case "profile": # case "profile":
elif k.lower() == "profile":
assert v in ["icc", "exif"] assert v in ["icc", "exif"]
hdata["profile"] = v hdata["profile"] = v
case "resolution": # case "resolution":
elif k.lower() == "resolution":
dpix, dpiy = v.split("x", 1) dpix, dpiy = v.split("x", 1)
hdata["resolution"] = (float(dpix), float(dpiy)) hdata["resolution"] = (float(dpix), float(dpiy))
assert "depth" in hdata assert "depth" in hdata
assert "columns" in hdata assert "columns" in hdata
assert "rows" in hdata assert "rows" in hdata
match hdata["class"]: #match hdata["class"]:
case "DirectClass": # case "DirectClass":
if hdata["class"] == "DirectClass":
if "colors" in hdata: if "colors" in hdata:
assert hdata["colors"] == 0 assert hdata["colors"] == 0
match hdata["colorspace"]: #match hdata["colorspace"]:
case "sRGB": # case "sRGB":
if hdata["colorspace"] == "sRGB":
numchannels = 3 numchannels = 3
colorspace = Colorspace.RGB colorspace = Colorspace.RGB
case "CMYK": # case "CMYK":
elif hdata["colorspace"] == "CMYK":
numchannels = 4 numchannels = 4
colorspace = Colorspace.CMYK colorspace = Colorspace.CMYK
case "Gray": # case "Gray":
elif hdata["colorspace"] == "Gray":
numchannels = 1 numchannels = 1
colorspace = Colorspace.L colorspace = Colorspace.L
if hdata["matte"]: if hdata["matte"]:
@ -1660,7 +1684,8 @@ def parse_miff(data):
# another image is here # another image is here
assert rest[lenimgdata:][:14].lower() == b"id=imagemagick" assert rest[lenimgdata:][:14].lower() == b"id=imagemagick"
results.extend(parse_miff(rest[lenimgdata:])) results.extend(parse_miff(rest[lenimgdata:]))
case "PseudoClass": # case "PseudoClass":
elif hdata["class"] == "PseudoClass":
assert "colors" in hdata assert "colors" in hdata
if hdata["matte"]: if hdata["matte"]:
numchannels = 2 numchannels = 2
@ -1694,6 +1719,7 @@ def parse_miff(data):
) )
results.extend(parse_miff(rest[lenpal + lenimgdata :])) results.extend(parse_miff(rest[lenpal + lenimgdata :]))
return results return results
# fmt: on
def read_images(rawdata, colorspace, first_frame_only=False, rot=None): def read_images(rawdata, colorspace, first_frame_only=False, rot=None):
@ -1879,7 +1905,6 @@ def read_images(rawdata, colorspace, first_frame_only=False, rot=None):
) )
] ]
if imgformat == ImageFormat.MIFF: if imgformat == ImageFormat.MIFF:
return parse_miff(rawdata) return parse_miff(rawdata)
@ -2517,8 +2542,8 @@ def convert(*images, **kwargs):
rawdata = f.read() rawdata = f.read()
f.close() f.close()
#md5 = hashlib.md5(rawdata).hexdigest() # md5 = hashlib.md5(rawdata).hexdigest()
#with open("./testdata/" + md5, "wb") as f: # with open("./testdata/" + md5, "wb") as f:
# f.write(rawdata) # f.write(rawdata)
for ( for (