From 9fa6d09a7fbe57f6ba602df7fbe44f27b6068317 Mon Sep 17 00:00:00 2001 From: eric Date: Wed, 5 Aug 2026 12:23:50 -0400 Subject: [PATCH] Cover.draw now decide on image based on the dc object Cover.draw now decide on image based on the dc object --- CHANGES | 2 ++ libgutenberg/Cover.py | 49 ++++++++++++++++++++++---------- libgutenberg/tests/test_cover.py | 3 +- 3 files changed, 38 insertions(+), 16 deletions(-) diff --git a/CHANGES b/CHANGES index 42b7a13..93c4cd4 100644 --- a/CHANGES +++ b/CHANGES @@ -1,4 +1,6 @@ CHANGES +0.11.2 (August ?, 2026) +- Ebookmaker expected that Cover.draw would use the dc object to determine whether an audio or music icon was needed, but Cover.draw expected an explicit parameter. Now it does what Ebookmaker was expecting. 0.11.1 (August 3, 2026) - New field `book_wikipedia_url(s)` has been added to the DublinCore dispatcher. diff --git a/libgutenberg/Cover.py b/libgutenberg/Cover.py index 887002b..ac4fe40 100755 --- a/libgutenberg/Cover.py +++ b/libgutenberg/Cover.py @@ -37,16 +37,25 @@ # cairo_is_ok = hasattr(Cover, 'cairo') import cairocffi as cairo +from .GutenbergGlobals import Struct + PY2 = sys.version_info[0] == 2 if PY2: FileNotFoundError = IOError +# Emoji names AUDIOBOOK = 1 MUSIC = 2 + # # Private helper functions. # +def cat_struct(pk): + struct = Struct() + struct.pk = pk + struct.category = '' + return [struct] def _join(s, tail): """ @@ -301,22 +310,28 @@ def _clip(value, lower, upper): # an Image instance which is a composition of different Cairo functionality. # -def draw(dc, cover_width=400, cover_height=600, branding="Project Gutenberg", audio=None): +def draw(dc, cover_width=400, cover_height=600, branding="Project Gutenberg"): """ Main drawing function, which generates a cover of the given dimension and renders title, author, and graphics. - - Args: - audio (int, optional): Display icon on cover. Defaults to None. - None = none - 1 = audiobook (speaker) - 2 = music (notes) + + title, author, and category (i.e. audio, music) are passed in dc """ # pull cover strings from DublinCore object title = dc.title_no_subtitle subtitle = dc.subtitle author = dc.authors_short() + audio = 0 + # categories is a list of (pk, label) + for cat in dc.categories: + if cat.pk in {1, 2, 6}: + audio = AUDIOBOOK + break + elif cat.pk in {3, 4}: + audio = MUSIC + break + # Based on some initial constants and the title+author strings, generate a base # background color and a shape color to draw onto the background. Try to keep @@ -678,7 +693,7 @@ def main(): from libgutenberg.DublinCore import DublinCore # Helper function. - def _draw_and_save(title, subtitle, author, filename, audio=None): + def _draw_and_save(title, subtitle, author, filename, categories): """ Draw a cover and write it to a file. Note that only PNG is supported. """ @@ -687,8 +702,9 @@ def _draw_and_save(title, subtitle, author, filename, audio=None): dc_instance.add_author(a) dc_instance.title = title dc_instance.subtitle = subtitle + dc_instance.categories = categories - cover_image = draw(dc_instance, audio=audio) + cover_image = draw(dc_instance) if filename == "-": assert not "Implement." else: @@ -731,15 +747,17 @@ def _draw_and_save(title, subtitle, author, filename, audio=None): data = json.loads(line) print("Generating cover for " + data["identifier"]) if args.audio: - data["audio"] = AUDIOBOOK + data["categories"] = cat_struct(1) elif args.music: - data["audio"] = MUSIC + data["categories"] = cat_struct(3) + else: + data["categories"] = [] status = _draw_and_save( data["title"], data["subtitle"], data["authors"], data["filename"], - data.get("audio", None), + data["categories"], ) if status: print("Error generating book cover image, skipping") @@ -756,11 +774,12 @@ def _draw_and_save(title, subtitle, author, filename, audio=None): elif not args.outfile: print("No outfile specified, exiting") else: - audio = None + # fake db row + audio = [] = None if args.audio: - audio = AUDIOBOOK + audio = cat_struct(1) elif args.music: - audio = MUSIC + audio = cat_struct(3) return _draw_and_save(args.title, args.subtitle, args.author, args.outfile, audio) return 1 diff --git a/libgutenberg/tests/test_cover.py b/libgutenberg/tests/test_cover.py index 9bbcedf..433ab68 100755 --- a/libgutenberg/tests/test_cover.py +++ b/libgutenberg/tests/test_cover.py @@ -18,10 +18,11 @@ def setUp(self): self.dc.add_author("Duck, Donald") self.dc.add_author("Mickey Mouse") self.dc.title = "A truly amazing book: (但不是那么神奇)" + self.dc.categories = Cover.cat_struct(1) def test_cover(self): try: - cover_image = Cover.draw(self.dc, audio=1) + cover_image = Cover.draw(self.dc) with open(self.test_path, 'wb+') as cover: cover_image.save(cover) self.assertTrue(os.path.exists(self.test_path))