View Single Post
Old 01-25-2018, 07:35 AM   #295
KevinH
Sigil Developer
KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.
 
Posts: 8,893
Karma: 6120478
Join Date: Nov 2009
Device: many
Actually you should not really need to use a temp folder at all. The plugin itself only has read access to the files inside Sigil. If you try to make a change, the plugin interface code will take a copy of the file, allow you to change the copy and store it hidden away. Once your plugin is complete and returns, Sigil itself will will be given a list of changes you requested and access to the hidden changes files and it will make the changes.

Bk.get_opf should always keep the opf updated and return a correct opf behind the scene but since it is rebuilt from a parsed structure, it may not keep the exact same white space as the original opf.



Quote:
Originally Posted by slowsmile View Post
@Becky...

I'm not sure whether I've got your problem right but, as far as I'm concerned, what really matters when you use the bk.get_opf() method is when you actually run it in your program. So if you've already changed epub files in the epub itself and then you run the get_opf() function then you will get errors because it also changes and updates epub files on the fly as someone has already said.

The way round this problem is just to create a separate work directory for all the files that your are going to change or update in the epub with your plugin code. Use the python mkdtemp() method to create the work directly. In your plugin just save all your changed files to the work dir. The very last task for your plugin should be to write all new or changed files from your work dir to your epub.

If you use a work dir and, as your last plugin task, save all changed or new files back to the epub, then it won't matter how many times you run get_opf() in your plugin because all the epub files will still be in their original state because nothing has been saved or changed yet. So doing it this way should give you correct line numbers.

I remember, somewhere in the distant past, Kevin patiently explaining all this to me in an email and enlightening me in the way the container updates all files that you've changed with your plugin after end-of-run. The container processing runs in this this order: delete files, add files(adds new files), write files(write existing files back to the epub). Appreciating the container processing order and always using a temp work dir in my plugins has stood me in good stead because that's what really helped me to understand how I could safely use the get_opf() function in my IDErrorCheck plugin without any problems.
KevinH is online now   Reply With Quote