From 471cdea23281b8ac106604b17a5023d7c08981e9 Mon Sep 17 00:00:00 2001 From: "James R. Barlow" Date: Thu, 16 May 2019 01:29:26 -0700 Subject: [PATCH] Move app specific settings a library may not want to __main__ --- src/ocrmypdf/__main__.py | 31 +++++++++++++++++++++++++++---- src/ocrmypdf/_jobcontext.py | 7 +------ src/ocrmypdf/_sync.py | 26 ++++++++++---------------- 3 files changed, 38 insertions(+), 26 deletions(-) diff --git a/src/ocrmypdf/__main__.py b/src/ocrmypdf/__main__.py index 3a154a2c..074ba51b 100755 --- a/src/ocrmypdf/__main__.py +++ b/src/ocrmypdf/__main__.py @@ -22,7 +22,9 @@ import os import sys from . import PROGRAM_NAME, VERSION +from .exceptions import ExitCode from ._sync import run_pipeline +from ._validation import check_closed_streams # ------------- # Parser @@ -35,7 +37,7 @@ def numeric(basetype, min_=None, max_=None): def _numeric(string): value = basetype(string) - if min_ is not None and value < min_ or max_ is not None and value > max_: + if (min_ is not None and value < min_) or (max_ is not None and value > max_): msg = "%r not in valid range %r" % (string, (min_, max_)) raise argparse.ArgumentTypeError(msg) return value @@ -462,16 +464,37 @@ debugging.add_argument( ) -def run(args=None): - options = parser.parse_args(args=args) +def setup_app_logging(options): + """Set up logging""" log = logging.getLogger() formatter = logging.Formatter('%(levelname)7s - %(message)s') console = logging.StreamHandler(stream=sys.stderr) - console.setLevel(logging.DEBUG) console.setFormatter(formatter) log.addHandler(console) + if options.quiet: + log.setLevel(logging.ERROR) + elif options.verbose >= 2: + log.setLevel(logging.DEBUG) + else: + log.setLevel(logging.INFO) + +def configure_app_environment(options): + """Configure the application environment + + Don't do anything here that a library user would not expect. + """ + if not check_closed_streams(options): + return ExitCode.bad_args + if hasattr(os, 'nice'): + os.nice(5) + + +def run(args=None): + options = parser.parse_args(args=args) + setup_app_logging(options) + configure_app_environment(options) result = run_pipeline(options) return result diff --git a/src/ocrmypdf/_jobcontext.py b/src/ocrmypdf/_jobcontext.py index 290a15e5..9b4b87b0 100644 --- a/src/ocrmypdf/_jobcontext.py +++ b/src/ocrmypdf/_jobcontext.py @@ -96,10 +96,5 @@ def get_logger(options=None, prefix='ocrmypdf', filename=None, page=None): adapter = LogNameAdapter(log, dict(filename=filename)) else: adapter = log - if options.quiet: - log.setLevel(logging.ERROR) - elif options.verbose >= 2: - log.setLevel(logging.DEBUG) - else: - log.setLevel(logging.INFO) + adapter.setLevel(logging.DEBUG) return adapter diff --git a/src/ocrmypdf/_sync.py b/src/ocrmypdf/_sync.py index d7676a78..40534b18 100644 --- a/src/ocrmypdf/_sync.py +++ b/src/ocrmypdf/_sync.py @@ -50,7 +50,6 @@ from .exceptions import ExitCode, ExitCodeException from . import VERSION from .helpers import available_cpu_count from ._validation import ( - check_closed_streams, check_options, check_dependency_versions, check_environ, @@ -163,18 +162,12 @@ def exec_concurrent(context): def run_pipeline(options): - if not check_closed_streams(options): - return ExitCode.bad_args - - # Default to INFO level - if options.verbose is None: - options.verbose = 2 - - log = get_logger(options, 'Setup: ') + log = get_logger(options, __name__) log.debug('ocrmypdf ' + VERSION) - check_code = check_options(options, log) - if check_code != ExitCode.ok: - return check_code + + result = check_options(options, log) + if result != ExitCode.ok: + return result check_dependency_versions(options, log) # Any changes to options will not take effect for options that are already @@ -196,8 +189,6 @@ def run_pipeline(options): work_folder = mkdtemp(prefix="com.github.ocrmypdf.") atexit.register(cleanup_working_files, work_folder, options) - if hasattr(os, 'nice'): - os.nice(5) try: check_requested_output_file(options, log) @@ -212,7 +203,7 @@ def run_pipeline(options): pdfinfo = get_pdfinfo(origin_pdf, detailed_page_analysis=options.redo_ocr) context = PDFContext(options, work_folder, origin_pdf, pdfinfo) - # Validate options are okey for this pdf + # Validate options are okay for this pdf validate_pdfinfo_options(context) # Execute the pipeline @@ -235,7 +226,10 @@ def run_pipeline(options): msg = f"Output file is a {pdfa_info['conformance']} (as expected)" log.info(msg) else: - msg = f"Output file is okay but is not PDF/A (seems to be {pdfa_info['conformance']})" + msg = ( + f"Output file is okay but is not PDF/A " + f"(seems to be {pdfa_info['conformance']})" + ) log.warning(msg) return ExitCode.pdfa_conversion_failed if not qpdf.check(options.output_file, log):