示例#1
0
def run(args=None):
    _parser, options, plugin_manager = get_parser_options_plugins(args=args)

    if not check_closed_streams(options):
        return ExitCode.bad_args

    if hasattr(os, 'nice'):
        os.nice(5)

    verbosity = options.verbose
    if not os.isatty(sys.stderr.fileno()):
        options.progress_bar = False
    if options.quiet:
        verbosity = Verbosity.quiet
        options.progress_bar = False
    configure_logging(verbosity,
                      progress_bar_friendly=options.progress_bar,
                      manage_root_logger=True)
    log.debug('ocrmypdf %s', __version__)
    try:
        check_options(options, plugin_manager)
    except ValueError as e:
        log.error(e)
        return ExitCode.bad_args
    except BadArgsError as e:
        log.error(e)
        return e.exit_code
    except MissingDependencyError as e:
        log.error(e)
        return ExitCode.missing_dependency

    result = run_pipeline(options=options, plugin_manager=plugin_manager)
    return result
示例#2
0
def test_old_unpaper(resources, no_outpdf):
    input_ = fspath(resources / "c02-22.pdf")
    output = fspath(no_outpdf)

    _parser, options, pm = get_parser_options_plugins(
        ["--clean", input_, output])
    with patch("ocrmypdf._exec.unpaper.version") as mock_unpaper_version:
        mock_unpaper_version.return_value = '0.5'

        with pytest.raises(MissingDependencyError):
            check_options(options, pm)
示例#3
0
def test_no_unpaper(resources, no_outpdf):
    input_ = fspath(resources / "c02-22.pdf")
    output = fspath(no_outpdf)

    _parser, options, pm = get_parser_options_plugins(
        ["--clean", input_, output])
    with patch("ocrmypdf._exec.unpaper.version") as mock_unpaper_version:
        mock_unpaper_version.side_effect = FileNotFoundError("unpaper")

        with pytest.raises(MissingDependencyError):
            check_options(options, pm)
示例#4
0
def run_ocrmypdf_api(input_file, output_file, *args):
    """Run ocrmypdf via API and let caller deal with results

    Does not currently have a way to manipulate the PATH except for Tesseract.
    """

    args = [str(input_file), str(output_file)
            ] + [str(arg) for arg in args if arg is not None]
    _parser, options, plugin_manager = get_parser_options_plugins(args=args)

    api.check_options(options, plugin_manager)
    return api.run_pipeline(options, plugin_manager=None, api=False)
示例#5
0
def check_ocrmypdf(input_file, output_file, *args):
    """Run ocrmypdf and confirmed that a valid file was created"""
    args = [str(input_file), str(output_file)
            ] + [str(arg) for arg in args if arg is not None]

    _parser, options, plugin_manager = get_parser_options_plugins(args=args)
    api.check_options(options, plugin_manager)
    result = api.run_pipeline(options, plugin_manager=plugin_manager, api=True)

    assert result == 0
    assert output_file.exists(), "Output file not created"
    assert output_file.stat().st_size > 100, "PDF too small or empty"

    return output_file