Register Guidelines E-Books Today's Posts Search

Go Back   MobileRead Forums > E-Book Software > Calibre > Plugins

Notices

Reply
 
Thread Tools Search this Thread
Old 02-13-2021, 07:36 AM   #316
chaley
Grand Sorcerer
chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.
 
Posts: 12,447
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by capink View Post
I am leaning towards the second solution, but since it breaks compatibility, I decided to post here first to seek input from others. As a transitional step, I might post a new version that implements the first solution, and then decide whether to pursue the second option or not.
If you do #1 then be careful to be sure that the setting is always restored. People complained for years about Find Duplicates leaving the setting as "Highlight". I think that has been fixed, but in the meantime it generated a lot of traffic.

Also, doing something "temporarily" probably means it is permanent.
chaley is offline   Reply With Quote
Old 02-13-2021, 08:18 AM   #317
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,196
Karma: 1995558
Join Date: Aug 2015
Device: Kindle
Quote:
Originally Posted by chaley View Post
If you do #1 then be careful to be sure that the setting is always restored. People complained for years about Find Duplicates leaving the setting as "Highlight". I think that has been fixed, but in the meantime it generated a lot of traffic.
The action will be wrapped in a one try .. finally block that restores the highlight option. So it should be OK unless a power outage or a cold restart is carried out.

The problem with the Find Duplicates is that the changing and restoring happened in two different (interactive) steps, which left a wide window for things to go wrong. The problem — as you yourself discovered — was that the restore step was not carried out if the user closed calibre without clearing the duplicates results first.

Last edited by capink; 02-13-2021 at 08:20 AM.
capink is offline   Reply With Quote
Old 02-13-2021, 12:02 PM   #318
jbacelar
Interested in the matter
jbacelar ought to be getting tired of karma fortunes by now.jbacelar ought to be getting tired of karma fortunes by now.jbacelar ought to be getting tired of karma fortunes by now.jbacelar ought to be getting tired of karma fortunes by now.jbacelar ought to be getting tired of karma fortunes by now.jbacelar ought to be getting tired of karma fortunes by now.jbacelar ought to be getting tired of karma fortunes by now.jbacelar ought to be getting tired of karma fortunes by now.jbacelar ought to be getting tired of karma fortunes by now.jbacelar ought to be getting tired of karma fortunes by now.jbacelar ought to be getting tired of karma fortunes by now.
 
jbacelar's Avatar
 
Posts: 421
Karma: 426094
Join Date: Dec 2011
Location: Spain, south coast
Device: Pocketbook InkPad 3
Quote:
Originally Posted by capink View Post
As a transitional step, I might post a new version that implements the first solution, and then decide whether to pursue the second option or not.
That seems like the most sensible step to me.
jbacelar is offline   Reply With Quote
Old 02-15-2021, 07:30 AM   #319
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,196
Karma: 1995558
Join Date: Aug 2015
Device: Kindle
Quote:
Originally Posted by capink View Post
I am leaning towards the second solution, but since it breaks compatibility, I decided to post here first to seek input from others. As a transitional step, I might post a new version that implements the first solution, and then decide whether to pursue the second option or not.
I decided to stick with option 1. Option 2 will make it impossible to combine a search with some other selection choices like modified by last action or modified by chain. New version already attached to first post.
capink is offline   Reply With Quote
Old 02-16-2021, 01:24 AM   #320
ownedbycats
Custom User Title
ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.
 
ownedbycats's Avatar
 
Posts: 10,993
Karma: 75337983
Join Date: Oct 2018
Location: Canada
Device: Kobo Libra H2O, formerly Aura HD
Question: Is there any known way to deliberately mark a book, rather than toggle mark? With the latter, already-marked books will become unmarked.
ownedbycats is online now   Reply With Quote
Old 02-16-2021, 01:53 AM   #321
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,196
Karma: 1995558
Join Date: Aug 2015
Device: Kindle
Yes. By using mark functionality provided by the plugin instead of calibre. Single Field Action > choose marked field. As usual you have to choice of runtime and predefined.
capink is offline   Reply With Quote
Old 02-16-2021, 02:48 AM   #322
ownedbycats
Custom User Title
ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.
 
ownedbycats's Avatar
 
Posts: 10,993
Karma: 75337983
Join Date: Oct 2018
Location: Canada
Device: Kobo Libra H2O, formerly Aura HD
I never noticed that. Thank you!
ownedbycats is online now   Reply With Quote
Old 02-17-2021, 12:05 PM   #323
chaley
Grand Sorcerer
chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.
 
