def deleteotherfile(self, book_href): id = unicode_str(book_href) id = unquoteurl(id) if id is None: raise WrapperException('None is not a valid book hrefbook href') if id not in self.other and id in self.id_to_href: raise WrapperException( 'Incorrect interface routine - use deletefile') filepath = self.book_href_to_filepath.get(id, None) if filepath is None: raise WrapperException('Book href does not exist') if id in PROTECTED_FILES: raise WrapperException('attempt to delete protected file') add_to_deleted = True # if file was added or modified delete file from outdir if id in self.added or id in self.modified: filepath = os.path.join(self.outdir, filepath) if unipath.exists(filepath) and unipath.isfile(filepath): os.remove(filepath) if id in self.added: self.added.remove(id) add_to_deleted = False if id in self.other: self.other.remove(id) if id in self.modified: del self.modified[id] if add_to_deleted: self.deleted.append(('other', id, book_href)) del self.book_href_to_filepath[id]
def deleteotherfile(self, book_href): id = unicode_str(book_href) if id in self.id_to_href: raise WrapperException('Incorrect interface routine - use deletefile') filepath = self.id_to_filepath.get(id, None) if id is None: raise WrapperException('book href does not exist') if id in PROTECTED_FILES: raise WrapperException('attempt to delete protected file') add_to_deleted = True # if file was added or modified delete file from outdir if id in self.added or id in self.modified: filepath = os.path.join(self.outdir,filepath) if unipath.exists(filepath) and unipath.isfile(filepath): os.remove(filepath) if id in self.added: self.added.remove(id) add_to_deleted = False if id in self.other: self.other.remove(id) if id in self.modified: del self.modified[id] if add_to_deleted: self.deleted.append(('other', id, book_href)) del self.id_to_filepath[id]
def deletefile(self, id): id = unicode_str(id) filepath = self.id_to_filepath.get(id, None) if id is None: raise WrapperException('id does not exist in manifest') add_to_deleted = True # if file was added or modified, delete file from outdir if id in self.added or id in self.modified: filepath = os.path.join(self.outdir, filepath) if unipath.exists(filepath) and unipath.isfile(filepath): os.remove(pathof(filepath)) if id in self.added: self.added.remove(id) add_to_deleted = False if id in self.modified: del self.modified[id] # remove from manifest href = self.id_to_href[id] del self.id_to_href[id] del self.id_to_mime[id] del self.href_to_id[href] # remove from spine new_spine = [] was_modified = False for sid, linear in self.spine: if sid != id: new_spine.append((sid, linear)) else: was_modified = True if was_modified: setspine(new_spine) if add_to_deleted: self.deleted.append(id) self.modified['OEBPS/content.opf'] = 'file' del self.id_to_filepath[id]
def deletefile(self, id): id = unicode_str(id) filepath = self.id_to_filepath.get(id, None) if id is None: raise WrapperException('id does not exist in manifest') add_to_deleted = True # if file was added or modified, delete file from outdir if id in self.added or id in self.modified: filepath = os.path.join(self.outdir,filepath) if unipath.exists(filepath) and unipath.isfile(filepath): os.remove(pathof(filepath)) if id in self.added: self.added.remove(id) add_to_deleted = False if id in self.modified: del self.modified[id] # remove from manifest href = self.id_to_href[id] del self.id_to_href[id] del self.id_to_mime[id] del self.href_to_id[href] # remove from spine new_spine = [] was_modified = False for sid, linear in self.spine: if sid != id: new_spine.append((sid, linear)) else: was_modified = True if was_modified: setspine(new_spine) if add_to_deleted: self.deleted.append(id) self.modified['OEBPS/content.opf'] = 'file' del self.id_to_filepath[id]
def deletefile(self, id): id = unicode_str(id) if id not in self.id_to_href: raise WrapperException('Id does not exist in manifest') filepath = self.id_to_filepath.get(id, None) if id is None: raise WrapperException('Id does not exist in manifest') if self.epub_version.startswith("2") and id == self.gettocid(): raise WrapperException('Can not add or remove an ncx under epub2') add_to_deleted = True # if file was added or modified, delete file from outdir if id in self.added or id in self.modified: filepath = os.path.join(self.outdir, filepath) if unipath.exists(filepath) and unipath.isfile(filepath): os.remove(pathof(filepath)) if id in self.added: self.added.remove(id) add_to_deleted = False if id in self.modified: del self.modified[id] # remove from manifest href = self.id_to_href[id] mime = self.id_to_mime[id] bookpath = self.id_to_bookpath[id] del self.id_to_href[id] del self.id_to_mime[id] del self.id_to_props[id] del self.id_to_fall[id] del self.id_to_over[id] del self.id_to_bookpath[id] del self.href_to_id[href] del self.bookpath_to_id[bookpath] # remove from spine new_spine = [] was_modified = False for sid, linear, properties in self.spine: if sid != id: new_spine.append((sid, linear, properties)) else: was_modified = True if was_modified: self.setspine_epub3(new_spine) if add_to_deleted: self.deleted.append(('manifest', id, bookpath)) self.modified[self.opfbookpath] = 'file' del self.id_to_filepath[id]
def addotherfile(self, book_href, data) : id = unicode_str(book_href) if id in self.other: raise WrapperException('book href must be unquie') desired_path = id.replace("/",os.sep) filepath = os.path.join(self.outdir,desired_path) if unipath.isfile(filepath): raise WrapperException('desired path already exists') base = os.path.dirname(filepath) if not unipath.exists(base): os.makedirs(pathof(base)) if isinstance(data, text_type): data = utf8_str(data) with open(pathof(filepath),'wb')as fp: fp.write(data) self.other.append(id) self.added.append(id) self.id_to_filepath[id] = desired_path
def zipUpDir(self, myzip, tdir, localname, compress_type=zipfile.ZIP_DEFLATED): currentdir = tdir if localname != "": currentdir = os.path.join(currentdir, localname) list = unipath.listdir(currentdir) for file in list: afilename = file localfilePath = os.path.join(localname, afilename) realfilePath = os.path.join(currentdir, file) if unipath.isfile(realfilePath): myzip.write(pathof(realfilePath), pathof(localfilePath), compress_type) elif unipath.isdir(realfilePath): self.zipUpDir(myzip, tdir, localfilePath)
def deleteotherfile(self, book_href): id = unicode_str(book_href) filepath = self.id_to_filepath.get(id, None) if id is None: raise WrapperException('book href does not exist') if id in PROTECTED_FILES: raise WrapperException('attempt to delete protected file') add_to_deleted = True # if file was added or modified delete file from outdir if id in self.added or id in self.modified: filepath = os.path.join(self.outdir, filepath) if unipath.exists(filepath) and unipath.isfile(filepath): os.remove(filepath) if id in self.added: self.added.remove(id) add_to_deleted = False if id in self.other: self.other.remove(id) if id in self.modified: del self.modified[id] if add_to_deleted: self.deleted.append(id) del self.id_to_filepath[id]
def main(argv=unicode_argv()): if len(argv) != 5: failed(None, msg="Launcher: improper number of arguments passed to launcher.py") return -1 ebook_root = argv[1] outdir = argv[2] script_type = argv[3] target_file = argv[4] script_home = os.path.dirname(target_file) script_module = os.path.splitext(os.path.basename(target_file))[0] # do basic sanity checking anyway if script_type not in SUPPORTED_SCRIPT_TYPES: failed(None, msg="Launcher: script type %s is not supported" % script_type) return -1 ok = unipath.exists(ebook_root) and unipath.isdir(ebook_root) ok = ok and unipath.exists(outdir) and unipath.isdir(outdir) ok = ok and unipath.exists(script_home) and unipath.isdir(script_home) ok = ok and unipath.exists(target_file) and unipath.isfile(target_file) if not ok: failed(None, msg="Launcher: missing or incorrect paths passed in") return -1 # update sys with path to target module home directory if script_home not in sys.path: sys.path.append(script_home) # load and parse opf if present op = None opf_path = os.path.join(ebook_root, "OEBPS", "content.opf") if unipath.exists(opf_path) and unipath.isfile(opf_path): op = Opf_Parser(opf_path) # create a wrapper for record keeping and safety rk = Wrapper(ebook_root, outdir, op) # get the correct container if script_type == "edit": bc = BookContainer(rk) elif script_type == "input": bc = InputContainer(rk) else: bc = OutputContainer(rk) # start the target script ps = ProcessScript(script_type, script_module, bc) ps.launch() # get standard error and standard out from the target script successmsg = "" for data in ps.stdouttext: successmsg += unicode_str(data) successmsg = escapeit(successmsg) errorlog = "" for data in ps.stderrtext: errorlog += unicode_str(data) errorlog = escapeit(errorlog) # get the target's script wrapper xml resultxml = "".join(ps.wrapout) resultxml += "<msg>\n" if ps.exitcode == 0: resultxml += successmsg if _DEBUG: resultxml += errorlog else: if _DEBUG: resultxml += successmsg resultxml += errorlog resultxml += "</msg>\n</wrapper>\n" # write it to stdout and exit if PY3: sys.stdout.buffer.write(utf8_str(resultxml)) else: sys.stdout.write(utf8_str(resultxml)) return 0
def main(argv=unicode_argv()): if len(argv) != 5: failed( None, msg="Launcher: improper number of arguments passed to launcher.py") return -1 ebook_root = argv[1] outdir = argv[2] script_type = argv[3] target_file = argv[4] script_home = os.path.dirname(target_file) plugin_name = os.path.split(script_home)[-1] plugin_dir = os.path.dirname(script_home) script_module = os.path.splitext(os.path.basename(target_file))[0] # do basic sanity checking anyway if script_type not in SUPPORTED_SCRIPT_TYPES: failed(None, msg="Launcher: script type %s is not supported" % script_type) return -1 ok = unipath.exists(ebook_root) and unipath.isdir(ebook_root) ok = ok and unipath.exists(outdir) and unipath.isdir(outdir) ok = ok and unipath.exists(script_home) and unipath.isdir(script_home) ok = ok and unipath.exists(target_file) and unipath.isfile(target_file) if not ok: failed(None, msg="Launcher: missing or incorrect paths passed in") return -1 # update sys with path to target module home directory sys.path.append(script_home) # load and parse opf if present op = None opf_path = os.path.join(ebook_root, 'OEBPS', 'content.opf') if unipath.exists(opf_path) and unipath.isfile(opf_path): op = Opf_Parser(opf_path) # create a wrapper for record keeping and safety rk = Wrapper(ebook_root, outdir, op, plugin_dir, plugin_name) # get the correct container if script_type == 'edit': bc = BookContainer(rk) elif script_type == 'input': bc = InputContainer(rk) elif script_type == 'validation': bc = ValidationContainer(rk) else: bc = OutputContainer(rk) # start the target script ps = ProcessScript(script_type, script_module, bc) ps.launch() # get standard error and standard out from the target script successmsg = '' for data in ps.stdouttext: successmsg += unicode_str(data) successmsg = escapeit(successmsg) errorlog = '' for data in ps.stderrtext: errorlog += unicode_str(data) errorlog = escapeit(errorlog) # get the target's script wrapper xml resultxml = "".join(ps.wrapout) resultxml += "<msg>\n" if ps.exitcode == 0: resultxml += successmsg if _DEBUG: resultxml += errorlog else: if _DEBUG: resultxml += successmsg resultxml += errorlog resultxml += '</msg>\n</wrapper>\n' # write it to stdout and exit if PY3: sys.stdout.buffer.write(utf8_str(resultxml)) else: sys.stdout.write(utf8_str(resultxml)) return 0