pyepub should allow commented nodes in metadata

pull/95/head
eric 2018-07-29 16:57:36 -04:00
parent f951137981
commit a6b735df71
2 changed files with 13 additions and 9 deletions

View File

@ -498,8 +498,6 @@ def manage_ebooks(request, edition_id, by=None):
file=ebook_form.cleaned_data['file'],
format=ebook_form.cleaned_data['format'],
edition=edition,
active=True,
)
ebook_form.instance.url = new_ebf.file.url
ebook_form.instance.provider = "Unglue.it"

View File

@ -4,12 +4,15 @@ import re
import uuid
from StringIO import StringIO
import datetime
import logging
try:
import lxml.etree as ET
except ImportError:
import xml.etree.ElementTree as ET
logger = logging.getLogger(__name__)
NAMESPACE = {
"dc": "{http://purl.org/dc/elements/1.1/}",
"opf": "{http://www.idpf.org/2007/opf}",
@ -81,14 +84,14 @@ class EPUB(zipfile.ZipFile):
f = self.read("META-INF/container.xml")
except KeyError:
# By specification, there MUST be a container.xml in EPUB
print "The %s file is not a valid OCF." % str(filename)
logger.warning("The %s file is not a valid OCF." % str(filename))
raise InvalidEpub
try:
# There MUST be a full path attribute on first grandchild...
self.opf_path = ET.fromstring(f)[0][0].get("full-path")
except IndexError:
# ...else the file is invalid.
print "The %s file is not a valid OCF." % str(filename)
logger.warning("The %s file is not a valid OCF." % str(filename))
raise InvalidEpub
# NEW: json-able info tree
@ -104,11 +107,12 @@ class EPUB(zipfile.ZipFile):
# Iterate over <metadata> section, fill EPUB.info["metadata"] dictionary
for i in self.opf.find("{0}metadata".format(NAMESPACE["opf"])):
tag = ns.sub('', i.tag)
if tag not in self.info["metadata"]:
self.info["metadata"][tag] = i.text or i.attrib
else:
self.info["metadata"][tag] = [self.info["metadata"][tag], i.text or i.attrib]
if 'tag' in i :
tag = ns.sub('', i.tag)
if tag not in self.info["metadata"]:
self.info["metadata"][tag] = i.text or i.attrib
else:
self.info["metadata"][tag] = [self.info["metadata"][tag], i.text or i.attrib]
# Get id of the cover in <meta name="cover" />
try:
@ -411,6 +415,8 @@ class EPUB(zipfile.ZipFile):
:type filename: str
:param filename: name of the file to be writte
"""
if isinstance(filename, str):
filename = open(filename,'w')
filename.seek(0)
new_zip = zipfile.ZipFile(filename, 'w')