Skip to content

Commit

Permalink
New .author, .title, .language properties with getter/setter.
Browse files Browse the repository at this point in the history
  • Loading branch information
gabalese committed Sep 18, 2013
1 parent 167ab0c commit 6aa9752
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 11 deletions.
56 changes: 46 additions & 10 deletions pyepub/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def __init__(self, filename, mode="r", title=None, language=None):
"Can't overwrite existing file: %s" % filename
self.filename = filename
zipfile.ZipFile.__init__(self, self.filename, mode="w")
self.__init__write(title, language)
self.__init__write()
elif mode == "a":
assert not isinstance(filename, StringIO), \
"Can't append to StringIO object, use write instead: %s" % filename
Expand Down Expand Up @@ -148,7 +148,7 @@ def __init__read(self, filename):
# loops over nested
# navPoints

def __init__write(self, title, language):
def __init__write(self):
"""
Init an empty EPUB
Expand All @@ -166,8 +166,8 @@ def __init__write(self, title, language):
self.writestr('mimetype', "application/epub+zip")
self.writestr('META-INF/container.xml', self._containerxml())
self.info["metadata"]["creator"] = "py-clave server"
self.info["metadata"]["title"] = title
self.info["metadata"]["language"] = language
self.info["metadata"]["title"] = ""
self.info["metadata"]["language"] = ""

# Problem is: you can't overwrite file contents with python ZipFile
# so you must add contents BEFORE finalizing the file
Expand All @@ -179,6 +179,38 @@ def __init__write(self, title, language):
self.writestr(self.opf_path, ET.tostring(self.opf, encoding="UTF-8")) # temporary opf & ncx
self.writestr(self.ncx_path, ET.tostring(self.ncx, encoding="UTF-8")) # will be re-init on close()

@property
def author(self):
return self.info["metadata"]["creator"]

@author.setter
def author(self, value):
tmp = self.opf.find(".//{0}creator".format(NAMESPACE["dc"]))
tmp.text = value
self.info["metadata"]["creator"] = value

@property
def language(self):
return self.info["metadata"]["language"]

@property
def title(self):
return self.info["metadata"]["title"]

@title.setter
def title(self, value):
tmp = self.opf.find(".//{0}title".format(NAMESPACE["dc"]))
tmp.text = value
ncx_title = self.ncx.find("{http://www.daisy.org/z3986/2005/ncx/}docTitle")[0]
ncx_title.text = value
self.info["metadata"]["title"] = value

@language.setter
def language(self, value):
tmp = self.opf.find(".//{0}language".format(NAMESPACE["dc"]))
tmp.text = value
self.info["metadata"]["language"] = value

def close(self):
if self.fp is None: # Check file status
return
Expand Down Expand Up @@ -225,8 +257,9 @@ def _init_opf(self):
<package xmlns="http://www.idpf.org/2007/opf" unique-identifier="BookId" version="2.0">
<metadata xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:opf="http://www.idpf.org/2007/opf">
<dc:identifier id="BookId" opf:scheme="UUID">{uid}</dc:identifier>
<dc:title>{title}</dc:title>
<dc:language>{lang}</dc:language>
<dc:title></dc:title>
<dc:creator></dc:creator>
<dc:language></dc:language>
<dc:date opf:event="modification">{date}</dc:date>
</metadata>
<manifest>
Expand All @@ -238,10 +271,7 @@ def _init_opf(self):
</guide>
</package>"""

doc = opf_tmpl.format(uid=self.uid,
date=today,
title=self.info["metadata"]["title"].encode('utf-8'),
lang=self.info["metadata"]["language"].encode('utf-8'))
doc = opf_tmpl.format(uid=self.uid, date=today)
return doc

def _init_ncx(self):
Expand Down Expand Up @@ -377,3 +407,9 @@ def writetodisk(self, filename):
self.filename.seek(0)
f.write(self.filename.read())
f.close()

def __del__(self):
try:
self.fp.close()
except ValueError:
pass
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name='pyepub',
version='0.2.9',
version='0.3',
packages=['pyepub'],
url='http://blog.alese.it/pyepub',
license='MIT',
Expand Down

0 comments on commit 6aa9752

Please sign in to comment.