Quote:
Originally Posted by chaley
I am glad I didn't do any eye rolling.
Is there a reason that you didn't simply allow templates in the args column for all the formats? This would work easily unless people are using paired { } characters in their arguments, because the template processor passes through anything that isn't a template language expression. If use of braces is a concern (I have no idea) then you could use a prefix to indicate that the argument string is to be passed through the template processor after stripping off the prefix, perhaps something like {TEMPLATE} or ::TEMPLATE::.
|
I think I said "quick and dirty"

Allowing templates with the other types was something that I was thinking of. The concern was making sure it didn't break existing function.
Quote:
Another thought: my guess is that the path to the file is tacked onto the end of the command after the arguments.
|
That's exactly what is happening. By adding the "TEMPLATE" type, I could easily bypass that. Of course, it wasn't as simple as that.
Quote:
I can easily imagine cases where that isn't what one wants. If one can use the template processor in any argument list then the user can put the filename wherever it is needed. The trick is to put the path where the template processor can find it. I suggest that sticking it into an "odd" identifier is the best way to go. Something like the following:
Code:
def init_cache(library_path):
from calibre.db.backend import DB
from calibre.db.cache import Cache
backend = DB(library_path)
cache = Cache(backend)
cache.init()
return cache
from calibre.ebooks.metadata.book.formatter import SafeFormat
cache = init_cache(library_path = sys.argv[1])
formatter = SafeFormat()
# Loop through all the books in the library
for id_ in cache.all_book_ids():
mi = cache.get_metadata(id_)
mi.set_identifier("**filepath", cache.format_abspath(id_, "epub"))
print(formatter.safe_format("{title} {tags} {identifiers:select(**filepath)}", mi, _('TEMPLATE ERROR'), mi ))
Using this idea would require you to know that the argument template is taking care of paths so you don't stick it at the end. Perhaps a second prefix is the best way to go, something like "{PATHTEMPLATE}". If you do this then you would not need the "TEMPLATE" type because the user would have control over if and where the path string is put.
|
OK, that's interesting idea. I was thinking of simply putting a marker of some sort in and if it exists, replace it with the file path, otherwise, add the path to the end like the plugin currently does.
And thinking of this, the "TEMPLATE" type should be "NONE" which would be "don't add the file name at all". But, it isn't as needed if all formats allow templates.