Register Guidelines E-Books Search Today's Posts Mark Forums Read

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

Notices

Reply
 
Thread Tools Search this Thread
Old 02-23-2021, 04:18 AM   #361
capink
Evangelist
capink knows more than wikipediacapink knows more than wikipediacapink knows more than wikipediacapink knows more than wikipediacapink knows more than wikipediacapink knows more than wikipediacapink knows more than wikipediacapink knows more than wikipediacapink knows more than wikipediacapink knows more than wikipediacapink knows more than wikipedia
 
Posts: 442
Karma: 47410
Join Date: Aug 2015
Device: Kindle
Quote:
Originally Posted by chaley View Post
@capink: specifying a separator in a for statement and the function list_remove_duplicates(l, sep) are now available in master source.
Thanks for both. I don't seem to know how to use the new for loop. I am trying to modify your previous template to this:

Code:
program:
	all_authors = category_items('authors');
	res = '';
	for a in '&' all_authors:
		ratings = from_search('rating', strcat('rating:true and author"=', a, '"'));
		if ratings then
			avg = divide(mean(ratings), 2);
			res = list_union(res, strcat(a, ':', avg), ',')
		fi
	rof;
	res
Obviously I am doing something wrong (and probably silly) here.

Last edited by capink; 02-23-2021 at 04:24 AM.
capink is offline   Reply With Quote
Old 02-23-2021, 04:27 AM   #362
chaley
Grumpy old git
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.
 
chaley's Avatar
 
Posts: 9,737
Karma: 3174216
Join Date: Jan 2010
Location: UK
Device: Reader
Quote:
Originally Posted by capink View Post
Thanks for both. I don't seem to know how to use the new for loop. I am trying to modify your previous template to this:
The syntax is
Code:
	for a in authors separator '&':
I used the keyword 'separator' to remove some ambiguity in the language parser. Note that the 'thing' following the keyword is an arbitrary expression. You can do something like this, admittedly a bit strange:
Code:
	for a in authors separator if something then '&' else ',' fi:
chaley is offline   Reply With Quote
Old 02-23-2021, 05:33 AM   #363
capink
Evangelist
capink knows more than wikipediacapink knows more than wikipediacapink knows more than wikipediacapink knows more than wikipediacapink knows more than wikipediacapink knows more than wikipediacapink knows more than wikipediacapink knows more than wikipediacapink knows more than wikipediacapink knows more than wikipediacapink knows more than wikipedia
 
Posts: 442
Karma: 47410
Join Date: Aug 2015
Device: Kindle
Quote:
Originally Posted by chaley View Post
[/CODE]I used the keyword 'separator' to remove some ambiguity in the language parser.
That makes senese. It is now working for me.

Code:
program:
	book_ids = from_selection('id');
	all_authors = category_items('authors',book_ids);
	res = '';
	for a in all_authors separator '&':
		ratings = from_search('rating', strcat('rating:true and author:"=', a, '"'));
		if ratings then
			avg = divide(mean(ratings), 2);
			res = list_union(res, strcat(a, ':', avg), ',')
		fi
	rof;
	res
Using all authors without restricting them using from_search() or from_selection() on large libraries is not recommended as it is verrrrrrry slow.

Quote:
Originally Posted by chaley View Post
Note that the 'thing' following the keyword is an arbitrary expression.
Wow. I will sure come up with a use for that sometime in the future.
capink is offline   Reply With Quote
Old 02-23-2021, 07:54 AM   #364
capink
Evangelist
capink knows more than wikipediacapink knows more than wikipediacapink knows more than wikipediacapink knows more than wikipediacapink knows more than wikipediacapink knows more than wikipediacapink knows more than wikipediacapink knows more than wikipediacapink knows more than wikipediacapink knows more than wikipediacapink knows more than wikipedia
 
Posts: 442
Karma: 47410
Join Date: Aug 2015
Device: Kindle
Also turns out subitems() automatically removes duplicates anyway. So this works fine to get the top level genres:

Code:
program:
    genres = category_items('#genre');
    subitems(genres, 0, 1)
