Add DirWriter. Add str() for manifest items.

This commit is contained in:
Marshall T. Vandegrift 2008-12-28 15:46:52 -05:00
parent a14d00db54
commit dc1bfe4d20

View File

@ -69,6 +69,9 @@ def barename(name):
def xpath(elem, expr):
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))
URL_SAFE = set(u'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
u'abcdefghijklmnopqrstuvwxyz'
@ -114,6 +117,9 @@ class DirContainer(AbstractContainer):
def write(self, path, data):
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:
return f.write(data)
@ -121,6 +127,21 @@ class DirContainer(AbstractContainer):
path = os.path.join(self.rootdir, 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):
TERMS = set(['contributor', 'coverage', 'creator', 'date', 'description',
@ -269,6 +290,12 @@ class Manifest(object):
return property(fget, fset, fdel)
data = data()
def __str__(self):
data = self.data
if isinstance(data, etree._Element):
return xml2str(data)
return str(data)
def __cmp__(self, other):
result = cmp(self.spine_position, other.spine_position)
if result != 0: