View Single Post
Old 12-16-2020, 11:52 AM   #102
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,203
Karma: 1995558
Join Date: Aug 2015
Device: Kindle
I don't understand what exactly you are trying to achieve, but I will address the question of branching in general. There are two ways to do it:

Edit: Starting with version 0.4.0, action chains introduced a conditions feature that allows the user to run actions based on conditions at runtime. This also allows branching.
  • For people who don't write code the only way to do it, is exactly as you discovered yourself (and as I pointed here before) by making search that return empty results (if condition is not met) in which case actions running will not have real effect, after that another search might return results that actions will act on and so so on.
  • The other alternative involves writing a custom action that does the branching for you, and creating a master chain that contains that action. You also have to create separate chains containing the different branches (they can be all grouped in one submenu). In your master chain's custom action, you test the conditions and call the appropriate chain like illustrated below.

    Code:
    from calibre_plugins.action_chains.chains import Chain
    from calibre_plugins.action_chains.actions.base import ChainAction
    import calibre_plugins.action_chains.config as cfg
    
    class RunChain(ChainAction):
    
        name = 'Run Chain'
    
        def run(self, gui, settings, chain):
            chain_name = 'Your Chain Name'
            chain_config = cfg.get_chain_config(chain_name)
            my_new_chain = Chain(chain.plugin_action, chain_config)
            my_new_chain.run()

On a side note, I thought about giving the users the ability to call other chains through Calibre Actions or through a separate action, and maybe supplementing that with a another flow control action. I dismissed the idea because it can cause problems like circular dependency. You can prevent a chain from directly calling itself, but it can do so indirectly through another chain that might reference it, and this would require a complicated dependency checking before running chains. You can write these action(s) for yourself as custom actions if you want this be a generalized solution, but I think it is opening a can of worms.

Last edited by capink; 04-09-2021 at 05:48 PM.
capink is offline   Reply With Quote