View Single Post
Old 07-31-2014, 06:36 PM   #41
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,828
Karma: 6120478
Join Date: Nov 2009
Device: many
Hi varlog,

Okay I didn't want to change new_uuid.py much, but I did have to add a return 0 at the end of the main routine.

I also added a short uuid_runner.py that interfaces between what bookcontainer.py provides and what new_uuid.py wants for input.

Code:
#!/usr/bin/env python                                                                                                           
# -*- coding: utf-8 -*-                                                                                                         
# vim:ts=4:sw=4:softtabstop=4:smarttab:expandtab                                                                                

import sys, os
import tempfile, shutil

import new_uuid

def run(bk):
    # get the opf data from the bookcontainer interace                                                                          
    opfdata = bk.get_opf()

    # create a temporary directory store it there as new_uuid wants a path                                                      
    mydir = tempfile.mkdtemp()
    opfpath = os.path.join(mydir, 'content.opf')
    file(opfpath,'wb').write(opfdata)

    # now invoke new_uuid's main routine and get its return value                                                               
    argv = ['new_uuid.py', opfpath]
    rv = new_uuid.main(argv)

    # if it failed raise failed exception                                                                                       
    if rv is not None and rv != 0:
        shutil.rmtree(mydir)
        raise Exception("new_uuid failed")

    # success so grab revised metadata from temp content.opf                                                                    
    # and set it via the interface                                                                                              
    if rv == 0:
        # extract just the new metadata and set it                                                                              
        opfdata = file(opfpath,'rb').read()
        ms = opfdata.find('<metadata')
        me = opfdata.find('</metadata>')
        if ms > -1 and me > -1:
            newmetadata = opfdata[ms:me+11] + '\n'
            bk.setmetadataxml(newmetadata)
        else:
            shutil.rmtree(mydir)
            raise Exception("no metadata found")

    # clean up our own tempspace                                                                                                
    shutil.rmtree(mydir)
    return 0
I have zipped everything up along with a number of other changes and fixes to all of the scripts. So please grab the attached zip archive.

To test it, I unpacked this zip on my Desktop, unpacked a previously saved epub from Sigil into ebook/ and cd'd into launcher and ran the following:

python edit_launcher.py ~/Desktop/ebook ~/Desktop/ebook/OEBPS/content.opf ~/Desktop/testout uuid_runner ~/Desktop/NewUUID


Here is the xml result file it produced along with a new content.pdf in the output directory

<?xml version="1.0" encoding="UTF-8"?>
<wrapper>
<result>success</result>
<deleted>
</deleted>
<added>
</added>
<modified>
<metadataxml>metadataxml</metadataxml>
</modified>
<successmsg>
bookid
urn:uuid:aaddb689-572a-4ef0-819d-630091072ea6
urn:uuid:9067c7b2-0534-4d34-952a-eb214c3a5724
Script Success
</successmsg>
</wrapper>

So it all appears to work on the command line. Sigil would still have to parse this result xml and know to copy the content.opf resource from the Sigil temporary directory and replace the one inside.

Hope this helps you understand the interface a bit better.

KevinH


Ps. I think the smarten one would be easy as well so I'll write a quick runner interface to it and post it here as well.

So here is the smarten_runner.py code I added to the Smarten to interface it with the code. The interesting thing is the bookcontainer.py has access to a built in QuickXHTMLParser, which separates out the Text from the tags on the fly (it is a stream parser) and so creating Smarten using that approach would have been even simpler.

Code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vim:ts=4:sw=4:softtabstop=4:smarttab:expandtab

import sys, os
import tempfile, shutil

import smarten

def run(bk):

    # iterate through all text files smartening their punctuation
    for id, href in bk.text_iter():
        data = bk.readfile(id)
        sdata = smarten.smarten(data)
        bk.writefile(id, sdata)

    return 0

 
def main():
    return -1
    
if __name__ == "__main__":
    sys.exit(main())

The command line to invoke it from inside of the launcher folder was ...
python edit_launcher.py ~/Desktop/ebook ~/Desktop/ebook/OEBPS/content.opf ~/Desktop/testout smarten_runner ~/Desktop/Smarten

I have zipped up Smarten with the runner inside and have posted it here.

Last edited by KevinH; 08-12-2014 at 03:49 PM.
KevinH is offline   Reply With Quote