From 50aae924fc3c9e060ccdc6ada455bd5366225ceb Mon Sep 17 00:00:00 2001 From: Charles Haley <> Date: Sun, 3 Oct 2010 09:21:08 +0100 Subject: [PATCH] Make template processing more robust (I hope) --- src/calibre/ebooks/metadata/book/base.py | 7 +++++-- src/calibre/gui2/device.py | 2 +- src/calibre/library/save_to_disk.py | 26 ++++++++++++++---------- src/calibre/utils/formatter.py | 14 +++++++------ 4 files changed, 29 insertions(+), 20 deletions(-) diff --git a/src/calibre/ebooks/metadata/book/base.py b/src/calibre/ebooks/metadata/book/base.py index 17611875f8..b8288e210c 100644 --- a/src/calibre/ebooks/metadata/book/base.py +++ b/src/calibre/ebooks/metadata/book/base.py @@ -8,6 +8,7 @@ __docformat__ = 'restructuredtext en' import copy, traceback from calibre import prints +from calibre.constants import DEBUG from calibre.ebooks.metadata.book import SC_COPYABLE_FIELDS from calibre.ebooks.metadata.book import SC_FIELDS_COPY_NOT_NULL from calibre.ebooks.metadata.book import STANDARD_METADATA_FIELDS @@ -50,6 +51,8 @@ class SafeFormat(TemplateFormatter): return '' return v except: + if DEBUG: + traceback.print_exc() return key composite_formatter = SafeFormat() @@ -320,8 +323,8 @@ class Metadata(object): else: self.set(dest, val) except: - traceback.print_exc() - pass + if DEBUG: + traceback.print_exc() # Old Metadata API {{{ def print_all_attributes(self): diff --git a/src/calibre/gui2/device.py b/src/calibre/gui2/device.py index 254c62e48c..d81fad3da9 100644 --- a/src/calibre/gui2/device.py +++ b/src/calibre/gui2/device.py @@ -340,7 +340,7 @@ class DeviceManager(Thread): # {{{ cpb = None if DEBUG: - prints('Using plugboard', ext, dev_name, cpb) + prints('Device using plugboard', ext, dev_name, cpb) if ext: try: if DEBUG: diff --git a/src/calibre/library/save_to_disk.py b/src/calibre/library/save_to_disk.py index 6c86db3420..f3c384cb15 100644 --- a/src/calibre/library/save_to_disk.py +++ b/src/calibre/library/save_to_disk.py @@ -8,6 +8,8 @@ __docformat__ = 'restructuredtext en' import os, traceback, cStringIO, re +from calibre import prints +from calibre.constants import DEBUG from calibre.utils.config import Config, StringConfig, tweaks from calibre.utils.formatter import TemplateFormatter from calibre.utils.filenames import shorten_components_to, supports_long_names, \ @@ -118,8 +120,8 @@ class SafeFormat(TemplateFormatter): try: b = self.book.get_user_metadata(key, False) except: - print 'save_to_disk get value exception' - traceback.print_exc() + if DEBUG: + traceback.print_exc() b = None if b is not None and b['datatype'] == 'composite': @@ -129,13 +131,13 @@ class SafeFormat(TemplateFormatter): self.composite_values[key] = \ self.vformat(b['display']['composite_template'], [], kwargs) return self.composite_values[key] - if kwargs[key]: - return self.sanitize(kwargs[key]) + if key in kwargs: + return kwargs[key].replace('/', '_').replace('\\', '_') return '' except: - print 'save_to_disk general exception' - traceback.print_exc() - return '' + if DEBUG: + traceback.print_exc() + return key safe_formatter = SafeFormat() @@ -182,8 +184,8 @@ def get_components(template, mi, id, timefmt='%b %Y', length=250, elif custom_metadata[key]['datatype'] == 'bool': format_args[key] = _('yes') if format_args[key] else _('no') - components = safe_formatter.safe_format(template, format_args, '', mi, - sanitize=sanitize_func) + components = safe_formatter.safe_format(template, format_args, + 'G_C-EXCEPTION!', mi) components = [x.strip() for x in components.split('/') if x.strip()] components = [sanitize_func(x) for x in components if x] if not components: @@ -267,7 +269,8 @@ def save_book_to_disk(id, db, root, opts, length): cpb = cpb[dev_name] else: cpb = None - #prints('Using plugboard:', fmt, cpb) + if DEBUG: + prints('Save-to-disk using plugboard:', fmt, cpb) data = db.format(id, fmt, index_is_id=True) if data is None: continue @@ -285,7 +288,8 @@ def save_book_to_disk(id, db, root, opts, length): newmi = mi set_metadata(stream, newmi, fmt) except: - traceback.print_exc() + if DEBUG: + traceback.print_exc() stream.seek(0) data = stream.read() fmt_path = base_path+'.'+str(fmt) diff --git a/src/calibre/utils/formatter.py b/src/calibre/utils/formatter.py index cba21705a9..fdfd7b77c3 100644 --- a/src/calibre/utils/formatter.py +++ b/src/calibre/utils/formatter.py @@ -4,7 +4,9 @@ Created on 23 Sep 2010 @author: charles ''' -import re, string +import re, string, traceback + +from calibre.constants import DEBUG class TemplateFormatter(string.Formatter): ''' @@ -19,7 +21,6 @@ class TemplateFormatter(string.Formatter): string.Formatter.__init__(self) self.book = None self.kwargs = None - self.sanitize = None def _lookup(self, val, field_if_set, field_not_set): if val: @@ -99,8 +100,8 @@ class TemplateFormatter(string.Formatter): return fmt, '', '' return matches.groups() except: - import traceback - traceback.print_exc() + if DEBUG: + traceback.print_exc() return fmt, '', '' def format_field(self, val, fmt): @@ -139,14 +140,15 @@ class TemplateFormatter(string.Formatter): ans = string.Formatter.vformat(self, fmt, args, kwargs) return self.compress_spaces.sub(' ', ans).strip() - def safe_format(self, fmt, kwargs, error_value, book, sanitize=None): + def safe_format(self, fmt, kwargs, error_value, book): self.kwargs = kwargs self.book = book - self.sanitize = sanitize self.composite_values = {} try: ans = self.vformat(fmt, [], kwargs).strip() except: + if DEBUG: + traceback.print_exc() ans = error_value return ans