From b1250a6db18366d599ea63e27658d7851d1e1f35 Mon Sep 17 00:00:00 2001 From: Charles Haley <> Date: Wed, 22 Sep 2010 16:43:28 +0100 Subject: [PATCH] Add prefix and postfix to template values. Syntax: either '{key}' or '{txt1|key|txt2}'. In the second case, if val[key] is not empty, then the result is 'txt1' + val[key] + txt2. --- src/calibre/ebooks/metadata/book/base.py | 18 +++++++++++++----- src/calibre/gui2/dialogs/metadata_bulk.py | 9 --------- src/calibre/library/save_to_disk.py | 17 ++++++++++++++++- 3 files changed, 29 insertions(+), 15 deletions(-) diff --git a/src/calibre/ebooks/metadata/book/base.py b/src/calibre/ebooks/metadata/book/base.py index 3d6d6b1bb8..d5a86264bf 100644 --- a/src/calibre/ebooks/metadata/book/base.py +++ b/src/calibre/ebooks/metadata/book/base.py @@ -14,8 +14,8 @@ from calibre.ebooks.metadata.book import STANDARD_METADATA_FIELDS from calibre.ebooks.metadata.book import TOP_LEVEL_CLASSIFIERS from calibre.ebooks.metadata.book import ALL_METADATA_FIELDS from calibre.library.field_metadata import FieldMetadata -from calibre.utils.date import isoformat, format_date +from calibre.utils.date import isoformat, format_date NULL_VALUES = { @@ -38,10 +38,17 @@ class SafeFormat(string.Formatter): Provides a format function that substitutes '' for any missing value ''' def get_value(self, key, args, mi): - ign, v = mi.format_field(key, series_with_index=False) - if v is None: - return '' - return v + from calibre.library.save_to_disk import explode_string_template_value + try: + prefix, key, suffix = explode_string_template_value(key) + ign, v = mi.format_field(key, series_with_index=False) + if v is None: + return '' + if v is '': + return '' + return '%s%s%s'%(prefix, v, suffix) + except: + return key composite_formatter = SafeFormat() compress_spaces = re.compile(r'\s+') @@ -50,6 +57,7 @@ def format_composite(x, mi): try: ans = composite_formatter.vformat(x, [], mi).strip() except: + traceback.print_exc() ans = x return compress_spaces.sub(' ', ans) diff --git a/src/calibre/gui2/dialogs/metadata_bulk.py b/src/calibre/gui2/dialogs/metadata_bulk.py index fa3b1a9aa7..a9e45087fd 100644 --- a/src/calibre/gui2/dialogs/metadata_bulk.py +++ b/src/calibre/gui2/dialogs/metadata_bulk.py @@ -122,15 +122,6 @@ class SafeFormat(string.Formatter): v = ','.join(v) return v -composite_formatter = SafeFormat() - -def format_composite(x, mi): - try: - ans = composite_formatter.vformat(x, [], mi).strip() - except: - ans = x - return ans - class MetadataBulkDialog(QDialog, Ui_MetadataBulkDialog): s_r_functions = { '' : lambda x: x, diff --git a/src/calibre/library/save_to_disk.py b/src/calibre/library/save_to_disk.py index fe62dcb7fd..6f7929a072 100644 --- a/src/calibre/library/save_to_disk.py +++ b/src/calibre/library/save_to_disk.py @@ -101,15 +101,30 @@ def preprocess_template(template): template = template.decode(preferred_encoding, 'replace') return template +template_value_re = re.compile(r'^([^\|]*(?=\|))(?:\|?)([^\|]*)(?:\|?)((?<=\|).*?)$') + +def explode_string_template_value(key): + try: + matches = template_value_re.match(key) + if matches.lastindex != 3: + return key + return matches.groups() + except: + return '', key, '' + class SafeFormat(string.Formatter): ''' Provides a format function that substitutes '' for any missing value ''' def get_value(self, key, args, kwargs): try: - return kwargs[key] + prefix, key, suffix = explode_string_template_value(key) + if kwargs[key]: + return '%s%s%s'%(prefix, kwargs[key], suffix) + return '' except: return '' + safe_formatter = SafeFormat() def safe_format(x, format_args):