Get rid of subprocess call on import of tesseract, unpaper -- bit nasty

This commit is contained in:
James R. Barlow
2015-07-28 01:00:29 -07:00
parent 8508141314
commit 6064160953
3 changed files with 23 additions and 31 deletions
+4 -4
View File
@@ -54,11 +54,11 @@ EXIT_OTHER_ERROR = 15
MINIMUM_TESS_VERSION = '3.02.02'
if tesseract.VERSION < MINIMUM_TESS_VERSION:
if tesseract.version() < MINIMUM_TESS_VERSION:
print(
"Please install tesseract {0} or newer "
"(currently installed version is {1})".format(
MINIMUM_TESS_VERSION, tesseract.VERSION),
MINIMUM_TESS_VERSION, tesseract.version()),
file=sys.stderr)
sys.exit(EXIT_MISSING_DEPENDENCY)
@@ -177,12 +177,12 @@ if not options.language:
if '+' in options.language[0]:
options.language = options.language[0].split('+')
if not set(options.language).issubset(tesseract.LANGUAGES):
if not set(options.language).issubset(tesseract.languages()):
print(
"The installed version of tesseract does not have language "
"data for the following requested languages: ",
file=sys.stderr)
for lang in (set(options.language) - tesseract.LANGUAGES):
for lang in (set(options.language) - tesseract.languages()):
print(lang, file=sys.stderr)
sys.exit(EXIT_BAD_ARGS)
+16 -18
View File
@@ -1,43 +1,41 @@
#!/usr/bin/env python3
from subprocess import Popen, PIPE, CalledProcessError
from subprocess import STDOUT, CalledProcessError, check_output
import sys
import os
import re
from functools import lru_cache
def _version():
@lru_cache(maxsize=1)
def version():
args_tess = [
'tesseract',
'--version'
]
p_tess = Popen(args_tess, close_fds=True, universal_newlines=True,
stdout=PIPE, stderr=PIPE)
_, versions = p_tess.communicate(timeout=5)
try:
versions = check_output(
args_tess, close_fds=True, universal_newlines=True,
stderr=STDOUT)
except CalledProcessError:
print("Could not find Tesseract executable on system PATH.")
sys.exit(1)
tesseract_version = re.match(r'tesseract\s(.+)', versions).group(1)
return tesseract_version
def _languages():
@lru_cache(maxsize=1)
def languages():
args_tess = [
'tesseract',
'--list-langs'
]
p_tess = Popen(args_tess, close_fds=True, universal_newlines=True,
stdout=PIPE, stderr=PIPE)
_, langs = p_tess.communicate(timeout=5)
langs = check_output(
args_tess, close_fds=True, universal_newlines=True,
stderr=STDOUT)
return set(lang.strip() for lang in langs.splitlines()[1:])
try:
VERSION = _version()
LANGUAGES = _languages()
except Exception as e:
print(e)
print("Could not find tesseract executable", file=sys.stderr)
sys.exit(1)
HOCR_TEMPLATE = '''<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
+3 -9
View File
@@ -6,9 +6,11 @@ from subprocess import Popen, PIPE
from tempfile import NamedTemporaryFile
import sys
import os
from functools import lru_cache
def _version():
@lru_cache(maxsize=1)
def version():
args_unpaper = [
'unpaper',
'--version'
@@ -20,14 +22,6 @@ def _version():
return version.strip()
try:
VERSION = _version()
AVAILABLE = True
except FileNotFoundError:
VERSION = '?'
AVAILABLE = False
raise
try:
from PIL import Image
except ImportError: