Refactor --language argument into set
This commit is contained in:
@@ -74,24 +74,19 @@ def check_platform():
|
||||
|
||||
|
||||
def check_options_languages(options, plugin_manager):
|
||||
if not options.language:
|
||||
options.language = [DEFAULT_LANGUAGE]
|
||||
if not options.languages:
|
||||
options.languages = {DEFAULT_LANGUAGE}
|
||||
system_lang = locale.getlocale()[0]
|
||||
if system_lang and not system_lang.startswith('en'):
|
||||
log.debug("No language specified; assuming --language %s", DEFAULT_LANGUAGE)
|
||||
|
||||
# Support v2.x "eng+deu" language syntax
|
||||
if '+' in options.language[0]:
|
||||
options.language = options.language[0].split('+')
|
||||
|
||||
languages = set(options.language)
|
||||
ocr_engine = plugin_manager.hook.get_ocr_engine()
|
||||
if not languages.issubset(ocr_engine.languages(options)):
|
||||
if not options.languages.issubset(ocr_engine.languages(options)):
|
||||
msg = (
|
||||
f"{ocr_engine} does not have language data for the following "
|
||||
"requested languages: \n"
|
||||
)
|
||||
for lang in languages - ocr_engine.languages(options):
|
||||
for lang in options.languages - ocr_engine.languages(options):
|
||||
msg += lang + '\n'
|
||||
raise MissingDependencyError(msg)
|
||||
|
||||
@@ -101,8 +96,7 @@ def check_options_output(options):
|
||||
# 1. Ghostscript < 9.20 mangles multibyte Unicode
|
||||
# 2. hocr doesn't work on non-Latin languages (so don't select it)
|
||||
|
||||
languages = set(options.language)
|
||||
is_latin = languages.issubset(HOCR_OK_LANGS)
|
||||
is_latin = options.languages.issubset(HOCR_OK_LANGS)
|
||||
|
||||
if options.pdf_renderer == 'hocr' and not is_latin:
|
||||
msg = (
|
||||
|
||||
@@ -95,7 +95,7 @@ def check_options(options):
|
||||
options.pdf_renderer = 'sandwich'
|
||||
|
||||
if options.pdf_renderer == 'sandwich' and not tesseract.has_textonly_pdf(
|
||||
options.tesseract_env, set(options.language)
|
||||
options.tesseract_env, set(options.languages)
|
||||
):
|
||||
raise MissingDependencyError(
|
||||
"You are using an alpha version of Tesseract 4.0 that does not support "
|
||||
@@ -147,7 +147,7 @@ class TesseractOcrEngine(OcrEngine):
|
||||
input_file=input_file,
|
||||
output_hocr=output_hocr,
|
||||
output_text=output_text,
|
||||
languages=options.language,
|
||||
languages=options.languages,
|
||||
engine_mode=options.tesseract_oem,
|
||||
tessconfig=options.tesseract_config,
|
||||
timeout=options.tesseract_timeout,
|
||||
@@ -163,7 +163,7 @@ class TesseractOcrEngine(OcrEngine):
|
||||
input_file=input_file,
|
||||
output_pdf=output_pdf,
|
||||
output_text=output_text,
|
||||
languages=options.language,
|
||||
languages=options.languages,
|
||||
engine_mode=options.tesseract_oem,
|
||||
tessconfig=options.tesseract_config,
|
||||
timeout=options.tesseract_timeout,
|
||||
|
||||
+16
-1
@@ -54,6 +54,20 @@ class ArgumentParser(argparse.ArgumentParser):
|
||||
raise ValueError(message)
|
||||
|
||||
|
||||
class LanguageSetAction(argparse.Action):
|
||||
def __init__(self, option_strings, dest, default=None, **kwargs):
|
||||
if default is None:
|
||||
default = set()
|
||||
super().__init__(option_strings, dest, default=default, **kwargs)
|
||||
|
||||
def __call__(self, parser, namespace, values, option_string=None):
|
||||
dest = getattr(namespace, self.dest)
|
||||
if '+' in values:
|
||||
dest.add(lang for lang in values.split('+'))
|
||||
else:
|
||||
dest.add(values)
|
||||
|
||||
|
||||
def get_parser():
|
||||
parser = ArgumentParser(
|
||||
prog=_PROGRAM_NAME,
|
||||
@@ -126,7 +140,8 @@ Online documentation is located at:
|
||||
parser.add_argument(
|
||||
'-l',
|
||||
'--language',
|
||||
action='append',
|
||||
dest='languages',
|
||||
action=LanguageSetAction,
|
||||
help="Language(s) of the file to be OCRed (see tesseract --list-langs for "
|
||||
"all language packs installed in your system). Use -l eng+deu for "
|
||||
"multiple languages.",
|
||||
|
||||
@@ -181,7 +181,7 @@ def test_language_warning(caplog):
|
||||
'ocrmypdf._validation.locale.getlocale', return_value=('en_US', 'UTF-8')
|
||||
):
|
||||
vd.check_options_languages(opts, plugin_manager)
|
||||
assert opts.language == ['eng']
|
||||
assert opts.languages == {'eng'}
|
||||
assert '' in caplog.text
|
||||
|
||||
opts = make_opts(language=None)
|
||||
@@ -189,7 +189,7 @@ def test_language_warning(caplog):
|
||||
'ocrmypdf._validation.locale.getlocale', return_value=('fr_FR', 'UTF-8')
|
||||
):
|
||||
vd.check_options_languages(opts, plugin_manager)
|
||||
assert opts.language == ['eng']
|
||||
assert opts.languages == {'eng'}
|
||||
assert 'assuming --language' in caplog.text
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user