View Single Post
Old 08-18-2021, 04:41 AM   #656
capink
Wizard
capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.
 
Posts: 1,208
Karma: 1995558
Join Date: Aug 2015
Device: Kindle
Convert book custom action

Here is a minimal "Convert Books" action based on calibre's auto-convert. It is non interactive, and follows the same settings as calibre's auto-convert.

Code:
from PyQt5.Qt import (Qt, QApplication, QWidget, QVBoxLayout, QHBoxLayout, QGridLayout,
                      QCheckBox, QIcon, QGroupBox)

from calibre import prints
from calibre.constants import DEBUG
from calibre.utils.date import now

from calibre_plugins.action_chains.actions.base import ChainAction
from calibre_plugins.action_chains.actions.calibre_actions import unfinished_job_ids, responsive_wait, responsive_wait_until

class ConfigWidget(QWidget):

    def __init__(self, plugin_action):
        QWidget.__init__(self)
        self.plugin_action = plugin_action
        self.gui = plugin_action.gui
        self.db = self.gui.current_db
        self._init_controls()

    def _init_controls(self):

        l = QVBoxLayout()
        self.setLayout(l)

        job_wait_chk = self.job_wait_chk = QCheckBox(_('Wait until all convert jobs finish.'))
        l.addWidget(job_wait_chk)
        job_wait_chk.setChecked(True)

        l.addStretch(1)

    def load_settings(self, settings):
        if settings:
            self.job_wait_chk.setChecked(settings['wait_jobs'])

    def save_settings(self):
        settings = {}
        settings['wait_jobs'] = self.job_wait_chk.isChecked()
        return settings

class ChainsConvertAction(ChainAction):

    name = 'Convert Books'
    support_scopes = True

    def run(self, gui, settings, chain):
        db = gui.current_db
        book_ids = chain.scope().get_book_ids()
        start_time = now()

        jobs_before_ids = unfinished_job_ids(gui)

        gui.iactions['Convert Books'].auto_convert_auto_add(book_ids)

        wait_jobs = settings.get('wait_jobs', True)

        if wait_jobs:            
            # wait for jobs spawned by action to kick in
            responsive_wait(1)
            
            # save ids of jobs started after running the action                    
            ids_jobs_by_action = unfinished_job_ids(gui).difference(jobs_before_ids)

            # wait for jobs to finish
            responsive_wait_until(lambda: ids_jobs_by_action.intersection(unfinished_job_ids(gui)) == set())         


    def config_widget(self):
        return ConfigWidget

Last edited by capink; 08-19-2021 at 05:11 AM.
capink is online now   Reply With Quote