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 03-14-2021, 09:56 AM   #376
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 ownedbycats View Post
I have this code:

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?
I am not sure what is happening here. Next time this occurs, please test the condition also in calibre's template tester. If it returns the device name after you have removed it, open a bug report about it. If it is happening only with action chains condition evaluator, report back here.
capink is offline   Reply With Quote
Old 03-14-2021, 02:49 PM   #377
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 not sure what is happening here. Next time this occurs, please test the condition also in calibre's template tester. If it returns the device name after you have removed it, open a bug report about it. If it is happening only with action chains condition evaluator, report back here.
I looked carefully at the mechanism that connected_device_name() uses and didn't see a way that it could return anything but '' if no device is showing on calibre's toolbar. It is possible that there can be a second or two delay after the eject, but in this case the calibre will still be showing the "toolbar when a device is connected".

Of course, me not seeing anything isn't definitive.
chaley is offline   Reply With Quote
Advert
Old 03-14-2021, 04:50 PM   #378
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: 11,000
Karma: 75337983
Join Date: Oct 2018
Location: Canada
Device: Kobo Libra H2O, formerly Aura HD
Quote:
Originally Posted by capink View Post
I am not sure what is happening here. Next time this occurs, please test the condition also in calibre's template tester. If it returns the device name after you have removed it, open a bug report about it. If it is happening only with action chains condition evaluator, report back here.
I'll do that then. I wonder if something might've gotten stuck in memory as that's the only thing I can think of. It does clear up on restart Calibre though.
ownedbycats is offline   Reply With Quote
Old 03-15-2021, 06:14 AM   #379
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 ownedbycats View Post
I'll do that then. I wonder if something might've gotten stuck in memory as that's the only thing I can think of. It does clear up on restart Calibre though.
I am posting part of my response to your bug report here just in case it helps trigger some ideas.
Quote:
Did you try connected_device_name() in the template tester in the library, outside of action chains? I ask because I see no way that the template can return a value once the device is fully disconnected.

Do note that connected_device_name() will return a non-empty value during the disconnect process. In particular it still has a value when the action chains device disconnected event occurs. If action chains remembers the result of a call to connected_device_name() during event processing then it will remember the wrong value.
chaley is offline   Reply With Quote
Old 03-15-2021, 07:01 AM   #380
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 not sure what is happening here. Next time this occurs, please test the condition also in calibre's template tester. If it returns the device name after you have removed it, open a bug report about it. If it is happening only with action chains condition evaluator, report back here.
This is definitely a problem with the semantics of the device_connection_changed signal. The device information available to action chains is still set/valid during the disconnect event. I can see this by adding print() to the disconnect event template.

I can "fix" this in calibre but that will change the information available to every client of the device_connection_changed signal. I am willing to do this if Kovid agrees.

Action chains can fix this by using a QTimer.singleShot(0, ...) to run anything associated with a disconnect event.
chaley is offline   Reply With Quote
Advert
Old 03-15-2021, 09:12 AM   #381
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 action chains remembers the result of a call to connected_device_name() during event processing then it will remember the wrong value.
No, action chains does not remember the value of connected_device_name().

Quote:
Originally Posted by chaley View Post
This is definitely a problem with the semantics of the device_connection_changed signal. The device information available to action chains is still set/valid during the disconnect event. I can see this by adding print() to the disconnect event template.

I can "fix" this in calibre but that will change the information available to every client of the device_connection_changed signal. I am willing to do this if Kovid agrees.

Action chains can fix this by using a QTimer.singleShot(0, ...) to run anything associated with a disconnect event.
I tried QTimer.singleShot(0, ...) and for some reason it is not working (print() returns the name of the device during the disconnect event.). I started playing with timeout value and found that QTimer.singleShot(5000, ...) works. I will add this to next release (I should have one later tonight or tomorrow). However, I am not sure this will solve the problem if it occurs outside the action chains disconnect event.
capink is offline   Reply With Quote
Old 03-15-2021, 09:23 AM   #382
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 tried QTimer.singleShot(0, ...) and for some reason it is not working (print() returns the name of the device during the disconnect event.). I started playing with timeout value and found that QTimer.singleShot(5000, ...) works. I will add this to next release (I should have one later tonight or tomorrow). However, I am not sure this will solve the problem if it occurs outside the action chains disconnect event.
A fix is in calibre source now. Try that before you change anything.
chaley is offline   Reply With Quote
Old 03-15-2021, 09:38 AM   #383
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
Still showing the device name. Should I update anything beside sr/calibre/gui2/device.py?
capink is offline   Reply With Quote
Old 03-15-2021, 09:48 AM   #384
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
Still showing the device name. Should I update anything beside sr/calibre/gui2/device.py?
No, that is what must be updated.