capink is offline   Reply With Quote
Old 02-23-2021, 04:58 PM   #365
ownedbycats
Wizard
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: 2,358
Karma: 10748428
Join Date: Oct 2018
Device: Kobo Aura HD
Question: In View Manager, I have a view called "On Device." I use a one-action chain and Event Manager to switch to it when the device plug in.

Is it possible to make a condition to make it not run if the view is already set to On Device?

Thank you!

Last edited by ownedbycats; 02-23-2021 at 05:07 PM.
ownedbycats is online now   Reply With Quote
Old 02-24-2021, 03:16 AM   #366
capink
Evangelist
capink knows more than wikipediacapink knows more than wikipediacapink knows more than wikipediacapink knows more than wikipediacapink knows more than wikipediacapink knows more than wikipediacapink knows more than wikipediacapink knows more than wikipediacapink knows more than wikipediacapink knows more than wikipediacapink knows more than wikipedia
 
Posts: 442
Karma: 47410
Join Date: Aug 2015
Device: Kindle
You will need a custom template function to check for that. Taking a quick look at the view manager code, this can be easily done using this template:

Code:
from calibre_plugins.action_chains.templates import TemplateFunction

class ViewManagerLastVIew(TemplateFunction):

    name = 'view_manager_last_view'
    arg_count = 0

    def evaluate(self, formatter, kwargs, mi, locals):
        import calibre_plugins.view_manager.config as cfg
        gui = self.plugin_action.gui
        library_config = cfg.get_library_config(gui.current_db)
        return library_config.get(cfg.KEY_LAST_VIEW, '')
Copy/Paste the above code into the module manager. Now you can use this function in a condition as follows:

Code:
Template        : program: view_manager_last_view()
Datatype        : text
Comparison      : !=
Condition Value : On Device
However, your request uncovers another problem. When the conditions are not met, an error dialog will pop-up. This might not be suitable for events. I can turn this off for events and let it fail silently, or add an option in the conditions dialog to make the error dialog optional. Will think about it to see which of these two options is more suitable, and then add it later.

In the mean time, you can test the above to see whether it works.
capink is offline   Reply With Quote
Old 02-24-2021, 03:21 AM   #367
ownedbycats
Wizard
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: 2,358
Karma: 10748428
Join Date: Oct 2018
Device: Kobo Aura HD
A while ago I did some experimenting and found that adding the condition to the actions rather than the chain itself doesn't result in error. This is clumsy though, and doesn't work if you want to use other conditions for the specific actions.

But it works well enough for a single-action chain, and the code above worked.

Last edited by ownedbycats; 02-24-2021 at 03:25 AM.
ownedbycats is online now   Reply With Quote
Old 03-07-2021, 08:30 AM   #368
Venia Legendi
Member
Venia Legendi began at the beginning.
 
Venia Legendi's Avatar
 
Posts: 10
Karma: 10
Join Date: Apr 2013
Device: lots of, frequently changing
Edit metadata in bulk with preset

Hello, what a great plugin, I have lots of very convenient chains.

Is it possible to call the calibre action "Edit metadata in bulk" with preset to "Change cover: Set from e-book files" and directly execute this? I think this should work with a module, but I have no idea how to start here. Any hints are highly appreciated, thanks.
Venia Legendi is offline   Reply With Quote
Old 03-07-2021, 11:48 AM   #369
capink
Evangelist
capink knows more than wikipediacapink knows more than wikipediacapink knows more than wikipediacapink knows more than wikipediacapink knows more than wikipediacapink knows more than wikipediacapink knows more than wikipediacapink knows more than wikipediacapink knows more than wikipediacapink knows more than wikipediacapink knows more than wikipedia
 
Posts: 442
Karma: 47410
Join Date: Aug 2015
Device: Kindle
You can try the following code (copy/past into the module editor):

Code:
from calibre.ebooks.metadata.meta import metadata_from_formats
from calibre_plugins.action_chains.actions.base import ChainAction

