Add setting to choose between parallel or sequencial fulfillment

This commit is contained in:
Florian Bach 2022-10-23 10:19:55 +02:00
parent 3bd4d28936
commit 4dfc7af7b8
3 changed files with 208 additions and 124 deletions

View File

@ -47,10 +47,13 @@
# rename plugin from "DeACSM" to "ACSM Input". BETA build, not a normal release!!
#
# v0.1.0: Continue work on renaming from "DeACSM" to "ACSM Input".
# The big version number jump is to make that name change clearer.
# The big version number jump is to make that name change clearer,
# and to support the "migration plugin" to rename the plugin.
# Print useful warning if LicenseServiceCertificate download fails,
# fix error with the loan list not being updated when importing multiple ACSMs at once,
# fix bug with the GUI extension in non-English environments,
# add setting to choose between simultaneous (faster) or sequencial (more ADE-like)
# import of multiple ACSM files
@ -63,7 +66,6 @@ __version__ = PLUGIN_VERSION = ".".join([str(x)for x in PLUGIN_VERSION_TUPLE])
from calibre.utils.config import config_dir # type: ignore
from calibre.constants import islinux # type: ignore
import os, shutil, traceback, sys, time, io, random
import zipfile
@ -406,18 +408,82 @@ class ACSMInput(FileTypePlugin):
print("{0} v{1}: Error: Unsupported file type ...".format(PLUGIN_NAME, PLUGIN_VERSION))
return None
def is_blocked(self):
import calibre_plugins.deacsm.prefs as prefs # type: ignore
deacsmprefs = prefs.ACSMInput_Prefs()
return deacsmprefs['fulfillment_block_token'] != 0
def unblock(self):
import calibre_plugins.deacsm.prefs as prefs # type: ignore
deacsmprefs = prefs.ACSMInput_Prefs()
my_token = deacsmprefs["fulfillment_block_token"]
deacsmprefs.refresh()
if (my_token == deacsmprefs["fulfillment_block_token"]):
# Only unlock if this is my own lock
deacsmprefs.set("fulfillment_block_token", 0)
deacsmprefs.set("fulfillment_block_time", 0)
deacsmprefs.commit()
def wait_and_block(self):
random_identifier = None
import calibre_plugins.deacsm.prefs as prefs # type: ignore
deacsmprefs = prefs.ACSMInput_Prefs()
while True:
deacsmprefs.refresh()
if deacsmprefs["fulfillment_block_token"] == 0:
random_identifier = random.getrandbits(64)
#print("setting block token to %s" % (str(random_identifier)))
deacsmprefs.set("fulfillment_block_token", random_identifier)
deacsmprefs.commit()
deacsmprefs.refresh()
if random_identifier != deacsmprefs["fulfillment_block_token"]:
# print("we broke another thread's global token")
continue
deacsmprefs.set("fulfillment_block_time", int(time.time() * 1000))
#print("Obtained lock!")
return True
else:
# Token already exists, wait for it to finish ...
current_time = int(time.time() * 1000)
saved_time = deacsmprefs["fulfillment_block_time"]
if saved_time + 60000 < current_time:
# Already locked since 60s, assume error
print("{0} v{1}: Looks like the lock was stuck, removing lock {2} ...".format(PLUGIN_NAME, PLUGIN_VERSION, deacsmprefs["fulfillment_block_token"]))
self.unblock()
time.sleep(0.02)
continue
def run(self, path_to_ebook):
# type: (str) -> str
try:
# This code gets called by Calibre with a path to the new book file.
# We need to check if it's an ACSM file
import calibre_plugins.deacsm.prefs as prefs # type: ignore
deacsmprefs = prefs.ACSMInput_Prefs()
if deacsmprefs['allow_parallel_fulfillment'] == False:
self.wait_and_block()
print("{0} v{1}: Trying to parse file {2}".format(PLUGIN_NAME, PLUGIN_VERSION, os.path.basename(path_to_ebook)))
ext = os.path.splitext(path_to_ebook)[1].lower()
if (ext != ".acsm"):
print("{0} v{1}: That's not an ACSM, returning (is {2} instead)... ".format(PLUGIN_NAME, PLUGIN_VERSION, ext))
self.unblock()
return path_to_ebook
# That's an ACSM.
@ -425,6 +491,7 @@ class ACSMInput(FileTypePlugin):
if not self.ADE_sanity_check():
print("{0} v{1}: ADE auth is missing or broken ".format(PLUGIN_NAME, PLUGIN_VERSION))
self.unblock()
return path_to_ebook
@ -433,6 +500,7 @@ class ACSMInput(FileTypePlugin):
if not are_ade_version_lists_valid():
print("{0} v{1}: ADE version list mismatch, please open a bug report.".format(PLUGIN_NAME, PLUGIN_VERSION))
self.unblock()
return path_to_ebook
print("{0} v{1}: Try to fulfill ...".format(PLUGIN_NAME, PLUGIN_VERSION))
@ -543,9 +611,14 @@ class ACSMInput(FileTypePlugin):
# Return path - either the original one or the one modified by the other plugins.
self.unblock()
return rpl
self.unblock()
return path_to_ebook
except:
self.unblock()
traceback.print_exc()
return path_to_ebook