It works for me. Here is the log:
Code:
Action Chains: Starting event: Device Disconnected
calling method (init_cache) of obj (selection_count)
('template', '')
calling method (flush_cache) of obj (selection_count)
Action Chains: Starting chain: Test bool
Action Chains: Test bool: starting action No. 1: Single Field Edit
('template', '')
Action Chains: chain (Test bool) finished in: 0:00:00.075093
Action Chains: event (Device Disconnected) finished in: 0.07902860641479492
The action triggered by the event manager is
Code:
{
              "action_name": "Single Field Edit",
              "action_settings": {
                "col_name": "#mybool",
                "template": "program: \n\tprint('template', connected_device_name('main'));\n\t'Yes'",
                "value_type": "template"
              },
              "comment": ""
            }
EDIT: for completeness, here is the device connected log using the same chain
Code:
Action Chains: Starting event: Device Connected
calling method (init_cache) of obj (selection_count)
('template', 'Folder Device')
calling method (flush_cache) of obj (selection_count)
Action Chains: Starting chain: Test bool
Action Chains: Test bool: starting action No. 1: Single Field Edit
('template', 'Folder Device')
Action Chains: chain (Test bool) finished in: 0:00:00.040365
Action Chains: event (Device Connected) finished in: 0.04210329055786133

Last edited by chaley; 03-15-2021 at 09:54 AM.
chaley is offline   Reply With Quote
Old 03-15-2021, 10:15 AM   #385
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
It works for me if I connect to Folder Device. Does not work when I use my kindle:

Code:
Action Chains: Starting event: Device Disconnected
calling method (init_cache) of obj (selection_count)
calling method (flush_cache) of obj (selection_count)
Action Chains: Starting chain: test device disconnect
Action Chains: test device disconnect: starting action No. 1: Chain Variables
('device_info: ', 'Amazon Kindle')
Action Chains: chain (test device disconnect) finished in: 0:00:00.930646
Action Chains: event (Device Disconnected) finished in: 1.0228612422943115
Code:
{
  "ActionsChainMenus": {
    "Menus": [
      {
        "active": true,
        "chain_settings": {
          "chain_links": [
            {
              "action_name": "Chain Variables",
              "action_settings": {
                "chain_vars": [
                  {
                    "iterate": false,
                    "name": "new_var",
                    "value": "program:\nprint('device_info: ', connected_device_name('main'))"
                  }
                ]
              },
              "comment": ""
            }
          ],
          "refresh_gui": true
        },
        "image": "",
        "menuText": "test device disconnect",
        "subMenu": ""
      }
    ]
  },
  "schemaVersion": 1.3
}
capink is offline   Reply With Quote
Old 03-15-2021, 10:52 AM   #386
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
Could you try this version of device.py? Thanks.

It turns out that using "eject" is rather different from unplugging the device. In the "eject" case calibre pretends that the device went away. It doesn't really go away until it is physically disconnected. This version of device.py deals with that case.
Attached Files
File Type: zip device.zip (18.5 KB, 640 views)
chaley is offline   Reply With Quote
Old 03-15-2021, 10:56 AM   #387
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
I can confirm that this one works.
capink is offline   Reply With Quote
Old 03-15-2021, 11:38 AM   #388
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 can confirm that this one works.
The new version of device.py is now in calibre source.

EDIT: I tested it on:
  • The Folder device
  • A USB reader
  • An MTP reader
  • The Wireless device
All worked. I don't have a kindle or kobo to try.

Last edited by chaley; 03-15-2021 at 11:52 AM.
chaley is offline   Reply With Quote
Old 03-15-2021, 03:32 PM   #389
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
API

