From cfcbc407a33e3cb3fab1cd9a1dce88d9be217f03 Mon Sep 17 00:00:00 2001 From: Charles Haley Date: Wed, 10 Nov 2021 15:49:47 +0000 Subject: [PATCH] Add new formatter function "current_virtual_library_name(). --- manual/template_lang.rst | 1 + src/calibre/utils/formatter_functions.py | 18 +++++++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/manual/template_lang.rst b/manual/template_lang.rst index 4d079a866c..c6501a78f1 100644 --- a/manual/template_lang.rst +++ b/manual/template_lang.rst @@ -389,6 +389,7 @@ In `GPM` the functions described in `Single Function Mode` all require an additi * ``connected_device_uuid(storage_location_key)`` -- if a device is connected then return the device uuid (unique id), otherwise return the empty string. Each storage location on a device has a different uuid. The ``storage_location_key`` location names are ``'main'``, ``'carda'`` and ``'cardb'``. This function works only in the GUI. * ``current_library_name()`` -- return the last name on the path to the current calibre library. * ``current_library_path()`` -- return the full path to the current calibre library. +* ``current_virtual_library_name()`` -- return the name of the current virtual library if there is one, otherwise the empty string. Library name case is preserved. Example: ``program: current_virtual_library_name()``. This function works only in the GUI. * ``date_arithmetic(date, calc_spec, fmt)`` -- Calculate a new date from ``date`` using ``calc_spec``. Return the new date formatted according to optional ``fmt``: if not supplied then the result will be in ISO format. The calc_spec is a string formed by concatenating pairs of ``vW`` (``valueWhat``) where ``v`` is a possibly-negative number and W is one of the following letters: * ``s``: add ``v`` seconds to ``date`` diff --git a/src/calibre/utils/formatter_functions.py b/src/calibre/utils/formatter_functions.py index a625e4905e..d8ae667309 100644 --- a/src/calibre/utils/formatter_functions.py +++ b/src/calibre/utils/formatter_functions.py @@ -1762,6 +1762,22 @@ class BuiltinVirtualLibraries(BuiltinFormatterFunction): return _('This function can be used only in the GUI') +class BuiltinCurrentVirtualLibraryName(BuiltinFormatterFunction): + name = 'current_virtual_library_name' + arg_count = 0 + category = 'Get values from metadata' + __doc__ = doc = _('current_virtual_library_name() -- ' + 'return the name of the current virtual library if there is one, ' + 'otherwise the empty string. Library name case is preserved. ' + 'Example: "program: current_virtual_library_name()".') + + def evaluate(self, formatter, kwargs, mi, locals): + with suppress(Exception): + from calibre.gui2.ui import get_gui + return get_gui().current_db.data.get_base_restriction_name() + return _('This function can be used only in the GUI') + + class BuiltinUserCategories(BuiltinFormatterFunction): name = 'user_categories' arg_count = 0 @@ -2086,7 +2102,7 @@ _formatter_builtins = [ BuiltinCapitalize(), BuiltinCharacter(), BuiltinCheckYesNo(), BuiltinCeiling(), BuiltinCmp(), BuiltinConnectedDeviceName(), BuiltinConnectedDeviceUUID(), BuiltinContains(), BuiltinCount(), BuiltinCurrentLibraryName(), BuiltinCurrentLibraryPath(), - BuiltinDateArithmetic(), + BuiltinCurrentVirtualLibraryName(), BuiltinDateArithmetic(), BuiltinDaysBetween(), BuiltinDivide(), BuiltinEval(), BuiltinFirstNonEmpty(), BuiltinField(), BuiltinFieldExists(), BuiltinFinishFormatting(), BuiltinFirstMatchingCmp(), BuiltinFloor(),