View File

@ -55,6 +55,7 @@ class ConfigWidget(QWidget):
self.tempdeacsmprefs['notify_fulfillment'] = self.deacsmprefs['notify_fulfillment']
self.tempdeacsmprefs['detailed_logging'] = self.deacsmprefs['detailed_logging']
self.tempdeacsmprefs['delete_acsm_after_fulfill'] = self.deacsmprefs['delete_acsm_after_fulfill']
self.tempdeacsmprefs['allow_parallel_fulfillment'] = self.deacsmprefs['allow_parallel_fulfillment']
self.tempdeacsmprefs['list_of_rented_books'] = self.deacsmprefs['list_of_rented_books']
@ -176,6 +177,11 @@ class ConfigWidget(QWidget):
self.chkDeleteAfterFulfill.toggled.connect(self.toggle_acsm_delete)
layout.addWidget(self.chkDeleteAfterFulfill)
self.chkParallelFulfill = QtGui.QCheckBox("Allow parallel fulfillment")
self.chkParallelFulfill.setToolTip("Default: True\n\nIf this is enabled (which was the default in previous versions), \nthe plugin will import multiple ACSM files simultaneously when you add more than one.\n\nIf this is disabled, it will add them one after another like ADE.")
self.chkParallelFulfill.setChecked(self.tempdeacsmprefs["allow_parallel_fulfillment"])
layout.addWidget(self.chkParallelFulfill)
# Key shortcut Ctrl+Shift+D / Cmd+Shift+D to remove authorization, just like in ADE.
self.deauthShortcut = QShortcut(QKeySequence("Ctrl+Shift+D"), self)
self.deauthShortcut.activated.connect(self.delete_ade_auth)
@ -1269,6 +1275,7 @@ class ConfigWidget(QWidget):
self.deacsmprefs.set('notify_fulfillment', self.chkNotifyFulfillment.isChecked())
self.deacsmprefs.set('detailed_logging', self.chkDetailedLogging.isChecked())
self.deacsmprefs.set('delete_acsm_after_fulfill', self.chkDeleteAfterFulfill.isChecked())
self.deacsmprefs.set('allow_parallel_fulfillment', self.chkParallelFulfill.isChecked())
self.deacsmprefs.writeprefs()
def load_resource(self, name):

View File

@ -28,8 +28,12 @@ class ACSMInput_Prefs():
self.deacsmprefs.defaults['notify_fulfillment'] = True
self.deacsmprefs.defaults['detailed_logging'] = False
self.deacsmprefs.defaults['delete_acsm_after_fulfill'] = False
self.deacsmprefs.defaults['allow_parallel_fulfillment'] = True
self.deacsmprefs.defaults['loan_identifier_token'] = 0
self.deacsmprefs.defaults['fulfillment_block_token'] = 0
self.deacsmprefs.defaults['fulfillment_block_time'] = 0
self.deacsmprefs.defaults['list_of_rented_books'] = []