Fix some error messages that printed directly to sys.stderr instead of logging
This commit is contained in:
@@ -22,15 +22,7 @@ from collections import namedtuple
|
||||
from contextlib import suppress
|
||||
from functools import lru_cache
|
||||
from os import fspath
|
||||
from subprocess import (
|
||||
PIPE,
|
||||
STDOUT,
|
||||
CalledProcessError,
|
||||
TimeoutExpired,
|
||||
check_output,
|
||||
run,
|
||||
)
|
||||
from textwrap import dedent
|
||||
from subprocess import PIPE, STDOUT, CalledProcessError, TimeoutExpired, run
|
||||
|
||||
from . import get_version
|
||||
from ..exceptions import (
|
||||
@@ -88,8 +80,9 @@ def has_textonly_pdf(tesseract_env=None):
|
||||
)
|
||||
params = proc.stdout
|
||||
except CalledProcessError as e:
|
||||
print("Could not --print-parameters from tesseract", file=sys.stderr)
|
||||
raise MissingDependencyError from e
|
||||
raise MissingDependencyError(
|
||||
"Could not --print-parameters from tesseract"
|
||||
) from e
|
||||
if 'textonly_pdf' in params:
|
||||
return True
|
||||
return False
|
||||
@@ -97,14 +90,13 @@ def has_textonly_pdf(tesseract_env=None):
|
||||
|
||||
def languages(tesseract_env=None):
|
||||
def lang_error(output):
|
||||
msg = dedent(
|
||||
"""Tesseract failed to report available languages.
|
||||
Output from Tesseract:
|
||||
-----------
|
||||
"""
|
||||
msg = (
|
||||
"Tesseract failed to report available languages.\n"
|
||||
"Output from Tesseract:\n"
|
||||
"-----------\n"
|
||||
)
|
||||
msg += output
|
||||
print(msg, file=sys.stderr)
|
||||
return msg
|
||||
|
||||
args_tess = ['tesseract', '--list-langs']
|
||||
try:
|
||||
@@ -118,13 +110,11 @@ def languages(tesseract_env=None):
|
||||
)
|
||||
output = proc.stdout
|
||||
except CalledProcessError as e:
|
||||
lang_error(e.output)
|
||||
raise MissingDependencyError from e
|
||||
raise MissingDependencyError(lang_error(e.output)) from e
|
||||
|
||||
header, *rest = output.splitlines()
|
||||
if not header.startswith('List of available languages'):
|
||||
lang_error(output)
|
||||
raise MissingDependencyError
|
||||
raise MissingDependencyError(lang_error(output))
|
||||
return set(lang.strip() for lang in rest)
|
||||
|
||||
|
||||
|
||||
@@ -29,11 +29,7 @@ from tempfile import TemporaryDirectory
|
||||
from . import get_version
|
||||
from ..exceptions import MissingDependencyError, SubprocessOutputError
|
||||
|
||||
try:
|
||||
from PIL import Image
|
||||
except ImportError:
|
||||
print("Could not find Python3 imaging library", file=sys.stderr)
|
||||
raise
|
||||
from PIL import Image
|
||||
|
||||
|
||||
@lru_cache(maxsize=1)
|
||||
@@ -55,16 +51,18 @@ def run(input_file, output_file, dpi, log, mode_args):
|
||||
else:
|
||||
im = im.convert(mode='RGB')
|
||||
except IOError as e:
|
||||
log.error("Could not convert image with type " + im.mode)
|
||||
im.close()
|
||||
raise MissingDependencyError() from e
|
||||
raise MissingDependencyError(
|
||||
"Could not convert image with type " + im.mode
|
||||
) from e
|
||||
|
||||
try:
|
||||
suffix = SUFFIXES[im.mode]
|
||||
except KeyError:
|
||||
log.error("Failed to convert image to a supported format.")
|
||||
im.close()
|
||||
raise MissingDependencyError() from e
|
||||
raise MissingDependencyError(
|
||||
"Failed to convert image to a supported format."
|
||||
) from e
|
||||
|
||||
with TemporaryDirectory() as tmpdir:
|
||||
input_pnm = os.path.join(tmpdir, f'input{suffix}')
|
||||
|
||||
+2
-2
@@ -37,8 +37,8 @@ else:
|
||||
# pylint: disable=E1101
|
||||
# pytest.helpers is dynamic so it confuses pylint
|
||||
|
||||
if sys.version_info.major < 3:
|
||||
print("Requires Python 3.4+")
|
||||
if sys.version_info < (3, 5):
|
||||
print("Requires Python 3.5+")
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user