Register Guidelines E-Books Today's Posts Search

Go Back   MobileRead Forums > E-Book Software > Calibre > Development

Notices

Reply
 
Thread Tools Search this Thread
Old 05-06-2023, 01:02 PM   #1
Leseratte_10
Groupie
Leseratte_10 ought to be getting tired of karma fortunes by now.Leseratte_10 ought to be getting tired of karma fortunes by now.Leseratte_10 ought to be getting tired of karma fortunes by now.Leseratte_10 ought to be getting tired of karma fortunes by now.Leseratte_10 ought to be getting tired of karma fortunes by now.Leseratte_10 ought to be getting tired of karma fortunes by now.Leseratte_10 ought to be getting tired of karma fortunes by now.Leseratte_10 ought to be getting tired of karma fortunes by now.Leseratte_10 ought to be getting tired of karma fortunes by now.Leseratte_10 ought to be getting tired of karma fortunes by now.Leseratte_10 ought to be getting tired of karma fortunes by now.
 
Posts: 181
Karma: 2010542
Join Date: Sep 2021
Device: PB Era, PB InkPad 3 Pro
Access input file name in postadd (FileTypePlugin)?

I've noticed that with the lastest update Calibre does support attaching arbitrary data files to a book.

For my ACSM-to-EPUB plugin I was planning to add an option to add the source ACSM file as a data file to the imported book, so people can archive that file if they want to.

In order to do that, I added the "postadd" method to my plugin - and then I got stuck, because the only parameters I get there are book_id (the ID the new book entry has in the database), fmt_map (a mapping between formats and file names for the books themselves), and db (probably a reference to the Calibre database object).

In the Calibre source code I found db.add_extra_files which is the method I'd need to call to add the ACSM files to the book records, but I'm still a bit stuck.

The plugin's "run" method is too early to do that, because at that point the book is not yet added to the database and so a book ID does not exist yet.

The plugin's "postadd" method is too late - when that's executed I do know the book's book ID, but I no longer know the path to (or the contents of) the original, unmodified input file before it ran through all the FileTypePlugins.

I can't add code to the "run" method to store the original path or even the full original file, because when multiple books are being imported at the same time, how do I know which of the stored files from the "run" method corresponds to the book IDs returned in "postadd"?

Is there a way to do what I'm trying to do, or is that not supported with Calibre's current API?
Leseratte_10 is offline   Reply With Quote
Old 05-06-2023, 02:50 PM   #2
thiago.eec
Guru
thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.
 
Posts: 929
Karma: 1177583
Join Date: Dec 2016
Location: Goiânia - Brazil
Device: iPad, Kindle Paperwhite
Since you know the book id, then you can get the book path.
This is how I would do it:

Code:
book_path = self.db.format_abspath(book_id, 'EPUB')

Last edited by thiago.eec; 05-06-2023 at 02:54 PM. Reason: typo
thiago.eec is offline   Reply With Quote
Advert
Old 05-06-2023, 03:23 PM   #3
Leseratte_10
Groupie
Leseratte_10 ought to be getting tired of karma fortunes by now.Leseratte_10 ought to be getting tired of karma fortunes by now.Leseratte_10 ought to be getting tired of karma fortunes by now.Leseratte_10 ought to be getting tired of karma fortunes by now.Leseratte_10 ought to be getting tired of karma fortunes by now.Leseratte_10 ought to be getting tired of karma fortunes by now.Leseratte_10 ought to be getting tired of karma fortunes by now.Leseratte_10 ought to be getting tired of karma fortunes by now.Leseratte_10 ought to be getting tired of karma fortunes by now.Leseratte_10 ought to be getting tired of karma fortunes by now.Leseratte_10 ought to be getting tired of karma fortunes by now.
 
Posts: 181
Karma: 2010542
Join Date: Sep 2021
Device: PB Era, PB InkPad 3 Pro
Thanks for your answer. That gets me the path to the book in the Calibre database, but that's not what I'm looking for.

But what I need is the path to the book where it was prior to import.

Say I have a file "A" and import it into Calibre. It runs through a FileTypePlugin which reads the file "A", from that file it creates a new file "B", and returns a path to that new file "B" via that plugin's run() method.

Calibre then copies the file "B" to somewhere else, lets call it "B2".

Then that file "B2" is stored into the DB, and the book ID and the path to "B2" is handed into the plugin's postadd method. But I don't want B2, I want A. I would like to attach the original file "A", before it's modified by my FileTypePlugin, to be attached to the book "B(2)".

