def readotherfile(self, book_href): id = _unicodestr(book_href) id = urldecodepart(id) if id is None: raise WrapperException('None is not a valid book href') if id not in self.other and id in self.id_to_href: raise WrapperException('Incorrect interface routine - use readfile') # handle special case of trying to read the opf after it has been modified if id == self.opfbookpath: if id in self.modified: return self.build_opf() filepath = self.book_href_to_filepath.get(id, None) if filepath is None: raise WrapperException('Book href does not exist') basedir = self.ebook_root if id in self.added or id in self.modified: basedir = self.outdir filepath = os.path.join(basedir, filepath) if not os.path.exists(filepath): raise WrapperException('File Does Not Exist') basename = os.path.basename(filepath) ext = os.path.splitext(basename)[1] ext = ext.lower() mime = ext_mime_map.get(ext, "") data = b'' with open(filepath, 'rb') as fp: data = fp.read() if mime in TEXT_MIMETYPES: data = _unicodestr(data) return data
def getmime(self, href): href = _unicodestr(href) href = urldecodepart(href) filename = os.path.basename(href) ext = os.path.splitext(filename)[1] ext = ext.lower() return ext_mime_map.get(ext, "")
def getmime(self, href): href = unicode_str(href) href = unquoteurl(href) filename = os.path.basename(href) ext = os.path.splitext(filename)[1] ext = ext.lower() return ext_mime_map.get(ext, "")
def addfile(self, uniqueid, basename, data, mime=None, properties=None, fallback=None, overlay=None): uniqueid = unicode_str(uniqueid) if uniqueid in self.id_to_href: raise WrapperException('Manifest Id is not unique') basename = unicode_str(basename) mime = unicode_str(mime) if mime is None: ext = os.path.splitext(basename)[1] ext = ext.lower() mime = ext_mime_map.get(ext, None) if mime is None: raise WrapperException("Mime Type Missing") if mime == "application/x-dtbncx+xml" and self.epub_version.startswith( "2"): raise WrapperException('Can not add or remove an ncx under epub2') group = mime_group_map.get(mime, "Misc") default_path = self.group_paths[group][0] bookpath = basename if default_path != "": bookpath = default_path + "/" + basename href = buildRelativePath(self.opfbookpath, bookpath) if href in self.href_to_id: raise WrapperException('Basename already exists') # now actually write out the new file filepath = bookpath.replace("/", os.sep) self.id_to_filepath[uniqueid] = filepath filepath = os.path.join(self.outdir, filepath) base = os.path.dirname(filepath) if not unipath.exists(base): os.makedirs(base) if mime in TEXT_MIMETYPES or isinstance(data, text_type): data = utf8_str(data) with open(filepath, 'wb') as fp: fp.write(data) self.id_to_href[uniqueid] = href self.id_to_mime[uniqueid] = mime self.id_to_props[uniqueid] = properties self.id_to_fall[uniqueid] = fallback self.id_to_over[uniqueid] = overlay self.id_to_bookpath[uniqueid] = bookpath self.href_to_id[href] = uniqueid self.bookpath_to_id[bookpath] = uniqueid self.added.append(uniqueid) self.modified[self.opfbookpath] = 'file' return uniqueid
def addbookpath(self, uniqueid, bookpath, data, mime=None): uniqueid = _unicodestr(uniqueid) if uniqueid in self.id_to_href: raise WrapperException('Manifest Id is not unique') bookpath = _unicodestr(bookpath) basename = bookpath.split("/")[-1] mime = _unicodestr(mime) if mime is None: ext = os.path.splitext(basename)[1] ext = ext.lower() mime = ext_mime_map.get(ext, None) if mime is None: raise WrapperException("Mime Type Missing") if mime == "application/x-dtbncx+xml" and self.epub_version.startswith( "2"): raise WrapperException('Can not add or remove an ncx under epub2') href = buildRelativePath(self.opfbookpath, bookpath) if href in self.href_to_id: raise WrapperException('bookpath already exists') # now actually write out the new file filepath = bookpath.replace("/", os.sep) self.id_to_filepath[uniqueid] = filepath filepath = os.path.join(self.outdir, filepath) base = os.path.dirname(filepath) if not os.path.exists(base): os.makedirs(base) if mime in TEXT_MIMETYPES or isinstance(data, str): data = _utf8str(data) with open(filepath, 'wb') as fp: fp.write(data) self.id_to_href[uniqueid] = href self.id_to_mime[uniqueid] = mime self.id_to_props[uniqueid] = None self.id_to_fall[uniqueid] = None self.id_to_over[uniqueid] = None self.id_to_bookpath[uniqueid] = bookpath self.href_to_id[href] = uniqueid self.bookpath_to_id[bookpath] = uniqueid self.added.append(uniqueid) self.modified[self.opfbookpath] = 'file' return uniqueid