class CoverFromBook(ChainAction):

    name = 'Cover from book'      

    def run(self, gui, settings, chain):
        db = gui.current_db.new_api
        rows = gui.current_view().selectionModel().selectedRows()
        book_ids = [ gui.library_view.model().db.id(row.row()) for row in rows ]
        for book_id in book_ids:
            fmts = db.formats(book_id, verify_formats=False)
            paths = list(filter(None, [db.format_abspath(book_id, fmt) for fmt in fmts]))
            mi = metadata_from_formats(paths)
            if mi.cover_data:
                cdata = mi.cover_data[-1]
            if cdata is not None:
                db.set_cover({book_id: cdata})

Last edited by capink; 03-23-2021 at 01:33 PM.
capink is offline   Reply With Quote
Old 03-07-2021, 03:12 PM   #370
Venia Legendi
Member
Venia Legendi began at the beginning.
 
Venia Legendi's Avatar
 
Posts: 10
Karma: 10
Join Date: Apr 2013
Device: lots of, frequently changing
Thumbs up Thank you

Quote:
Originally Posted by capink View Post
You can try the following code (copy/past into the module editor):
Wow, that works like charm, you made my day, thank you.
Venia Legendi is offline   Reply With Quote
Old 03-11-2021, 11:34 PM   #371
ownedbycats
Wizard
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: 2,358
Karma: 10748428
Join Date: Oct 2018
Device: Kobo Aura HD
Did program: connected_device_name('main') get changed? This condition always shows up as true even with nothing plugged in:

Click image for larger version

Name:	2021-03-12 00_33_15-Evaluate Condition.png
Views:	21
Size:	15.4 KB
ID:	185867

EDIT: I just noticed the addition of the new option. Sorry.
ownedbycats is online now   Reply With Quote
Old 03-12-2021, 04:23 AM   #372
capink
Evangelist
capink knows more than wikipediacapink knows more than wikipediacapink knows more than wikipediacapink knows more than wikipediacapink knows more than wikipediacapink knows more than wikipediacapink knows more than wikipediacapink knows more than wikipediacapink knows more than wikipediacapink knows more than wikipediacapink knows more than wikipedia
 
Posts: 442
Karma: 47410
Join Date: Aug 2015
Device: Kindle
Some conditions slowed down the menu loading (e.g selection_count() when thousands of books are selected), so I made disabling menu entries based on conditions optional in version 0.6.5.
capink is offline   Reply With Quote
Old 03-13-2021, 01:19 AM   #373
compurandom
Guru
compurandom ought to be getting tired of karma fortunes by now.compurandom ought to be getting tired of karma fortunes by now.compurandom ought to be getting tired of karma fortunes by now.compurandom ought to be getting tired of karma fortunes by now.compurandom ought to be getting tired of karma fortunes by now.compurandom ought to be getting tired of karma fortunes by now.compurandom ought to be getting tired of karma fortunes by now.compurandom ought to be getting tired of karma fortunes by now.compurandom ought to be getting tired of karma fortunes by now.compurandom ought to be getting tired of karma fortunes by now.compurandom ought to be getting tired of karma fortunes by now.
 
Posts: 664
Karma: 294342
Join Date: Jun 2015
Device: kobo aura h2o, kobo forma
Quote:
Originally Posted by capink View Post
Some conditions slowed down the menu loading (e.g selection_count() when thousands of books are selected), so I made disabling menu entries based on conditions optional in version 0.6.5.
Would it be faster / possible to have a function that returned selection count as 0, 1, or many? That might be a better fit for determining if a menu entry should be enabled or not.
compurandom is offline   Reply With Quote
Old 03-13-2021, 03:59 AM   #374
capink
Evangelist
capink knows more than wikipediacapink knows more than wikipediacapink knows more than wikipediacapink knows more than wikipediacapink knows more than wikipediacapink knows more than wikipediacapink knows more than wikipediacapink knows more than wikipediacapink knows more than wikipediacapink knows more than wikipediacapink knows more than wikipedia
 
Posts: 442
Karma: 47410
Join Date: Aug 2015
Device: Kindle
You can already do that by using the selection_count() function which returns the exact number of selected books, and combine that with the comparison operators (= | != | > |<) in the condition evaluator dialog. (see attached screenshot)

Having a function that returns the selection count as 0, 1, or many will not improve performance as it still needs to make the same call that takes time when a lot of books are selected:

