Moderator Notice
Moved back to the development forum
Quote:
Originally Posted by capink
[*]Monkey patching calibre's context class. I am not sure how clean/desirable this would be. But if offers me one thing none of the other options do; the ability to use any plugin defined helper functions in calibre's template search.
|
I am not sure I understand. Are you suggesting making Action Chains helper functions available to python templates, GPM templates, or both? Mentioning the context class tells me it is for PTM templates.
If you mean both then the only mechanism that exists is the stored templates / template functions. You could add to the base functions but we would need to work out how to control their lifetime.
Assuming you mean PTM templates, I think I see the problem you are solving. The context for template searches is the calibre base context, not one you created, so you have no way to put your helper functions in it. However, there is another way consistent with python and calibre: put these helper functions into their own module or own class and the PTM writer can "import" them. Doing it this way avoids monkey patching and all the lifetime and name collision problems that come with it. It also handles the problem that action chains might not be installed because the user writing the import can ensure that it is. Something like this:
Code:
python:
from calibre_plugins.action_chains.templates.functions import SelectionCount
def evaluate(book, ctx):
return ''