Posts: 12,447
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Regarding marked books: I added a template function is_marked() that returns the list of comma-separated mark names if it exists, "true" if the book is marked with a non-named mark, or the empty string if the book isn't marked. The new function is in calibre source now.
chaley is offline   Reply With Quote
Old 02-17-2021, 01:27 PM   #324
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,196
Karma: 1995558
Join Date: Aug 2015
Device: Kindle
Should be useful for conditionally changing the mark values, among other things. An immediate use that jumps to mind to toggling a specific text mark, using templates.
capink is offline   Reply With Quote
Old 02-18-2021, 07:23 AM   #325
chaley
Grand Sorcerer
chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.
 
Posts: 12,447
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
@capink: I have been thinking about something I would find useful, but a) I am not sure anyone else would, and b) whether the idea fits inside your plans/ideas for action chains.

The idea: sometimes I want to compute some value for a set of books. Some easy examples are the sum of some column, how many series does an author write, names of series for an author, etc. These can all be computed using a template if
  • it could be run on a selection
  • it could pass information between invocations
  • the computed information can be displayed
The "passing information" could be solved by a template function that modifies the globals dict. This works as long as the same dict is passed to the template for each book, which I suspect is what you do for a chain.

The "run on selection" could be done with a new built-in action that runs the provided template on each selected book. The information could be displayed by providing a built-in action that shows the values in the globals dict in a dialog. If really fancy it could take a set of pairs ('global_var_name': 'display_name'). The action would display the listed globals using the display_name. Alternatively, computing and displaying might be done by changing the "formula" action to (optionally?) run over the selection, and to display some values from the globals dict.

With these changes I could create an action that sets up variables (if needed), runs a template that computes what is needed and saves the result to globals, then runs an action that displays the answer.

To continue the example, a template that counts the number of series in a selection could be:
Code:
program:
	s = field('series');
	globals(series_seen='', series_count=0);
	series_seen = list_union(series_seen, s, ',');
	series_count = count(series_seen, ',');
# the new function to change the globals dict
	set_globals(series_seen, series_count)
What do you think?
chaley is offline   Reply With Quote
Old 02-18-2021, 10:17 AM   #326
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,196
Karma: 1995558
Join Date: Aug 2015
Device: Kindle
The idea of template modifying values that are remembered between invocations is interesting.

I think the action and the display should be separated. We run the action that iterates over selected books, and modify the global dict. After that the display can be handled by the formulas action, which already has access to the global dict. This way, any other action that uses templates, can be preceded by this new action (now that the display is optional and separate), and access whatever values it writes to the global dict.

All this action would need to do is loop over all selected books, and run the template for each one. Here is what I think can be a preliminary implementation for this action:
Code:
# python3 compatibility
from six import text_type as unicode

from calibre import prints
from calibre.constants import DEBUG
from calibre.gui2 import error_dialog
from calibre.ebooks.metadata.book.formatter import SafeFormat

from calibre_plugins.action_chains.actions.base import ChainAction
from calibre_plugins.action_chains.templates import TemplateBox, check_template, get_metadata_object, TEMPLATE_ERROR

class TemplateIteratorDialog(TemplateBox):
    def __init__(self, parent, plugin_action, action, name, title):
        self.plugin_action = plugin_action
        self.action = action
        self.gui = plugin_action.gui
        self.db = self.gui.current_db
        mi = get_metadata_object(self.gui)
        TemplateBox.__init__(
                self,
                parent,
                plugin_action,
                template_text='',
                placeholder_text=_('Enter a template here. It will be iterated over all selected books'),
                mi=mi
            )
        self.setWindowTitle(title)

    def load_settings(self, settings):
        if settings:
            template = settings['template']
            self.textbox.insertPlainText(template)

    def save_settings(self):
        settings = {}
        settings['template'] = unicode(self.textbox.toPlainText()).rstrip()
        return settings

    def accept(self):
        self.settings = self.save_settings()
        # validate settings
        is_valid = self.action.validate(self.settings)
        if is_valid is not True:
            msg, details = is_valid
            error_dialog(
                self,
                msg,
                details,
                show=True
            )
            return
        TemplateBox.accept(self)

