def test(self): self._check_target_options( ["pages-dir", "input-dir", "transform-file"]) # Check for presence of ImageMagick cmd_run(["convert", "-version"], fail_msg=_COMMAND_NOT_FOUND_MSG.format(command="convert", package="ImageMagick"))
def test(self): self._check_target_options( ["toc-file", "tmp-file", "pdf-file", "pdf-tmp-file"]) cmd_run(["gs", "--version"], fail_msg=_COMMAND_NOT_FOUND_MSG.format(command="gs", package="GhostScript"))
def handler(info): try: os.remove(info["output"]) except OSError as err: if err.errno != errno.ENOENT: raise cmd_run(["c44", info["input"], info["output"]])
def test(self): self._check_target_options(["pages-dir", "input-dir", "transform-file"]) # Check for presence of ImageMagick cmd_run(["convert", "-version"], fail_msg=_COMMAND_NOT_FOUND_MSG.format( command="convert", package="ImageMagick"))
def after_tasks(self): out_cache_dir = self._get_option("out-cache-dir") djvu_file = self._get_option("djvu-file") input_files = sorted(glob.glob(os.path.join(out_cache_dir, "*.djvu"))) if len(input_files) == 0: raise ProgramError(_("No input files.")) cmd_run(["djvm", "-create", djvu_file] + input_files)
def test(self): self._check_target_options(["toc-file", "tmp-file", "pdf-file", "pdf-tmp-file"]) cmd_run(["gs", "--version"], fail_msg=_COMMAND_NOT_FOUND_MSG.format( command="gs", package="GhostScript"))
def before_tasks(self): toc_file = self._get_option("toc-file") tmp_file = self._get_option("tmp-file") djvu_file = self._get_option("djvu-file") generate_toc(toc_file, tmp_file, _write_entry, "set-outline\n(bookmarks\n", ")\n.") cmd = ["djvused", "-s", "-f", tmp_file, djvu_file] cmd_run(cmd)
def test(self): self._check_target_options( ["in-cache-dir", "out-cache-dir", "pdf-file"]) cmd_run(["convert", "-version"], fail_msg=_COMMAND_NOT_FOUND_MSG.format(command="convert", package="ImageMagick")) cmd_run(["gs", "--version"], fail_msg=_COMMAND_NOT_FOUND_MSG.format(command="gs", package="GhostScript"))
def test(self): self._check_target_options(["in-cache-dir", "out-cache-dir", "pdf-file"]) cmd_run(["convert", "-version"], fail_msg=_COMMAND_NOT_FOUND_MSG.format( command="convert", package="ImageMagick")) cmd_run(["gs", "--version"], fail_msg=_COMMAND_NOT_FOUND_MSG.format( command="gs", package="GhostScript"))
def handler(info): config = ConfigParser.SafeConfigParser(_DEFAULT_TRANSFORM_OPTIONS) transform_file = info["transform-file"] try: with open(transform_file, "rt") as conffile: config.readfp(conffile) except (IOError, ConfigParser.Error) as err: raise ProgramError( _("Cannot read config file '{file}':\n{error}").format( file=transform_file, error=err)) try: justconvert = config.getboolean("transform", "justconvert") if not justconvert: chop = set(config.get("transform", "chop-edge").lower().split()) chop_size = config.getint("transform", "chop-size") chop_background = config.get("transform", "chop-background") odd = config.getint("transform", "rotate-odd") even = config.getint("transform", "rotate-even") blur = config.getint("transform", "blur") fuzz = config.getint("transform", "fuzz") if not set(config.options("transform")) <= _POSSIBLE_TRANSFORM_OPTIONS: raise ProgramError( _("Unhandled extra options in '{file}' file: {opts}.").format( file=transform_file, opts=list( set(config.options("transform")) - _POSSIBLE_TRANSFORM_OPTIONS))) except (ConfigParser.Error, ValueError) as err: raise ProgramError( _("Incorrect '{file}' file:\n{error}").format(file=transform_file, error=err)) if justconvert: cmd_run(["convert", info["input"], info["output"]]) return chop = _check_and_normalize_chop(transform_file, chop, chop_background) if info["num"] % 2 == 0: angle = even else: angle = odd cmd = [ "convert", info["input"], "-crop", _get_crop_area(info, chop, chop_size, chop_background, blur, fuzz), "+repage", "-rotate", str(angle), info["output"] ] cmd_run(cmd)
def handler(info): config = ConfigParser.SafeConfigParser(_DEFAULT_TRANSFORM_OPTIONS) transform_file = info["transform-file"] try: with open(transform_file, "rt") as conffile: config.readfp(conffile) except (IOError, ConfigParser.Error) as err: raise ProgramError(_( "Cannot read config file '{file}':\n{error}") .format(file=transform_file, error=err)) try: justconvert = config.getboolean("transform", "justconvert") if not justconvert: chop = set(config.get("transform", "chop-edge").lower().split()) chop_size = config.getint("transform", "chop-size") chop_background = config.get("transform", "chop-background") odd = config.getint("transform", "rotate-odd") even = config.getint("transform", "rotate-even") blur = config.getint("transform", "blur") fuzz = config.getint("transform", "fuzz") if not set(config.options("transform")) <= _POSSIBLE_TRANSFORM_OPTIONS: raise ProgramError(_( "Unhandled extra options in '{file}' file: {opts}.") .format(file=transform_file, opts=list(set(config.options("transform")) - _POSSIBLE_TRANSFORM_OPTIONS))) except (ConfigParser.Error, ValueError) as err: raise ProgramError(_( "Incorrect '{file}' file:\n{error}") .format(file=transform_file, error=err)) if justconvert: cmd_run(["convert", info["input"], info["output"]]) return chop = _check_and_normalize_chop(transform_file, chop, chop_background) if info["num"] % 2 == 0: angle = even else: angle = odd cmd = ["convert", info["input"], "-crop", _get_crop_area(info, chop, chop_size, chop_background, blur, fuzz), "+repage", "-rotate", str(angle), info["output"]] cmd_run(cmd)
def before_tasks(self): toc_file = self._get_option("toc-file") tmp_file = self._get_option("tmp-file") pdf_file = self._get_option("pdf-file") pdf_tmp_file = self._get_option("pdf-tmp-file") generate_toc(toc_file, tmp_file, _write_entry) cmd = [ "gs", "-dNOPAUSE", "-dBATCH", "-q", "-dSAFER", "-sDEVICE=pdfwrite", "-sOutputFile=%s" % pdf_tmp_file, pdf_file, tmp_file ] cmd_run(cmd) os.remove(pdf_file) shutil.move(pdf_tmp_file, pdf_file)
def after_tasks(self): out_cache_dir = self._get_option("out-cache-dir") pdf_file = self._get_option("pdf-file") input_files = sorted(glob.glob(os.path.join(out_cache_dir, "*.pdf"))) if len(input_files) == 0: raise ProgramError(_("No input files.")) try: os.remove(pdf_file) except OSError as err: if err.errno != errno.ENOENT: raise cmd_run([ "gs", "-dNOPAUSE", "-dBATCH", "-dSAFER", "-sDEVICE=pdfwrite", "-sOutputFile=%s" % pdf_file ] + input_files)
def after_tasks(self): out_cache_dir = self._get_option("out-cache-dir") pdf_file = self._get_option("pdf-file") input_files = sorted(glob.glob(os.path.join(out_cache_dir, "*.pdf"))) if len(input_files) == 0: raise ProgramError(_("No input files.")) try: os.remove(pdf_file) except OSError as err: if err.errno != errno.ENOENT: raise cmd_run(["gs", "-dNOPAUSE", "-dBATCH", "-dSAFER", "-sDEVICE=pdfwrite", "-sOutputFile=%s" % pdf_file] + input_files)
def before_tasks(self): toc_file = self._get_option("toc-file") tmp_file = self._get_option("tmp-file") pdf_file = self._get_option("pdf-file") pdf_tmp_file = self._get_option("pdf-tmp-file") generate_toc(toc_file, tmp_file, _write_entry) cmd = ["gs", "-dNOPAUSE", "-dBATCH", "-q", "-dSAFER", "-sDEVICE=pdfwrite", "-sOutputFile=%s" % pdf_tmp_file, pdf_file, tmp_file] cmd_run(cmd) os.remove(pdf_file) shutil.move(pdf_tmp_file, pdf_file)
def _get_crop_area(info, chop, chop_size, chop_background, blur, fuzz): BORDER_SIZE = 10 cmd = ["convert", info["input"], "-background", chop_background] for edge in chop: if edge in ["north", "south"]: sizestr = "0x" + str(chop_size) else: sizestr = str(chop_size) + "x0" cmd += ["-gravity", edge, "-chop", sizestr, "-splice", sizestr] cmd += ["-bordercolor", chop_background, "-border", "%sx%s" % (BORDER_SIZE, BORDER_SIZE), "-virtual-pixel", "edge", "-blur", "0x%d" % blur, "-fuzz", "%d%%" % fuzz, "-trim", "-format", "%X %Y %w %h", "info:-"] output = cmd_run(cmd) off_x, off_y, sz_x, sz_y = [int(x) for x in output.split()] # Adjust border off_x -= BORDER_SIZE off_y -= BORDER_SIZE # Adjust chop if "north" in chop: off_y -= chop_size sz_y += chop_size if "east" in chop: sz_x += chop_size if "south" in chop: sz_y += chop_size if "west" in chop: off_x -= chop_size sz_x += chop_size return "%dx%d%+d%+d" % (sz_x, sz_y, off_x, off_y)
def _get_crop_area(info, chop, chop_size, chop_background, blur, fuzz): BORDER_SIZE = 10 cmd = ["convert", info["input"], "-background", chop_background] for edge in chop: if edge in ["north", "south"]: sizestr = "0x" + str(chop_size) else: sizestr = str(chop_size) + "x0" cmd += ["-gravity", edge, "-chop", sizestr, "-splice", sizestr] cmd += [ "-bordercolor", chop_background, "-border", "%sx%s" % (BORDER_SIZE, BORDER_SIZE), "-virtual-pixel", "edge", "-blur", "0x%d" % blur, "-fuzz", "%d%%" % fuzz, "-trim", "-format", "%X %Y %w %h", "info:-" ] output = cmd_run(cmd) off_x, off_y, sz_x, sz_y = [int(x) for x in output.split()] # Adjust border off_x -= BORDER_SIZE off_y -= BORDER_SIZE # Adjust chop if "north" in chop: off_y -= chop_size sz_y += chop_size if "east" in chop: sz_x += chop_size if "south" in chop: sz_y += chop_size if "west" in chop: off_x -= chop_size sz_x += chop_size return "%dx%d%+d%+d" % (sz_x, sz_y, off_x, off_y)