mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Add DirWriter. Add str() for manifest items.
This commit is contained in:
parent
a14d00db54
commit
dc1bfe4d20
@ -69,6 +69,9 @@ def barename(name):
|
|||||||
def xpath(elem, expr):
|
def xpath(elem, expr):
|
||||||
return elem.xpath(expr, namespaces=XPNSMAP)
|
return elem.xpath(expr, namespaces=XPNSMAP)
|
||||||
|
|
||||||
|
def xml2str(root):
|
||||||
|
return etree.tostring(root, encoding='utf-8', xml_declaration=True)
|
||||||
|
|
||||||
ASCII_CHARS = set(chr(x) for x in xrange(128))
|
ASCII_CHARS = set(chr(x) for x in xrange(128))
|
||||||
URL_SAFE = set(u'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
URL_SAFE = set(u'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
||||||
u'abcdefghijklmnopqrstuvwxyz'
|
u'abcdefghijklmnopqrstuvwxyz'
|
||||||
@ -114,6 +117,9 @@ class DirContainer(AbstractContainer):
|
|||||||
|
|
||||||
def write(self, path, data):
|
def write(self, path, data):
|
||||||
path = os.path.join(self.rootdir, path)
|
path = os.path.join(self.rootdir, path)
|
||||||
|
dir = os.path.dirname(path)
|
||||||
|
if not os.path.isdir(dir):
|
||||||
|
os.makedirs(dir)
|
||||||
with open(urlunquote(path), 'wb') as f:
|
with open(urlunquote(path), 'wb') as f:
|
||||||
return f.write(data)
|
return f.write(data)
|
||||||
|
|
||||||
@ -121,6 +127,21 @@ class DirContainer(AbstractContainer):
|
|||||||
path = os.path.join(self.rootdir, path)
|
path = os.path.join(self.rootdir, path)
|
||||||
return os.path.isfile(urlunquote(path))
|
return os.path.isfile(urlunquote(path))
|
||||||
|
|
||||||
|
class DirWriter(object):
|
||||||
|
def __init__(self, version=2.0):
|
||||||
|
self.version = version
|
||||||
|
|
||||||
|
def dump(self, oeb, path):
|
||||||
|
if not os.path.isdir(path):
|
||||||
|
os.mkdir(path)
|
||||||
|
output = DirContainer(path)
|
||||||
|
for item in oeb.manifest.values():
|
||||||
|
output.write(item.href, str(item))
|
||||||
|
metadata = oeb.to_opf2() if self.version == 2 else oeb.to_opf1()
|
||||||
|
for href, data in metadata.values():
|
||||||
|
output.write(href, xml2str(data))
|
||||||
|
return
|
||||||
|
|
||||||
|
|
||||||
class Metadata(object):
|
class Metadata(object):
|
||||||
TERMS = set(['contributor', 'coverage', 'creator', 'date', 'description',
|
TERMS = set(['contributor', 'coverage', 'creator', 'date', 'description',
|
||||||
@ -269,6 +290,12 @@ class Manifest(object):
|
|||||||
return property(fget, fset, fdel)
|
return property(fget, fset, fdel)
|
||||||
data = data()
|
data = data()
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
data = self.data
|
||||||
|
if isinstance(data, etree._Element):
|
||||||
|
return xml2str(data)
|
||||||
|
return str(data)
|
||||||
|
|
||||||
def __cmp__(self, other):
|
def __cmp__(self, other):
|
||||||
result = cmp(self.spine_position, other.spine_position)
|
result = cmp(self.spine_position, other.spine_position)
|
||||||
if result != 0:
|
if result != 0:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user