check for kill failure on first round as well as second
[objavi2.git] / tests / _zip2epub.py
blobd1dd0e2d6df3c8e7cca1e1a7ea798c6d89b9b7df
1 #!/usr/bin/python
2 import os, sys
3 import zipfile
4 from cStringIO import StringIO
6 try:
7 import simplejson as json
8 except ImportError:
9 import json
11 from objavi.fmbook import log, Book, make_book_name
12 from objavi.cgi_utils import shift_file
13 from objavi.config import PUBLISH_DIR
14 from iarchive import epub as ia_epub
15 from objavi.xhtml_utils import EpubChapter
17 from objavi import epub_utils
19 DCNS = "{http://purl.org/dc/elements/1.1/}"
21 USE_CACHED_IMAGES = True #avoid network -- will make out of date books in production!
23 class ZipBook(Book):
25 def __init__(self, zipstring, **kwargs):
26 f = StringIO(zipstring)
27 self.store = zipfile.ZipFile(f, 'r')
28 self.info = json.loads(self.store.read('info.json'))
30 metadata = self.info['metadata']
31 book = metadata['fm:book']
32 server = metadata['fm:server']
33 bookname = make_book_name(book, server)
35 Book.__init__(self, book, server, bookname, **kwargs)
36 self.set_title(metadata['title'])
38 def make_epub(self):
39 self.epubfile = self.filepath('%s.epub' % self.book)
40 ebook = ia_epub.Book(self.epubfile, content_dir='')
41 manifest = self.info['manifest']
42 metadata = self.info['metadata']
43 toc = self.info['TOC']
44 spine = self.info['spine']
46 #manifest
47 filemap = {} #reformualted manifest for NCX
48 for ID in manifest:
49 fn, mediatype = manifest[ID]
50 content = self.store.read(fn)
51 if mediatype == 'text/html':
52 #convert to application/xhtml+xml
53 c = EpubChapter(self.server, self.book, ID, content,
54 use_cache=USE_CACHED_IMAGES)
55 c.remove_bad_tags()
56 content = c.as_xhtml()
57 fn = fn[:-5] + '.xhtml'
58 mediatype = 'application/xhtml+xml'
59 if mediatype == 'application/xhtml+xml':
60 filemap[ID] = fn
62 #XXX fix up text/html
63 info = {'id': ID, 'href': fn, 'media-type': mediatype}
64 ebook.add_content(info, content)
66 #spine
67 for ID in spine:
68 ebook.add_spine_item({'idref': ID})
70 #toc
71 ncx = epub_utils.make_ncx(toc, metadata, filemap)
72 ebook.add(ebook.content_dir + 'toc.ncx', ncx)
75 #metadata -- no use of attributes (yet)
76 # and fm: metadata disappears for now
77 meta_info_items = []
78 for k, v in metadata.iteritems():
79 if k.startswith('fm:'):
80 continue
81 meta_info_items.append({'item': DCNS + k,
82 'text': v}
85 #copyright
86 authors = sorted(self.info['copyright'])
87 for a in authors:
88 meta_info_items.append({'item': DCNS + 'creator',
89 'text': a}
91 meta_info_items.append({'item': DCNS + 'rights',
92 'text': 'This book is free. Copyright %s' % (', '.join(authors))}
95 tree_str = ia_epub.make_opf(meta_info_items,
96 ebook.manifest_items,
97 ebook.spine_items,
98 ebook.guide_items,
99 ebook.cover_id)
100 ebook.add(ebook.content_dir + 'content.opf', tree_str)
102 ebook.z.close()
105 if __name__ == '__main__':
106 try:
107 f = open(sys.argv[1])
108 zipstring = f.read()
109 f.close()
110 except (IOError, OSError), e:
111 log(e, "USAGE: %s <booki-zip file>" % sys.argv[0])
112 sys.exit()
114 book = ZipBook(zipstring)
115 print book
116 book.make_epub()
117 shift_file(book.epubfile, PUBLISH_DIR)