mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Allow the formatter used in save_to_disk to toss exceptions.
This commit is contained in:
parent
36bcb8a754
commit
7e24ed31e2
@ -131,7 +131,7 @@ def preprocess_template(template):
|
|||||||
template = template.decode(preferred_encoding, 'replace')
|
template = template.decode(preferred_encoding, 'replace')
|
||||||
return template
|
return template
|
||||||
|
|
||||||
class SafeFormat(TemplateFormatter):
|
class Formatter(TemplateFormatter):
|
||||||
'''
|
'''
|
||||||
Provides a format function that substitutes '' for any missing value
|
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])
|
format_args[key] = unicode(format_args[key])
|
||||||
else:
|
else:
|
||||||
format_args[key] = ''
|
format_args[key] = ''
|
||||||
components = SafeFormat().safe_format(template, format_args,
|
components = Formatter().unsafe_format(template, format_args, mi)
|
||||||
'G_C-EXCEPTION!', mi)
|
|
||||||
components = [x.strip() for x in components.split('/')]
|
components = [x.strip() for x in components.split('/')]
|
||||||
components = [sanitize_func(x) for x in components if x]
|
components = [sanitize_func(x) for x in components if x]
|
||||||
if not components:
|
if not components:
|
||||||
|
@ -310,7 +310,16 @@ class TemplateFormatter(string.Formatter):
|
|||||||
ans = string.Formatter.vformat(self, fmt, args, kwargs)
|
ans = string.Formatter.vformat(self, fmt, args, kwargs)
|
||||||
return self.compress_spaces.sub(' ', ans).strip()
|
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):
|
def safe_format(self, fmt, kwargs, error_value, book):
|
||||||
self.kwargs = kwargs
|
self.kwargs = kwargs
|
||||||
|
Loading…
x
Reference in New Issue
Block a user