From cbdc4d5e00ac9d30dbe13dbe3ee39fa30cebae4d Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 11 Jan 2010 10:46:16 -0700 Subject: [PATCH] When using Save to disk/saned to device templates, ignore index and atrtibute errors if the suer incorrectly uses advanced formatting --- src/calibre/library/save_to_disk.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/calibre/library/save_to_disk.py b/src/calibre/library/save_to_disk.py index 581a1e400d..5076f10d86 100644 --- a/src/calibre/library/save_to_disk.py +++ b/src/calibre/library/save_to_disk.py @@ -23,7 +23,8 @@ DEFAULT_SEND_TEMPLATE = '{author_sort}/{title} - {authors}' FORMAT_ARG_DESCS = dict( title=_('The title'), authors=_('The authors'), - author_sort=_('The author sort string'), + author_sort=_('The author sort string. To use only the first letter ' + 'of the name use {author_sort[0]}'), tags=_('The tags'), series=_('The series'), series_index=_('The series number. To get leading zeros use {series_index:0>3s}'), @@ -94,6 +95,15 @@ def preprocess_template(template): template = template.decode(preferred_encoding, 'replace') return template +def safe_format(x, format_args): + try: + return x.format(**format_args).strip() + except IndexError: # Thrown if user used [] and index is out of bounds + pass + except AttributeError: # Thrown if user used a non existing attribute + pass + return '' + def get_components(template, mi, id, timefmt='%b %Y', length=250, sanitize_func=ascii_filename, replace_whitespace=False, to_lowercase=False): @@ -124,7 +134,7 @@ def get_components(template, mi, id, timefmt='%b %Y', length=250, format_args['pubdate'] = strftime(timefmt, mi.pubdate.timetuple()) format_args['id'] = str(id) components = [x.strip() for x in template.split('/') if x.strip()] - components = [x.format(**format_args).strip() for x in components] + components = [safe_format(x, format_args) for x in components] components = [sanitize_func(x) for x in components if x] if not components: components = [str(id)]