2013-11-13 17:56:18 +00:00
|
|
|
# coding=utf-8
|
2013-09-16 01:43:01 +00:00
|
|
|
import unittest
|
|
|
|
import urllib2
|
2013-11-18 04:24:03 +00:00
|
|
|
import zipfile
|
|
|
|
import random
|
2013-09-16 01:43:01 +00:00
|
|
|
from tempfile import NamedTemporaryFile
|
|
|
|
from StringIO import StringIO
|
|
|
|
from . import EPUB
|
2013-09-20 21:44:37 +00:00
|
|
|
try:
|
|
|
|
import lxml.etree as ET
|
|
|
|
except ImportError:
|
|
|
|
import xml.etree.ElementTree as ET
|
2013-09-16 01:43:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
class EpubTests(unittest.TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
# get a small epub test file as a file-like object
|
|
|
|
self.epub2file = NamedTemporaryFile(delete=False)
|
|
|
|
test_file_content = urllib2.urlopen('http://www.hxa.name/articles/content/EpubGuide-hxa7241.epub')
|
|
|
|
self.epub2file.write(test_file_content.read())
|
|
|
|
self.epub2file.seek(0)
|
2013-10-06 19:12:33 +00:00
|
|
|
# get an epub with no guide element
|
|
|
|
self.epub2file2 = NamedTemporaryFile(delete=False)
|
|
|
|
test_file_content2 = urllib2.urlopen('http://www.gutenberg.org/ebooks/2701.epub.noimages')
|
|
|
|
self.epub2file2.write(test_file_content2.read())
|
|
|
|
self.epub2file2.seek(0)
|
2013-11-18 04:24:03 +00:00
|
|
|
|
2013-09-16 01:43:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_instantiation(self):
|
|
|
|
epub=EPUB(self.epub2file)
|
2013-11-18 04:24:03 +00:00
|
|
|
members = len(epub.namelist())
|
2013-09-16 01:43:01 +00:00
|
|
|
self.assertNotEqual(epub.filename, None)
|
|
|
|
self.assertEqual(len(epub.opf),4)
|
|
|
|
self.assertEqual(len(epub.opf[0]),11) #metadata items
|
|
|
|
self.assertEqual(len(epub.opf[1]),11) #manifest items
|
|
|
|
self.assertEqual(len(epub.opf[2]),8) #spine items
|
|
|
|
self.assertEqual(len(epub.opf[3]),3) #guide items
|
2013-11-18 04:24:03 +00:00
|
|
|
# test writing
|
|
|
|
new_epub=StringIO()
|
|
|
|
#epub.writetodisk("test_instantiation")
|
|
|
|
epub.writetodisk(new_epub)
|
|
|
|
epub=EPUB(new_epub)
|
|
|
|
self.assertEqual(len(epub.opf),4)
|
|
|
|
self.assertEqual(members,len(epub.namelist()))
|
|
|
|
self.assertTrue(zipfile.is_zipfile(new_epub))
|
2013-09-16 01:43:01 +00:00
|
|
|
|
|
|
|
def test_addpart(self):
|
|
|
|
epub=EPUB(self.epub2file,mode='a')
|
2013-11-18 04:24:03 +00:00
|
|
|
members = len(epub.namelist())
|
2013-09-16 01:43:01 +00:00
|
|
|
self.assertNotEqual(epub.filename, None)
|
|
|
|
part = StringIO('<?xml version="1.0" encoding="utf-8" standalone="yes"?>')
|
|
|
|
epub.addpart(part, "testpart.xhtml", "application/xhtml+xml", 2)
|
|
|
|
self.assertEqual(len(epub.opf[2]),9) #spine items
|
2013-11-18 04:24:03 +00:00
|
|
|
# test writing
|
|
|
|
new_epub=StringIO()
|
|
|
|
epub.writetodisk(new_epub)
|
|
|
|
epub=EPUB(new_epub)
|
|
|
|
self.assertEqual(len(epub.opf[2]),9)
|
|
|
|
self.assertEqual(members+1,len(epub.namelist()))
|
|
|
|
#test delete
|
|
|
|
epub._delete("testpart.xhtml")
|
|
|
|
new_epub=StringIO()
|
|
|
|
epub.writetodisk(new_epub)
|
|
|
|
new_zip = zipfile.ZipFile(new_epub)
|
|
|
|
self.assertEqual(members,len(new_zip.namelist()))
|
|
|
|
self.assertTrue(zipfile.is_zipfile(new_epub))
|
|
|
|
|
2013-10-06 19:12:33 +00:00
|
|
|
def test_addpart_noguide(self):
|
|
|
|
epub2=EPUB(self.epub2file2,mode='a')
|
|
|
|
self.assertEqual(len(epub2.opf),3)
|
|
|
|
self.assertEqual(epub2.info['guide'],None)
|
|
|
|
num_spine_items = len(epub2.opf[2])
|
2013-11-13 17:56:18 +00:00
|
|
|
uxml = u'<?xml version="1.0" encoding="utf-8" standalone="yes"?><test>VojtěchVojtíšek</test>'
|
|
|
|
part = StringIO(unicode(uxml))
|
2013-10-06 19:12:33 +00:00
|
|
|
epub2.addpart(part, "testpart.xhtml", "application/xhtml+xml", 2)
|
|
|
|
self.assertEqual(len(epub2.opf[2]), num_spine_items +1) #spine items
|
2013-11-18 04:24:03 +00:00
|
|
|
new_epub=StringIO()
|
|
|
|
epub2.writetodisk(new_epub)
|
|
|
|
epub2=EPUB(new_epub)
|
|
|
|
|
2013-09-20 21:44:37 +00:00
|
|
|
def test_addmetadata(self):
|
|
|
|
epub=EPUB(self.epub2file,mode='a')
|
2013-11-18 04:24:03 +00:00
|
|
|
members = len(epub.namelist())
|
2013-09-20 21:44:37 +00:00
|
|
|
epub.addmetadata('test', 'GOOD')
|
|
|
|
self.assertIn('<dc:test>GOOD<',ET.tostring(epub.opf, encoding="UTF-8"))
|
|
|
|
self.assertTrue(epub.opf.find('.//{http://purl.org/dc/elements/1.1/}test') is not None)
|
|
|
|
self.assertEqual(epub.info['metadata']['test'], 'GOOD')
|
2013-11-18 04:24:03 +00:00
|
|
|
# test writing
|
|
|
|
new_epub=StringIO()
|
|
|
|
epub.writetodisk(new_epub)
|
|
|
|
epub=EPUB(new_epub)
|
|
|
|
self.assertEqual(epub.info['metadata']['test'], 'GOOD')
|
|
|
|
new_zip = zipfile.ZipFile(new_epub)
|
|
|
|
self.assertEqual(members,len(new_zip.namelist()))
|
|
|
|
self.assertTrue(zipfile.is_zipfile(new_epub))
|
|
|
|
|
|
|
|
def test_new_epub(self):
|
|
|
|
f = '%012x.epub' % random.randrange(16**12) #random name
|
|
|
|
epub=EPUB(f,mode='w')
|
|
|
|
epub.addmetadata('test', 'GOOD')
|
|
|
|
uxml = u'<?xml version="1.0" encoding="utf-8" standalone="yes"?><test>VojtěchVojtíšek</test>'
|
|
|
|
part = StringIO(unicode(uxml))
|
|
|
|
epub.addpart(part, "testpart.xhtml", "application/xhtml+xml", 2)
|
|
|
|
epub.close()
|
|
|
|
epub=EPUB(f,mode='r')
|
|
|
|
self.assertEqual(len(epub.opf),4)
|
2016-07-24 22:39:36 +00:00
|
|
|
self.assertEqual(len(epub.opf[0]),6) #metadata items
|
2013-11-18 04:24:03 +00:00
|
|
|
self.assertEqual(len(epub.opf[1]),2) #manifest items
|
|
|
|
self.assertEqual(len(epub.opf[2]),1) #spine items
|
|
|
|
self.assertEqual(len(epub.opf[3]),0) #guide items
|