Move app specific settings a library may not want to __main__

This commit is contained in:
James R. Barlow
2019-05-16 01:29:26 -07:00
parent 9d750828c7
commit 471cdea232
3 changed files with 38 additions and 26 deletions
+27 -4
View File
@@ -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
+1 -6
View File
@@ -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
+10 -16
View File
@@ -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):