From cffaa528f2552b1927315a18a8fa613cae22c170 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 17 Sep 2013 10:36:10 +0530 Subject: [PATCH] Metadata jackets: Use a safe formatter Metadata jackets: Do not error out when using a custom template with some custom fields that are not present. Fixes #1225357 [polish fails when adding jacket to epub](https://bugs.launchpad.net/calibre/+bug/1225357) --- src/calibre/ebooks/oeb/transforms/jacket.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/calibre/ebooks/oeb/transforms/jacket.py b/src/calibre/ebooks/oeb/transforms/jacket.py index a9cc10dc3d..bf506bfe29 100644 --- a/src/calibre/ebooks/oeb/transforms/jacket.py +++ b/src/calibre/ebooks/oeb/transforms/jacket.py @@ -8,6 +8,7 @@ __docformat__ = 'restructuredtext en' import sys, os from xml.sax.saxutils import escape +from string import Formatter from lxml import etree @@ -21,6 +22,14 @@ from calibre.ebooks.chardet import strip_encoding_declarations JACKET_XPATH = '//h:meta[@name="calibre-content" and @content="jacket"]' +class SafeFormatter(Formatter): + + def get_value(self, *args, **kwargs): + try: + return Formatter.get_value(self, *args, **kwargs) + except KeyError: + return '' + class Jacket(object): ''' Book jacket manipulation. Remove first image and insert comments at start of @@ -217,8 +226,9 @@ def render_jacket(mi, output_profile, args['_genre_label'] = args.get('_genre_label', '{_genre_label}') args['_genre'] = args.get('_genre', '{_genre}') - generated_html = P('jacket/template.xhtml', - data=True).decode('utf-8').format(**args) + formatter = SafeFormatter() + generated_html = formatter.format(P('jacket/template.xhtml', + data=True).decode('utf-8'), **args) # Post-process the generated html to strip out empty header items