View Single Post
Old 06-12-2012, 08:35 AM   #2
kiwidude
Calibre Plugins Developer
kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.
 
Posts: 4,731
Karma: 2197770
Join Date: Oct 2010
Location: Australia
Device: Kindle Oasis
The challenge with using Modify ePub as a starting point is it is designed to (a) work with multiple ePubs at once, and (b) to run as a background job in calibre. The latter in particular is a requirement if you need (a) or else your calibre will freeze up while the plugin runs. In your case if you are just doing an ad hoc single epub at a time you could get away without all that complexity to do with threading, queuing books and passing data around between processes which Modify ePub has to do.

However you should be able to still steal parts of the code. The modify.py class contains the BookModifier class, which if you look at the process_book function has responsibility for extracting the epub into a temporary directory, giving you an "ExtendedContainer" object (from container.py) to work with which lets you manipulate the opf manifest and get access to the html files, writing any modified files back to the zip when done. It also has the various tasks that Modify ePub implements so plenty of examples of how to manipulate the ePub using my container object. You could copy/strip those two python files, strip down the action.py to just directly call your version of the BookModifier class from the modify_epub function (rather than launching dialogs to get options like it does now), and then use some code to put the modified ePub back into your library (self.db.add_format).

Or you could start with something really basic which is where I originally started for Modify ePub - looking at the ePubFixer plugin code in the calibre source, in calibre/ebooks/epub/fix/ which has a very basic container object etc. That plugin does a very simple extraction of an epub for some modifications. I found a number of bugs/limitations with that particular container object in container.py which is why I ended up writing my own version of it for Modify ePub, but it might be ok for your needs or to start with. If you bolted that onto one of Kovid's plugin examples in the help tutorial that might be a simpler starting point.
kiwidude is offline   Reply With Quote