From: Radek Czajka Date: Mon, 20 Aug 2012 10:29:17 +0000 (+0200) Subject: added WLDocument.as_cover: treat covers more like ebooks X-Git-Tag: 1.7~148 X-Git-Url: https://git.mdrn.pl/librarian.git/commitdiff_plain/cbe81ee35b07783b4f52c3d3dda83db7aaf82d34?hp=94a0753b79228b516a994934f5dfbc3a9ce03d6f added WLDocument.as_cover: treat covers more like ebooks --- diff --git a/librarian/book2anything.py b/librarian/book2anything.py index 7660ec7..b8b8d27 100755 --- a/librarian/book2anything.py +++ b/librarian/book2anything.py @@ -99,7 +99,9 @@ class Book2Anything(object): # Add cover support, if any. if cls.uses_cover: if options.image_cache: - transform_args['cover'] = lambda x: WLCover(x, image_cache = options.image_cache) + def cover_class(*args, **kwargs): + return WLCover(image_cache=options.image_cache, *args, **kwargs) + transform_args['cover'] = cover_class elif not cls.cover_optional or options.with_cover: transform_args['cover'] = WLCover diff --git a/librarian/cover.py b/librarian/cover.py index 02d76f9..a2de837 100644 --- a/librarian/cover.py +++ b/librarian/cover.py @@ -5,7 +5,8 @@ # import re import Image, ImageFont, ImageDraw, ImageFilter -from librarian import get_resource +from StringIO import StringIO +from librarian import get_resource, OutputFile class TextBox(object): @@ -119,9 +120,11 @@ class Cover(object): 'PNG': 'image/png', } - def __init__(self, book_info): + def __init__(self, book_info, format=None): self.author = ", ".join(auth.readable() for auth in book_info.authors) self.title = book_info.title + if format is not None: + self.format = format def pretty_author(self): """Allows for decorating author's name.""" @@ -180,6 +183,11 @@ class Cover(object): def save(self, *args, **kwargs): return self.image().save(format=self.format, *args, **kwargs) + def output_file(self, *args, **kwargs): + imgstr = StringIO() + self.save(imgstr, *args, **kwargs) + return OutputFile.from_string(imgstr.getvalue()) + class WLCover(Cover): """Default Wolne Lektury cover generator.""" @@ -212,8 +220,8 @@ class WLCover(Cover): u'Współczesność': '#06393d', } - def __init__(self, book_info, image_cache=None): - super(WLCover, self).__init__(book_info) + def __init__(self, book_info, format=None, image_cache=None): + super(WLCover, self).__init__(book_info, format=format) self.kind = book_info.kind self.epoch = book_info.epoch if book_info.cover_url: diff --git a/librarian/parser.py b/librarian/parser.py index e605dd9..a9e8c65 100644 --- a/librarian/parser.py +++ b/librarian/parser.py @@ -5,6 +5,7 @@ # from librarian import ValidationError, NoDublinCore, ParseError, NoProvider from librarian import RDFNS +from librarian.cover import WLCover from librarian import dcparser from xml.parsers.expat import ExpatError @@ -205,6 +206,11 @@ class WLDocument(object): from librarian import fb2 return fb2.transform(self, *args, **kwargs) + def as_cover(self, cover_class=None, *args, **kwargs): + if cover_class is None: + cover_class = WLCover + return cover_class(self.book_info, *args, **kwargs).output_file() + def save_output_file(self, output_file, output_path=None, output_dir_path=None, make_author_dir=False, ext=None): if output_dir_path: diff --git a/scripts/book2cover b/scripts/book2cover index ae11e60..3cc0ed7 100755 --- a/scripts/book2cover +++ b/scripts/book2cover @@ -17,9 +17,7 @@ class Book2Cover(Book2Anything): @staticmethod def transform(wldoc, cover): - output = StringIO() - cover(wldoc.book_info).save(output) - return OutputFile.from_string(output.getvalue()) + return wldoc.as_cover(cover_class=cover) if __name__ == '__main__':