View Single Post
Old 05-02-2015, 06:12 AM   #259
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,510
Karma: 8065348
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by davidfor View Post
* chaley: stop rolling your eyes at my glib statement
I am glad I didn't do any eye rolling.

Quote:
Originally Posted by davidfor View Post
Putting my money were my mouth is...
...
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::.

Another thought: my guess is that the path to the file is tacked onto the end of the command after the arguments. 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.
chaley is offline   Reply With Quote