mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-07 10:14:46 -04:00
new formatter functions. formats_paths and current_library_path
This commit is contained in:
parent
4ac509e76e
commit
839b68d4b6
@ -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)
|
||||
|
||||
|
@ -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
|
||||
|
@ -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(),
|
||||
|
Loading…
x
Reference in New Issue
Block a user