class TemplateIteratorAction(ChainAction):

    name = 'Template Iterator'

    def run_template_for_book(self, db, template, book_id, chain_loop):
        mi = db.new_api.get_proxy_metadata(book_id)

        template_functions = self.plugin_action.template_functions
        template_output = SafeFormat().safe_format(template, mi, TEMPLATE_ERROR, mi,
                                                   global_vars=chain_loop.chain_vars,
                                                   template_functions=template_functions)

        return template_output

    def run(self, gui, settings, chain_loop):
        db = gui.current_db
        
        rows = gui.current_view().selectionModel().selectedRows()
        book_ids = [ gui.library_view.model().db.id(row.row()) for row in rows ]
                    
        template = settings['template']
        for book_id in book_ids:
            template_output = self.run_template_for_book(db, template, book_id, chain_loop)

    def config_widget(self):
        return TemplateIteratorDialog
This action can be tested now as a custom action, before it is included in the plugin.

I still have to ponder on this for sometime, before jumping into final implementation, to get it right first. I am also struggling with name of this new action. Changing it later would break compatibility. Maybe someone can help.

Edit: Maybe make the display part of the same action, and make it optional instead. Not yet sure about this.

Last edited by capink; 02-18-2021 at 10:19 AM.
capink is offline   Reply With Quote
Old 02-18-2021, 11:11 AM   #327
chaley
Grand Sorcerer
chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.
 
Posts: 12,447
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by capink View Post
The idea of template modifying values that are remembered between invocations is interesting.
I submitted changes to Kovid to implement set_globals(). EDIT: The changes are now in master source.
Quote:
I think the action and the display should be separated. We run the action that iterates over selected books, and modify the global dict. After that the display can be handled by the formulas action, which already has access to the global dict. This way, any other action that uses templates, can be preceded by this new action (now that the display is optional and separate), and access whatever values it writes to the global dict.
I think you are right, this way permits more generality. One could run more than one iterative template, perhaps separated by other actions. Then when ready the user would ask that the values be displayed. Or perhaps the computed values would be used in action that modifies metadata.
Quote:
This action can be tested now as a custom action, before it is included in the plugin.
This is why I added set_globals. Getting some usage feedback would be good.
Quote:
I still have to ponder on this for sometime, before jumping into final implementation, to get it right first. I am also struggling with name of this new action. Changing it later would break compatibility. Maybe someone can help.

Edit: Maybe make the display part of the same action, and make it optional instead. Not yet sure about this.
Yes, getting it right first time is a good thing.

Last edited by chaley; 02-18-2021 at 11:19 AM. Reason: The changes were accepted.
chaley is offline   Reply With Quote
Old 02-18-2021, 05:56 PM   #328
ownedbycats
Custom User Title
ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.
 
ownedbycats's Avatar
 
Posts: 10,993
Karma: 75337983
Join Date: Oct 2018
Location: Canada
Device: Kobo Libra H2O, formerly Aura HD
Quote:
Originally Posted by chaley View Post
The idea: sometimes I want to compute some value for a set of books. Some easy examples are the sum of some column, how many series does an author write, names of series for an author, etc.
Could it be used to, for instance, put the average rating of a category into a custom column for icon rules? (Not sure how it would handle multiple categories though.)
ownedbycats is online now   Reply With Quote
Old 02-18-2021, 06:18 PM   #329
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,196
Karma: 1995558
Join Date: Aug 2015
Device: Kindle
While testing the new action, I discovered the formulas action in the current version does not have access to the chain vars (strange that I overlooked this). Should be easy to correct. Will post a new version tomorrow, as it is time to go to bed now.
capink is offline   Reply With Quote
Old 02-19-2021, 03:30 AM   #330
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,196
Karma: 1995558
Join Date: Aug 2015
Device: Kindle
New version now attached to first post with modifications to the formulas to give access to the chain_vars. Also functions defined by the formulas action can be used from other actions as well.

I tested the new action with the set_globals and it is working fine so far.
capink is offline   Reply With Quote
Reply


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[Editor Plugin] Editor Chains capink Plugins 106 06-17-2025 05:36 PM
Action Chains Resources capink Plugins 77 06-16-2025 12:45 PM
[GUI Plugin] Noosfere_util, a companion plugin to noosfere DB lrpirlet Plugins 2 08-18-2022 03:15 PM
[GUI Plugin] Save Virtual Libraries To Column (GUI) chaley Plugins 14 04-04-2021 05:25 AM


All times are GMT -4. The time now is 12:20 AM.


MobileRead.com is a privately owned, operated and funded community.