add license to opf file metadata

pull/1/head
eric 2013-09-20 17:44:37 -04:00
parent 5a256ef16b
commit aedf080965
3 changed files with 35 additions and 0 deletions

View File

@ -11,6 +11,7 @@ def personalize(epub_file, acq):
context={'acq':acq}
part = StringIO(str(render_to_string('epub/datedcc_license.xhtml', context)))
output.addpart(part, "datedcc_license.xhtml", "application/xhtml+xml", 1) #after title, we hope
output.addmetadata('rights','%s after %s'%(acq.work.last_campaign().license_url,acq.work.last_campaign().cc_date))
output.close()
output.writetodisk('testfile2.epub')
return output

View File

@ -309,7 +309,30 @@ class EPUB(zipfile.ZipFile):
zipfile.ZipFile.close(self) # Don't know why
new_zip.close() # but it works, don't ever touch
zipfile.ZipFile.__init__(self, FLO, mode="a")
def addmetadata(self, term, value, namespace='dc'):
"""
Add an metadata entry
:type term: str
:param term: element name/tag for metadata item
:type value: str
:param value: a value
:type namespace: str
:param namespace. either a '{URI}' or a registered prefix ('dc', 'opf', 'ncx') are currently built-in
"""
assert self.mode != "r", "%s is not writable" % self
namespace = NAMESPACE.get(namespace,namespace)
element = ET.Element(namespace+term, attrib={})
element.text = value
self.opf[0].append(element)
# note that info is ignoring namespace entirely
if self.info["metadata"].has_key(term):
self.info["metadata"][term] = [self.info["metadata"][term] , value]
else:
self.info["metadata"][term] = value
def additem(self, fileObject, href, mediatype):
"""
Add a file to manifest only

View File

@ -3,6 +3,10 @@ import urllib2
from tempfile import NamedTemporaryFile
from StringIO import StringIO
from . import EPUB
try:
import lxml.etree as ET
except ImportError:
import xml.etree.ElementTree as ET
class EpubTests(unittest.TestCase):
@ -31,3 +35,10 @@ class EpubTests(unittest.TestCase):
epub.addpart(part, "testpart.xhtml", "application/xhtml+xml", 2)
self.assertEqual(len(epub.opf[2]),9) #spine items
def test_addmetadata(self):
epub=EPUB(self.epub2file,mode='a')
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')