I'm trying to keep a particular setting / variable for the entire duration that Calibre is running (in a FileTypePlugin)
I tried just putting a python variable inside one of the .py files, accessing it with the "global" modifier, but it looks like all the plugins get unloaded for each book.
I have this code in one of my files:
Code:
counter = 0
def getCounter():
global counter
return counter
def incCounter():
global counter
counter = counter + 1
and then I have this code in the initialize() function of my plugin (just to test if it works):
Code:
print("Counter is " + str(getCounter()))
incCounter()
print("Counter is " + str(getCounter()))
When I then import a book into Calibre, it prints "Counter is 0", then "Counter is 1". However, when I import another book after that, it again prints "Counter is 0" instead of continuing with "Counter is 2" which is what I would have expected.
I could store this counter variable inside the JSON settings file like I do with user-defined settings already, but then how do I get the counter reset to 0 when restarting Calibre?
I just want to keep this counter in-between different executions of my plugin, but have it reset to 0 when Calibre completely exits. Is this possible somehow?
Or do I have to use tricks like opening a PersistentTemporaryFile, writing its path to the plugin config (to remember the path between plugin runs), then check if the file is still present or if it's been deleted to detect if Calibre has restarted in the meantime? Sounds like an unreliable workaround ...