Allow the formatter used in save_to_disk to toss exceptions.

This commit is contained in:
Charles Haley 2011-08-06 17:54:12 +01:00
parent 36bcb8a754
commit 7e24ed31e2
2 changed files with 12 additions and 4 deletions

View File

@ -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:

View File

@ -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