From e167dbee2b213e6dd650eaa9aeb5afc99d0e0bb7 Mon Sep 17 00:00:00 2001 From: "B. Stack" Date: Fri, 12 Jul 2024 13:24:34 -0400 Subject: fully implement separate images --- library_info_cli.py | 57 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 32 insertions(+), 25 deletions(-) diff --git a/library_info_cli.py b/library_info_cli.py index e2aeaca..1de3cf3 100755 --- a/library_info_cli.py +++ b/library_info_cli.py @@ -33,6 +33,36 @@ def serialize(item): eprint(f"WARNING: unknown type {type(item)} for json-serializing object {item}") return item +def html_td_img(i, imagepath, imagerelativepath): + """ + Given the item, imagepath, and imagerelativepath, prepare the table data object in html. + It is possible for imagepath and imagerelativepath to be None, in which case the image will be stored inline in the html. + """ + if "img" in i: + if imagepath and imagerelativepath: + ext = i["img_type"].split("/")[-1] + img_contents = base64.b64decode(i["img"]) + # use chars -25 to -5 (so 20 characters long, 5 from the right end) + # to avoid the == at the end + # also img50 was annoying. + filename = f"{i['img'][-25:-5:]}.{ext}".replace("/","_") + filepath = os.path.join(imagepath, filename) + relativefilepath = os.path.join(imagerelativepath, filename) + if debuglevel >= 3: + eprint(f"Writing to file {filepath} for {i['title']}") + with open(filepath,"wb") as w: + w.write(img_contents) + return f"" + else: + # Just print it inline in the html. + img_src = i["img"] + return "" + else: + # No image available. This might happen? + return "none" + # failsafe + return "none" + def html(checkouts, reservations, imagepath, imagerelativepath): """ Make a pretty html page of the items. If imagepath and imagerelativepath are defined, save the images to imagepath, and set the html img tags src attribute to the imagerelativepath. @@ -78,26 +108,7 @@ def html(checkouts, reservations, imagepath, imagerelativepath): due = i["due"].strftime("%F") prn(f"{due}") prn(f"{i['format']}") - if "img" in i: - img_src = i["img"] - if imagepath and imagerelativepath: - ext = i["img_type"].split("/")[-1] - img_contents = base64.b64decode(i["img"]) - # use chars -25 to -5 (so 20 characters long, 5 from the right end) - # to avoid the == at the end - # also img50 was annoying. - filename = f"{i['img'][-25:-5:]}.{ext}".replace("/","_") - filepath = os.path.join(imagepath, filename) - relativefilepath = os.path.join(imagerelativepath, filename) - with open(filepath,"wb") as w: - if debuglevel >= 3: - eprint(f"Writing to file {filepath} for {i['title']}") - w.write(img_contents) - prn(f"") - else: - prn(f"") - else: - prn(f"none") + prn(html_td_img(i, imagepath, imagerelativepath)) prn(f"{i['title']}") prn(f"\n") prn(f"\n") @@ -116,11 +127,7 @@ def html(checkouts, reservations, imagepath, imagerelativepath): prn(f"{i['location']}") prn(f"{i['position']}") prn(f"{i['status']}") - if "img" in i: - img_src = i["img"] - else: - img_src = "" - prn(f"") + prn(html_td_img(i, imagepath, imagerelativepath)) prn(f"{i['title']}") prn(f"\n") prn(f"\n") -- cgit