From 839b68d4b61a75b29ed6ce06ac7eecf0c66a76b1 Mon Sep 17 00:00:00 2001 From: Charles Haley <> Date: Sun, 16 Sep 2012 13:13:36 +0200 Subject: [PATCH] new formatter functions. formats_paths and current_library_path --- src/calibre/library/__init__.py | 9 +++++-- src/calibre/library/database2.py | 1 + src/calibre/utils/formatter_functions.py | 31 +++++++++++++++++++++++- 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/src/calibre/library/__init__.py b/src/calibre/library/__init__.py index 605d062de3..3ae237c919 100644 --- a/src/calibre/library/__init__.py +++ b/src/calibre/library/__init__.py @@ -61,13 +61,18 @@ def generate_test_db(library_path, # {{{ print 'Time per record:', t/float(num_of_records) # }}} -def current_library_name(): +def current_library_path(): from calibre.utils.config import prefs - import posixpath path = prefs['library_path'] if path: path = path.replace('\\', '/') while path.endswith('/'): path = path[:-1] + return path + +def current_library_name(): + import posixpath + path = current_library_path() + if path: return posixpath.basename(path) diff --git a/src/calibre/library/database2.py b/src/calibre/library/database2.py index 0b23e3f0a4..8e8eb691ca 100644 --- a/src/calibre/library/database2.py +++ b/src/calibre/library/database2.py @@ -1262,6 +1262,7 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns): ans = {} if path is not None: stat = os.stat(path) + ans['path'] = path; ans['size'] = stat.st_size ans['mtime'] = utcfromtimestamp(stat.st_mtime) self.format_metadata_cache[id_][fmt] = ans diff --git a/src/calibre/utils/formatter_functions.py b/src/calibre/utils/formatter_functions.py index fb92d04932..d57e50006e 100644 --- a/src/calibre/utils/formatter_functions.py +++ b/src/calibre/utils/formatter_functions.py @@ -629,6 +629,22 @@ class BuiltinFormatsSizes(BuiltinFormatterFunction): fmt_data = mi.get('format_metadata', {}) return ','.join(k.upper()+':'+str(v['size']) for k,v in fmt_data.iteritems()) +class BuiltinFormatsPaths(BuiltinFormatterFunction): + name = 'formats_paths' + arg_count = 0 + category = 'Get values from metadata' + __doc__ = doc = _('formats_paths() -- return a comma-separated list of ' + 'colon_separated items representing full path to ' + 'the formats of a book. You can use the select ' + 'function to get the path for a specific ' + 'format. Note that format names are always uppercase, ' + 'as in EPUB.' + ) + + def evaluate(self, formatter, kwargs, mi, locals): + fmt_data = mi.get('format_metadata', {}) + return ','.join(k.upper()+':'+str(v['path']) for k,v in fmt_data.iteritems()) + class BuiltinHumanReadable(BuiltinFormatterFunction): name = 'human_readable' arg_count = 1 @@ -1146,6 +1162,18 @@ class BuiltinCurrentLibraryName(BuiltinFormatterFunction): from calibre.library import current_library_name return current_library_name() +class BuiltinCurrentLibraryPath(BuiltinFormatterFunction): + name = 'current_library_path' + arg_count = 0 + category = 'Get values from metadata' + __doc__ = doc = _('current_library_path() -- ' + 'return the path to the current calibre library. This function can ' + 'be called in template program mode using the template ' + '"{:\'current_library_path()\'}".') + def evaluate(self, formatter, kwargs, mi, locals): + from calibre.library import current_library_path + return current_library_path() + class BuiltinFinishFormatting(BuiltinFormatterFunction): name = 'finish_formatting' arg_count = 4 @@ -1168,7 +1196,8 @@ _formatter_builtins = [ BuiltinCurrentLibraryName(), BuiltinDaysBetween(), BuiltinDivide(), BuiltinEval(), BuiltinFirstNonEmpty(), BuiltinField(), BuiltinFinishFormatting(), BuiltinFormatDate(), - BuiltinFormatNumber(), BuiltinFormatsModtimes(), BuiltinFormatsSizes(), + BuiltinFormatNumber(), BuiltinFormatsModtimes(), BuiltinFormatsPaths(), + BuiltinFormatsSizes(), BuiltinHasCover(), BuiltinHumanReadable(), BuiltinIdentifierInList(), BuiltinIfempty(), BuiltinLanguageCodes(), BuiltinLanguageStrings(), BuiltinInList(), BuiltinListDifference(), BuiltinListEquals(),