That means inside the postadd method, where I only know the book ID and "B2", I'd like to figure out what "A" is - so I can call "db.add_extra_files(book_id, A)" But the command you posted will also just return "B2" which I already know from the input parameters of the function anyways.

Inside "run" I only know "A" and "B". Inside "postadd" I only know "B2" (same file but different path as "B") and "book_id". I see no way to "link these together", but I need to know "book_id" and "A" at the same time to add that file to the book record.

Last edited by Leseratte_10; 05-06-2023 at 04:30 PM.
Leseratte_10 is offline   Reply With Quote
Old 05-06-2023, 04:46 PM   #4
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: 11,742
Karma: 6997045
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Perhaps look at the GetFileName plugin to see how it knows when to store the one-true original file name? It must have the book id when it does it.

Perhaps store the original file name as a property in the output? I think both PDF and EPUB support custom properties.
chaley is offline   Reply With Quote
Old 05-06-2023, 05:09 PM   #5
Leseratte_10
Groupie
Leseratte_10 ought to be getting tired of karma fortunes by now.Leseratte_10 ought to be getting tired of karma fortunes by now.Leseratte_10 ought to be getting tired of karma fortunes by now.Leseratte_10 ought to be getting tired of karma fortunes by now.Leseratte_10 ought to be getting tired of karma fortunes by now.Leseratte_10 ought to be getting tired of karma fortunes by now.Leseratte_10 ought to be getting tired of karma fortunes by now.Leseratte_10 ought to be getting tired of karma fortunes by now.Leseratte_10 ought to be getting tired of karma fortunes by now.Leseratte_10 ought to be getting tired of karma fortunes by now.Leseratte_10 ought to be getting tired of karma fortunes by now.
 
Posts: 181
Karma: 2010542
Join Date: Sep 2021
Device: PB Era, PB InkPad 3 Pro
That looked like interesting options.

Unfortunately, the GetFileName plugin seems to be a bit unreliable, especially when there's multiple files involved with the same name (say I import /tmp/test1/file.acsm and /tmp/test2/file.acsm at the same time), but I will take a closer look tomorrow when I'm more awake (the fact that all comments and even all variable names are in spanish doesn't really make the code easy to read). And it also seems to be unreliable when combined with multiple FileTypePlugins, as mentioned in the last post(s) #401 and later in the thread.

I found a comment by Kovid Goyal about a cool attribute "self.original_path_to_file", which I thought was my exact solution, but unfortunately that attribute is only set during the "run" phase of the plugin - not during "postadd" ... maybe Calibre could be modded to set that property for the postadd phase as well.

But another question would be - even if this property is added, does the file still exist by then? Particularly when a file has been added through the Calibre server?

As for storing that information in the file - I could do that, but A) you'd never know if FileTypePlugins running after mine would keep that, and B) I'd like the original file to stay as close to the original as possible, so I'd rather not do that.

There's just no good way to match up information stored during the run() phase with information gathered during the postadd() phase, because there's no single unique identifier (for the book) that's available during both.

It would be cool if Calibre would assign a random unique UUID to a book file immediately when the import starts, and then made that UUID available to every single FileTypePlugin in every single execution step of the plugins that could be touching that file, so plugins could immediately identify "Okay, *this* is the exact same file I touched earlier".

Last edited by Leseratte_10; 05-06-2023 at 05:30 PM.
Leseratte_10 is offline   Reply With Quote
Advert
Old 05-07-2023, 01:23 AM   #6
kovidgoyal
creator of calibre
kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.
 
kovidgoyal's Avatar
 
Posts: 43,863
Karma: 22666666
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
Simply add some custom metadata field to the OPF file in the EPUB you generate. Nothing else is likely to remove it. The field can point to the original acsm file or just have a hex encoded dump of the data. Then in postadd use db.format_abspath() to get the path to the added EPUB file, read the added field and optionally remove it and add the acsm file with add_extra_file
kovidgoyal is offline   Reply With Quote
Reply


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Is it possible to convert a book using an *unzipped* HTML file as the input? jdunning Conversion 13 08-27-2019 01:41 AM
Book Tags in Input HTML File? titani Calibre 6 08-06-2014 11:37 PM
Kindle input event device file names can CHANGE. geekmaster Kindle Developer's Corner 26 03-13-2012 01:02 PM
calibre input/output error while starts to view a file ccczzx Calibre 4 08-05-2011 10:56 AM


All times are GMT -4. The time now is 01:13 AM.


MobileRead.com is a privately owned, operated and funded community.