Action chains provides an experimental API that enables other plugins to communicate with it. At this point it is unstable and is meant mainly for testing. Two main things that can be done are:
  1. API For other plugins to provide resources to Action Chains

    Using this, other plugins can provide Action Chains with different resources: actions, events and template functions.

    All you need to do is to define the resources you want in a module called action_chains.py in the root directory of your plugin. Action chains will read this module and import relevant resources based on their type (ChainAction, ChainEvent, TemplateFunction)

    All the resources imported to Action Chains can be used the same as their builtin counter parts.

  2. A fuction to allow other plugins to access Action Chains Modules for their own user defined objects (Experimental)

    This can be useful if you want to give users of you plugin to define their own objects that can be used in your plugin (e.g. user define algorithms in the Find Duplicates plugin)

    Fuction: on_modules_update(user_modules)

    This function should be implemented in a file called action_chains.py in the root of your plugin. A plugin that implements this function will have access to the action chains module manager. It passes user_modules instance which allows plugins to access different objects and classes defined in the module editor. Using this, plugins can give users the option to implement custom classes or functions.

    The user_modules provides the following methods:
    • get_objects(self, module_filters=[], type_filters=[])
    • get_classes(self, module_filters=[], class_filters=[])
    • get_namespace(self, module_name)

    Will start with an example of how action chains gets its custom actions from the user_modules instance:
    Code:
    user_actions = {}
    for cls in user_modules.get_classes(class_filters=[ChainAction]):
        name = cls.name
        if name in ['', 'Chain Action']:
            continue
        user_actions[name] = cls
    The above example will get all classes of type ChainAction form all modules. You can filter by module name by passing a list of filters to the module_list argument. The members of this list can be: string, compiled regex pattern, function.

    The second example is used by a custom version of Find Duplicates to get user defined algorithms (defined as functions not as classes):

    Code:
    user_algorithms = {}
    for module_name in user_modules.module_names:
        module_algos = user_modules.get_namespace(module_name).get('FIND_DUPLICATES_ALGORITHMS', {})
        user_algorithms.update(module_algos)
    The above code deal with functions that are defined in the modules and then added to a dictionary called 'FIND_DUPLICATES_ALGORITHMS'. It gets the 'FIND_DUPLICATES_ALGORITHMS' dictionary and uses it to added the functions to the plugin.

    Notes:
    • The get_namespace(module_name) method returns a dictionary of name : object for the specified module.
    • All methods above will return all objects including imported objects. So if you import a builtin class it will included with the user defined objects. Imported classes will only be excluded if the define the following attribute:
      Code:
      exclude_from_modules = True
      This will affect the class only and will not affect any other class the inherits from it.

      Another way to deal with this is to define a dictionary of objects you want to include as illustrated in the second example above.
    • on_modules_update() is called whenever the modules are updated + at calibre startup + library change.

Last edited by capink; 07-01-2024 at 11:36 PM.
capink is offline   Reply With Quote
Old 03-15-2021, 03:33 PM   #390
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
Version 1.4.0

Version 1.3.2
  • Minor changes to TemplateBox.
  • Minor changes to category_items() template function.

Version 1.3.3
  • Fix: Calibre Actions: Always persist non translated strings in settings. This is done to avoid the settings being invalid if the the user switches languages.

Version 1.4.0
  • Update: Add a new Action: Search Using Templates.
  • Update: Change actions/events combobox into tree combobox.
  • Update: Import/Export: Include schema version plus other minor changes.
  • Update: Conditions Evaluator: Add option to turn off action validation of conditions are not met. The default as of this version if to verify all actions regardless of conditions unless this option is checked.
  • Update: (Experimental) Add two API methods: action_chains_resources(), on_action_chains_modules_update(). The first enables other plugins to provide action chains with resources (actions, events, template_functions). The second method makes the modules manager available for other plugins.
  • Code refactoring.

The API changes are experimental at this stage. For those interested you can read more about them here.

The newly added action is a modified version of an action that was posted in this thread as a custom. It gives the user the ability to construct calibre search using calibre template language. The only modificatoin is the addition of a checkbox to enable selecting the books resulting from the search.

This action could not be added to the selection modifier, because the selection modifier has to validate the search when the user configures the action to make sure it will always return a valid search. This is done because if a user combines a search with the option to select all books in current view, and the search turns out to invalid, all books in current view (the whole library most likely) will be selected and acted upon by subsequent actions.

Since the new action constructs a search from a template it cannot be verified at configure time. It was also dangerous to combine the old iteration of the action (the one posted previously as a custom action) with the selection modifier to select all books in current view because it would have led to the same problem outlined above. So the modification to this action, is to make it able to select the books resulting from the search, but only if the search yields valid results, otherwise no books will be selected.

@Terisa De Morgan: The behavior of the plugin changes in that if you have an action that operates on a field that does not exist in the current library, the chain will fail to validate and will not run even if you set a condition using field_exists(). You have to take one additional step, by explicitly setting the newly introduced option in the condition evaluator to not validate the action whenever the conditions are not met. This change was necessary because the old behavior did lead to some strange and inconsistent behavior.
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 10:57 AM.


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