View Single Post
Old 12-25-2022, 01:28 PM   #2
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 DaltonST View Post
Question: Can the PTM template cache get poisoned when the PTM template is run "in batch" via a Job? If so, is there a way to clear it so the template_dialog for Preferences > Template Functions can always be successfully executed?
Is it possible? Probably. The formatter is supposed to be thread safe, but the management of the formatter functions isn't. How to fix it? Restart calibre should do it, unless the poison has made it into the database. In your example it didn't seem like the database was poisoned as the template data you "carved out" seemed syntactically correct.

The error indicates that the name of a stored template changed from a string to a list. I don't see any way that this can happen in base calibre. It is hard to follow the effects of your code as it modifies internal data structures. At a minimum I suspect possible threading problems.

On this topic, I wonder why you are reaching so far into the guts of the formatter to run your template? It is very hard to predict what will happen, given that the state save/restore mechanism is being bypassed and base calibre data structures are being modified. Why not use safe_format(), which is how templates are supposed to be called? Or unsafe_format() for that matter?

If you want to call a stored template then call safe_format() with something like
Code:
program: stored_template_name()
passing any arguments you want, or passing arguments in globals.
Quote:
Question: How do I create a PTM template using a dict with keys that only exist at runtime?
Code:
    context.globals.get('dict_name', {}).get('key_name', None)
One techique I use is to check if the key is in globals. If it isn't I do what is required to initialize globals with default values.
Quote:
Question: Is this syntax supported: text = context.globals['val'] ? If not, what is the proper way to accomplish passing a single string value to a PTM template at runtime?
Yes, it is supported. However, the key 'val' must be in the dict. If it might not be then use (as above)
Code:
    text = context.globals.get('val', whatever_default_you_want)
That said, I wonder if globals is the right place to pass the values. If what is being passed is truly an argument then the template should look in arguments. This lets you write something like
Code:
program:
    some_python_template(some_argument)
The python template would check the length of arguments and do what is appropriate if the arguments don't exist or are the wrong type.
Quote:

Happy/Merry Christmas.


DaltonST
Thank you, and I hope the same for you.
chaley is offline   Reply With Quote