From 7e24ed31e2f6d963baaf76c886d0ac2145f805ec Mon Sep 17 00:00:00 2001 From: Charles Haley <> Date: Sat, 6 Aug 2011 17:54:12 +0100 Subject: [PATCH] Allow the formatter used in save_to_disk to toss exceptions. --- src/calibre/library/save_to_disk.py | 5 ++--- src/calibre/utils/formatter.py | 11 ++++++++++- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/calibre/library/save_to_disk.py b/src/calibre/library/save_to_disk.py index 3a95086faa..8fcdfc9855 100644 --- a/src/calibre/library/save_to_disk.py +++ b/src/calibre/library/save_to_disk.py @@ -131,7 +131,7 @@ def preprocess_template(template): template = template.decode(preferred_encoding, 'replace') return template -class SafeFormat(TemplateFormatter): +class Formatter(TemplateFormatter): ''' Provides a format function that substitutes '' for any missing value ''' @@ -225,8 +225,7 @@ def get_components(template, mi, id, timefmt='%b %Y', length=250, format_args[key] = unicode(format_args[key]) else: format_args[key] = '' - components = SafeFormat().safe_format(template, format_args, - 'G_C-EXCEPTION!', mi) + components = Formatter().unsafe_format(template, format_args, mi) components = [x.strip() for x in components.split('/')] components = [sanitize_func(x) for x in components if x] if not components: diff --git a/src/calibre/utils/formatter.py b/src/calibre/utils/formatter.py index 3a93c2b650..bc25f25043 100644 --- a/src/calibre/utils/formatter.py +++ b/src/calibre/utils/formatter.py @@ -310,7 +310,16 @@ class TemplateFormatter(string.Formatter): ans = string.Formatter.vformat(self, fmt, args, kwargs) return self.compress_spaces.sub(' ', ans).strip() - ########## a formatter guaranteed not to throw and exception ############ + ########## a formatter that throws exceptions ############ + + def unsafe_format(self, fmt, kwargs, book): + self.kwargs = kwargs + self.book = book + self.composite_values = {} + self.locals = {} + return self.vformat(fmt, [], kwargs).strip() + + ########## a formatter guaranteed not to throw an exception ############ def safe_format(self, fmt, kwargs, error_value, book): self.kwargs = kwargs