The user template functions is not working for me no matter what I do. I can create one, see it in the database, but, when I restart calibre it does not appear. This happens whether I run from source, or use the installed beta. And, it doesn't matter how I create the library. I have tried creating a new library copying my existing structure and creating a new library with the default configuration.
The attached database and JSON file come from using the installed calibre 5.99.12 by clicking on icon created when installing. I uses "Switch/create library" and created a new directory. After that, I closed calibre. Then I opened it and created a user function template using the example in the help, except that I added a couple of a's to the name to have it first. Then I closed and reopened calibre. When I looked, the function was not their. I then zipped up the database and backup file.
For simplicity, I created the same directory structure as you. That was to make sure that the paths in the config files did not not need to be fiddled with. And of course, it worked:
And running using my normal configuration it doesn't work. Looking through the log below, I can see "backend.__init__" called twice, but, only once in yours. The position in the log suggests that a plugin is triggering that.
Spoiler:
Code:
E:\Development\CalibrePlugins>SET CALIBRE_DEVELOP_FROM=E:\Development\GitHub\calibre\src
E:\Development\CalibrePlugins>rem SET CALIBRE_ENABLE_DEVELOP_MODE=1
E:\Development\CalibrePlugins>calibre-debug -s
Failed to contact running instance of calibre
Failed to connect to Listener at: \\.\pipe\CalibreGUI-davidforx with error: QLocalSocket::connectToServer: Invalid name
E:\Development\CalibrePlugins>calibre-debug --paths
CALIBRE_RESOURCES_PATH=C:\Program Files\Calibre2\app\resources
CALIBRE_EXTENSIONS_PATH=C:\Program Files\Calibre2\app\bin
CALIBRE_PYTHON_PATH=
E:\Development\CalibrePlugins>calibre-debug -g
calibre 6.0* embedded-python: True
Windows-10-10.0.19043 Windows ('64bit', 'WindowsPE')
('Windows', '10', '10.0.19043')
Python 3.10.1
Windows: ('10', '10.0.19043', '', 'Multiprocessor Free')
Interface language: None
DEBUG: 0.0 GetFileName::__init__.py - loading translations
Successfully initialized third party plugins: Gather KFX-ZIP (from KFX Input) (1, 49, 0) && DeDRM (7, 2, 1) && Package KFX (from KFX Input) (1, 49, 0) && Annotations (1, 17, 10) && Calibre Power Search Plugin (2, 1, 2) && Clean Comments (0, 2, 0) && Clipboard Search (1, 3, 2) && Count Pages (1, 11, 2) && DOC Input (1, 0, 1) && Databazeknih (1, 4, 11) && Define Content (1, 0, 0) && Device Old Books (0, 0, 3) && Diaps Editing Toolbag (0, 3, 7) && Douban Books v2 (4, 2, 1) && Embed Comic Metadata (1, 6, 1) && EpubMerge (2, 13, 0) && EpubSplit (3, 3, 0) && Ex Libris (1, 0, 20) && Extract ISBN (1, 5, 1) && FanFicFare (4, 14, 0) && Fantastic Fiction (1, 5, 1) && Fantastic Fiction Adults (1, 2, 0) && Favourites Menu (1, 1, 0) && FictionDB (1, 2, 2) && Find Duplicates (1, 9, 5) && Generate Cover (2, 1, 0) && GetFileName (0, 2, 0) && Goodreads (1, 5, 3) && Goodreads Sync (1, 15, 3) && Google Scholar (0, 1, 1) && Hyphenate This! (0, 1, 3) && Import List (1, 8, 3) && KFX metadata reader (from KFX Input) (1, 49, 0) && KFX Input (1, 49, 0) && Set KFX metadata (from KFX Output) (1, 63, 0) && KFX Output (1, 63, 0) && KePub Metadata Reader (3, 4, 3) && KePub Metadata Writer (3, 4, 3) && KePub Output (3, 6, 0) && Kindle 2, 3, 4, Touch, PaperWhite, Voyage Device Interface MBP Update Mod (1, 0, 0) && Kindle hi-res covers (0, 5, 0) && Kobo Books (1, 9, 2) && Kobo Import (1, 0, 1) && Kobo Utilities (2, 16, 3) && KoboTouchExtended (3, 6, 0) && KoboTouchTOCUpdate (1, 2, 12) && LubimyCzytac (2, 2, 26) && METAmover (0, 0, 40) && Manage Series (1, 3, 0) && Manga (0, 1, 0) && Martinus.sk (2021, 12, 20) && Modify SOL Story (0, 10, 3) && Modify SOL Story (0, 10, 3) && Modify ePub (1, 7, 3) && Moly_hu (1, 1, 7) && Obok DeDRM (7, 2, 2) && Open With (1, 7, 1) && Overdrive Link (2, 51, 0) && Quality Check (1, 12, 0) && Quick Preferences (1, 5, 0) && Reading List (1, 12, 0) && Resize Cover (1, 1, 1) && ScrambleEbook (0, 6, 0) && Scroll Book Details (1, 0, 0) && Search The Internet (1, 8, 1) && Shentai (0, 0, 3) && Skoob Books (1, 5, 4) && SmartEject (2, 5, 0) && Smashwords Metadata (1, 0, 2) && Sum Column (0, 3, 3) && Unplugged (1, 0, 0) && User Category (1, 5, 6) && Velamma Comics (0, 0, 1) && View Manager (1, 8, 0) && Walk Search History (1, 3, 2) && Webscription (1, 1, 2) && WordDumb (3, 22, 0) && WorldsWithoutEnd (0, 0, 2)
devicePixelRatio: 1.5
logicalDpi: 96.0 x 96.0
physicalDpi: 127.66492146596858 x 127.59069767441859
Using calibre Qt style: False
[0.00] Starting up...
[0.00] Showing splash screen...
[0.22] splash screen shown
[0.22] Initializing db...
backend.__init__ - set template functions aamy_ifempty
from calibre.utils.formatter_functions import FormatterUserFunction
from calibre.utils.formatter_functions import formatter_functions
class UserFunction(FormatterUserFunction):
def evaluate(self, formatter, kwargs, mi, locals, val):
if val:
return val
else:
return 'EMPTY'
backend.set_user_template_functions 1 aamy_ifempty
backend.set_user_template_functions 2 add, and, approximate_formats, arguments, assign, author_links, author_sorts, book_count, book_values, booksize, capitalize, character, check_yes_no, ceiling, cmp, connected_device_name, connected_device_uuid, contains, count, list_count, current_library_name, current_library_path, current_virtual_library_name, date_arithmetic, days_between, divide, eval, first_non_empty, field, field_exists, finish_formatting, first_matching_cmp, floor, format_date, format_number, formats_modtimes, formats_paths, formats_sizes, fractional_part, globals, has_cover, human_readable, identifier_in_list, ifempty, language_codes, language_strings, in_list, list_contains, is_marked, list_count_matching, count_matching, list_difference, list_equals, list_intersection, list_item, list_join, list_re, list_re_group, list_remove_duplicates, list_sort, list_split, list_union, merge_lists, lookup, lowercase, mod, multiply, not, ondevice, or, print, rating_to_stars, range, raw_field, raw_list, re, re_group, round, select, series_sort, set_globals, shorten, strcat, strcat_max, strcmp, str_in_list, strlen, subitems, sublist, substr, subtract, swap_around_articles, swap_around_comma, switch, template, test, titlecase, today, transliterate, uppercase, urls_from_identifiers, user_categories, virtual_libraries, annotation_count, aamy_ifempty
backend.get_user_template_functions aamy_ifempty
backend.set_global_state aamy_ifempty
backend.get_user_template_functions aamy_ifempty
[0.23] db initialized
[0.23] Constructing main UI...
DEBUG: 0.0 Annotations plugin: Error loading libiMobileDevice. This hasn't worked for a while, and is blacklisted in calibre v3.
DEBUG: 0.0 Annotations plugin: Error is: No module named 'calibre.devices.idevice'
DEBUG: 0.0 Annotations::reader_app_support.py - loading translations
DEBUG: 0.0 Annotations::common_utils.py - loading translations
DEBUG: 0.0 Annotations::appearance.py - loading translations
DEBUG: 0.0 Annotations::config.py - loading translations
DEBUG: 0.0 Annotations::annotated_books.py - loading translations
DEBUG: 0.2 Annotations::action.py - loading translations
DEBUG: 0.0 Clean Comments::config.py - loading translations
DEBUG: 0.0 CleanComment::action.py - loading translations
DEBUG: 0.0 Goodreads Sync::dialogs.py - loading translations
DEBUG: 0.0 GoodreadsSync::action.py - loading translations
DEBUG: 0.0 kobo_import::utilities.py - loading translations
DEBUG: 0.0 kobo_import::dialogs.py - loading translations
DEBUG: 0.0 kobo_import::config.py - loading translations
DEBUG: 0.0 kobo_import::action_err.py - loading translations
DEBUG: 0.0 Kobo Utilities Configuration - CreateNewCustomColumn is supported
DEBUG: 0.0 KoboUtilites::action.py - loading translations
DEBUG: 0.0 KoboUtilites::dialogs.py - loading translations
DEBUG: 0.0 KoboUtilites::action.py - loading translations
DEBUG: 0.0 obok::utilities.py - loading translations
DEBUG: 0.0 obok::dialogs.py - loading translations
DEBUG: 0.0 obok::config.py - loading translations
DEBUG: 0.0 obok::action_err.py - loading translations
qt.webengine.QWebEngineView OK
DEBUG: 6.0 ~~~~~~~~~~~~~~~~~~~~~ Annotations plugin 1.17.10 (Windows) ~~~~~~~~~~~~~~~~~~~~~
DEBUG: 6.0 AnnotationsAction:genesis(ERROR) Error loading library libiMobileDevice: name 'libiMobileDevice' is not defined
DEBUG: 6.1 AnnotationsAction:init_options(disable_caching: True)
DEBUG: 6.1 AnnotationsDB:connect(db_version: 1)
DEBUG: 6.1 AnnotationsAction:load_dynamic_reader_classes()
DEBUG: 6.1 loading built-in class 'BooxReaderApp'
DEBUG: 6.1 loading built-in class 'GoodReader'
DEBUG: 6.1 loading built-in class 'Kindle'
DEBUG: 6.1 loading built-in class 'KoboTouch'
DEBUG: 6.1 loading built-in class 'ParseKindleMyClippingsTxt'
DEBUG: 6.1 loading built-in class 'ParseTolinoNotesTxt'
DEBUG: 6.1 loading built-in class 'PocketBook'
DEBUG: 6.1 loading built-in class 'Sony'
DEBUG: 6.1 loading built-in class 'Tolino'
DEBUG: 6.1 AnnotationsAction:inflate_dialog_resources()
backend.__init__ - set template functions aamy_ifempty
from calibre.utils.formatter_functions import FormatterUserFunction
from calibre.utils.formatter_functions import formatter_functions
class UserFunction(FormatterUserFunction):
def evaluate(self, formatter, kwargs, mi, locals, val):
if val:
return val
else:
return 'EMPTY'
backend.set_user_template_functions 1 aamy_ifempty
backend.set_user_template_functions 2 add, and, approximate_formats, arguments, assign, author_links, author_sorts, book_count, book_values, booksize, capitalize, character, check_yes_no, ceiling, cmp, connected_device_name, connected_device_uuid, contains, count, list_count, current_library_name, current_library_path, current_virtual_library_name, date_arithmetic, days_between, divide, eval, first_non_empty, field, field_exists, finish_formatting, first_matching_cmp, floor, format_date, format_number, formats_modtimes, formats_paths, formats_sizes, fractional_part, globals, has_cover, human_readable, identifier_in_list, ifempty, language_codes, language_strings, in_list, list_contains, is_marked, list_count_matching, count_matching, list_difference, list_equals, list_intersection, list_item, list_join, list_re, list_re_group, list_remove_duplicates, list_sort, list_split, list_union, merge_lists, lookup, lowercase, mod, multiply, not, ondevice, or, print, rating_to_stars, range, raw_field, raw_list, re, re_group, round, select, series_sort, set_globals, shorten, strcat, strcat_max, strcmp, str_in_list, strlen, subitems, sublist, substr, subtract, swap_around_articles, swap_around_comma, switch, template, test, titlecase, today, transliterate, uppercase, urls_from_identifiers, user_categories, virtual_libraries, annotation_count, aamy_ifempty
backend.get_user_template_functions aamy_ifempty
backend.set_global_state aamy_ifempty
backend.get_user_template_functions aamy_ifempty
Key 'Ctrl+Shift+S' for shortcut Exact text search is already used by Power Search, ignoring
Key 'Ctrl+Shift+S' for shortcut Exact text search is already used by Power Search, ignoring
Key 'Ctrl+Shift+S' for shortcut Exact text search is already used by Power Search, ignoring
Key 'Ctrl+Shift+S' for shortcut Exact text search is already used by Power Search, ignoring
Key 'Ctrl+Shift+S' for shortcut Exact text search is already used by Power Search, ignoring
Key 'Ctrl+Shift+S' for shortcut Exact text search is already used by Power Search, ignoring
Key 'Ctrl+Shift+S' for shortcut Exact text search is already used by Power Search, ignoring
Key 'Ctrl+Shift+S' for shortcut Exact text search is already used by Power Search, ignoring
Key 'Ctrl+Shift+S' for shortcut Exact text search is already used by Power Search, ignoring
Key 'Ctrl+Shift+S' for shortcut Exact text search is already used by Power Search, ignoring
qt.gui.imageio: libpng warning: iCCP: known incorrect sRGB profile
{'default': False, 'height': 680, 'keep_aspect_ratio': False, 'only_shrink_larger_images': True, 'width': 450}
Resize Covers::rebuild_menus - size_data={'default': False, 'height': 680, 'keep_aspect_ratio': False, 'only_shrink_larger_images': True, 'width': 450}
{'default': False, 'height': 900, 'keep_aspect_ratio': False, 'only_shrink_larger_images': True, 'width': 1100}
Resize Covers::rebuild_menus - size_data={'default': False, 'height': 900, 'keep_aspect_ratio': False, 'only_shrink_larger_images': True, 'width': 1100}
{'default': True, 'height': 900, 'keep_aspect_ratio': True, 'only_shrink_larger_images': True, 'width': 999999}
Resize Covers::rebuild_menus - size_data={'default': True, 'height': 900, 'keep_aspect_ratio': True, 'only_shrink_larger_images': True, 'width': 999999}
{'default': False, 'height': 680, 'keep_aspect_ratio': False, 'only_shrink_larger_images': False, 'width': 450}
Resize Covers::rebuild_menus - size_data={'default': False, 'height': 680, 'keep_aspect_ratio': False, 'only_shrink_larger_images': False, 'width': 450}
{'default': False, 'height': 800, 'keep_aspect_ratio': True, 'only_shrink_larger_images': False, 'width': 600}
Resize Covers::rebuild_menus - size_data={'default': False, 'height': 800, 'keep_aspect_ratio': True, 'only_shrink_larger_images': False, 'width': 600}
{'default': False, 'height': 1024, 'keep_aspect_ratio': True, 'only_shrink_larger_images': False, 'width': 758}
Resize Covers::rebuild_menus - size_data={'default': False, 'height': 1024, 'keep_aspect_ratio': True, 'only_shrink_larger_images': False, 'width': 758}
Key 'Ctrl+Shift+S' for shortcut Exact text search is already used by Power Search, ignoring
qt.gui.imageio: libpng warning: iCCP: known incorrect sRGB profile
qt.gui.imageio: libpng warning: iCCP: known incorrect sRGB profile
qt.gui.imageio: libpng warning: iCCP: cHRM chunk does not match sRGB
qt.gui.imageio: libpng warning: iCCP: known incorrect sRGB profile
qt.gui.imageio: libpng warning: iCCP: known incorrect sRGB profile
qt.gui.imageio: libpng warning: iCCP: known incorrect sRGB profile
qt.gui.imageio: libpng warning: iCCP: known incorrect sRGB profile
qt.gui.imageio: libpng warning: iCCP: known incorrect sRGB profile
qt.gui.imageio: libpng warning: iCCP: known incorrect sRGB profile
qt.gui.imageio: libpng warning: iCCP: known incorrect sRGB profile
qt.gui.imageio: libpng warning: iCCP: known incorrect sRGB profile
Key 'Ctrl+Shift+S' for shortcut Exact text search is already used by Power Search, ignoring
qt.gui.imageio: libpng warning: iCCP: known incorrect sRGB profile
backend.get_template_functions add, and, approximate_formats, arguments, assign, author_links, author_sorts, book_count, book_values, booksize, capitalize, character, check_yes_no, ceiling, cmp, connected_device_name, connected_device_uuid, contains, count, list_count, current_library_name, current_library_path, current_virtual_library_name, date_arithmetic, days_between, divide, eval, first_non_empty, field, field_exists, finish_formatting, first_matching_cmp, floor, format_date, format_number, formats_modtimes, formats_paths, formats_sizes, fractional_part, globals, has_cover, human_readable, identifier_in_list, ifempty, language_codes, language_strings, in_list, list_contains, is_marked, list_count_matching, count_matching, list_difference, list_equals, list_intersection, list_item, list_join, list_re, list_re_group, list_remove_duplicates, list_sort, list_split, list_union, merge_lists, lookup, lowercase, mod, multiply, not, ondevice, or, print, rating_to_stars, range, raw_field, raw_list, re, re_group, round, select, series_sort, set_globals, shorten, strcat, strcat_max, strcmp, str_in_list, strlen, subitems, sublist, substr, subtract, swap_around_articles, swap_around_comma, switch, template, test, titlecase, today, transliterate, uppercase, urls_from_identifiers, user_categories, virtual_libraries, annotation_count, aamy_ifempty
Key 'Ctrl+Shift+S' for shortcut Exact text search is already used by Power Search, ignoring
EpubMerge: DEBUG: 2022-07-12 11:56:05,010: calibre_plugins.epubmerge.epubmerge_plugin(155): macmenuhack file_path:C:\Users\davidfor\AppData\Roaming\calibre\plugins\fanficfare_macmenuhack.txt
Key 'Ctrl+Shift+S' for shortcut Exact text search is already used by Power Search, ignoring
FFF: INFO: 2022-07-12 11:56:05,035: calibre_plugins.fanficfare_plugin.prefs(214): Using default settings
FFF: DEBUG: 2022-07-12 11:56:05,076: calibre_plugins.fanficfare_plugin.fff_plugin(218): Plugin FanFicFare macmenuhack file_path:C:\Users\davidfor\AppData\Roaming\calibre\plugins\fanficfare_macmenuhack.txt
Key 'Ctrl+Shift+S' for shortcut Exact text search is already used by Power Search, ignoring
QLayout::addChildLayout: layout "" already has a parent
DEBUG: 4.9 HttpHelper::__init__: proxy=None
Key 'Ctrl+Shift+S' for shortcut Exact text search is already used by Power Search, ignoring
DEBUG: 4.7 No Kobo Touch, Glo or Mini appears to be connected
DEBUG: 4.8 rebuild_menus - self.supports_ratings=None, self.supports_tiles=None
DEBUG: 4.8 KoboUtilities:set_toolbar_button_tooltip - start: text='None'
DEBUG: 4.8 KoboUtilities:set_toolbar_button_tooltip - setting to text='Utilities to use with Kobo ereaders
Driver: KoboTouchExtended'
Key 'Ctrl+Shift+S' for shortcut Exact text search is already used by Power Search, ignoring
Key 'Ctrl+Shift+S' for shortcut Exact text search is already used by Power Search, ignoring
Key 'Ctrl+Shift+S' for shortcut Exact text search is already used by Power Search, ignoring
Key 'Ctrl+Shift+S' for shortcut Exact text search is already used by Power Search, ignoring
Key 'Ctrl+Shift+S' for shortcut Exact text search is already used by Power Search, ignoring
qt.gui.imageio: libpng warning: iCCP: known incorrect sRGB profile
qt.gui.imageio: libpng warning: iCCP: known incorrect sRGB profile
Key 'Ctrl+Shift+S' for shortcut Exact text search is already used by Power Search, ignoring
Key 'Ctrl+Shift+S' for shortcut Exact text search is already used by Power Search, ignoring
backend.close - unloading template functions 32d3fa1c-9a4a-450a-a422-e0dcface23d5
[7.64] main UI initialized...
[7.64] Hiding splash screen
Starting QuickView
[8.31] splash screen hidden
[8.31] Started up in 8.31 seconds with 1 books
And the problem is in the plugin Clean Comments. That has:
Code:
def build_menus(self):
m = self.menu
m.clear()
candidate = self.gui.library_path
db = LibraryDatabase (candidate)
I don't know why it is doing that as it doesn't actually use the database in that, or the other method. Where it does use the database, it connects to it properly.
Removing the last two lines, plus the similar ones elsewhere, fixes the problem.
And I have posted a fixed version in the plugins thread.
I see what is happening. When you open a database it registers its user-defined formatter functions using the library UUID. When a database instance is closed it de-registers them using that UUID. This means that if you open the *same* database twice then the functions are registered using the same UUID, which in practice means they are registered once. When either is closed the functions for both are deregistered because they share the UUID.
You can see this happening in your log:
Code:
backend.close - unloading template functions 32d3fa1c-9a4a-450a-a422-e0dcface23d5
[7.64] main UI initialized...
The problem can be avoided by passing is_second_db=True when opening the database.
I looked at checking for a second registration of functions for a given database. That won't work because it is legal to re-register functions for an open db instance, for example when functions are created/changed in preferences / template functions.