Code:
len(gui.current_view().selectionModel().selectedRows())
Attached Thumbnails
Click image for larger version

Name:	1.png
Views:	15
Size:	50.6 KB
ID:	185907  
capink is offline   Reply With Quote
Old 03-13-2021, 06:51 PM   #375
ownedbycats
Wizard
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: 2,358
Karma: 10748428
Join Date: Oct 2018
Device: Kobo Aura HD
I have this code:

Code:
      {
        "active": true,
        "chain_settings": {
          "chain_links": [
            {
              "action_name": "Single Field Edit",
              "action_settings": {
                "col_name": "#kobocoll",
                "template": "program:\n\tg = field('#genres');\n\tk = field('#kobocoll');\n\tif list_contains(g, ',', '^Fanfiction$', '1', '') then\n\t\tk = list_union(k, 'Fanfiction', ',')\n\tfi;\n\tif list_contains(g, ',', '^Cozy Mystery$', '1', '') then\n\t\tk = list_union(k, 'Cozy Mysteries', ',')\n\tfi;\n\tif list_contains(g, ',', '^Horses$', '1', '') then\n\t\tk = list_union(k, 'Horses', ',')\n\tfi;\n\tif list_contains(g, ',', '^(Fantasy|Science Fiction)$', '1', '') then\n\t\tk = list_union(k, 'Fantasy & Sci-Fi', ',')\n\tfi;\n\tk",
                "value_type": "template"
              },
              "comment": "Update #kobocoll column"
            },
            {
              "action_name": "Calibre Actions",
              "action_settings": {
                "disable_busy_cursor": false,
                "selection": [
                  {
                    "display": "Send to device",
                    "path": [
                      "Send To Device"
                    ]
                  }
                ]
              },
              "comment": "Send to device if device is connected",
              "condition_settings": {
                "cmp_type": "=",
                "datatype": "text",
                "template": "program: connected_device_name('main')",
                "value": "Kobo Aura HD"
              }
            },
            {
              "action_name": "Calibre Actions",
              "action_settings": {
                "disable_busy_cursor": false,
                "selection": [
                  {
                    "display": "Add to list: Send to Device",
                    "path": [
                      "Reading List",
                      "Add to list",
                      "Add to list: Send to Device"
                    ],
                    "paths_text": "Add to list|Add to list: Send to Device"
                  }
                ]
              },
              "comment": "Add to \"Send to Device\" list if device is not connected",
              "condition_settings": {
                "cmp_type": "=",
                "datatype": "text",
                "template": "program: connected_device_name('main')",
                "value": ""
              }
            },
            {
              "action_name": "Single Field Edit",
              "action_settings": {
                "col_name": "#onkobo",
                "template": "program:\n\tif and(\tcheck_yes_no('#onkobo', '', '', 1),) then \t'False' \n\telse 'True'\n\tfi",
                "value_type": "template"
              },
              "comment": "Set #onkobo column"
            }
          ],
          "refresh_gui": true
        },
        "image": "sync.png",
        "menuText": "Send to Device",
        "subMenu": ""
      },
However i noticed a slight glitch with it. When I eject my Kobo, sometimes the conditional doesn't update (the template value in the evaluator confirms this) and it tries to do the wrong actions (and predictably fail) the next time I run the chain. When this happens my two greyed-out chains are also available.

Any idea why?
Attached Files
File Type: zip Send to Device Chain.zip (3.7 KB, 12 views)

Last edited by ownedbycats; 03-13-2021 at 06:59 PM.
ownedbycats is online now   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[GUI Plugin] Save Virtual Libraries To Column (GUI) chaley Plugins 14 04-04-2021 05:25 AM
[GUI Plugin] KindleUnpack - The Plugin DiapDealer Plugins 468 02-07-2021 04:07 PM
[GUI Plugin] Manga plugin mastertea Plugins 0 10-20-2019 02:45 PM
[GUI Plugin] Wordpress frescogamba Plugins 11 04-06-2015 09:09 PM
[GUI Plugin] Plugin Updater **Deprecated** kiwidude Plugins 159 06-19-2011 12:27 PM


All times are GMT -4. The time now is 01:29 AM.


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