Unify the way backends are defined and loaded, unify the fs_cleanup function
This commit is contained in:
@@ -1,28 +1,3 @@
|
||||
from __future__ import absolute_import
|
||||
|
||||
import sys
|
||||
|
||||
from django.utils.importlib import import_module
|
||||
|
||||
from ..conf.settings import BACKEND
|
||||
|
||||
|
||||
class BackendBase(object):
|
||||
def execute(input_filename, language=None):
|
||||
raise NotImplemented
|
||||
|
||||
|
||||
def get_ocr_backend():
|
||||
"""
|
||||
Return the OCR backend using the path specified in the configuration
|
||||
settings
|
||||
"""
|
||||
try:
|
||||
module = import_module(BACKEND)
|
||||
except ImportError:
|
||||
sys.stderr.write(u'\nWarning: No OCR backend named: %s\n\n' % BACKEND)
|
||||
raise
|
||||
else:
|
||||
return module
|
||||
|
||||
ocr_backend = get_ocr_backend()
|
||||
|
||||
@@ -5,6 +5,8 @@ import os
|
||||
import subprocess
|
||||
import tempfile
|
||||
|
||||
from common.utils import fs_cleanup
|
||||
|
||||
from . import BackendBase
|
||||
from ..conf.settings import TESSERACT_PATH
|
||||
from ..exceptions import OCRError
|
||||
@@ -27,8 +29,8 @@ class Tesseract(BackendBase):
|
||||
return_code = proc.wait()
|
||||
if return_code != 0:
|
||||
error_text = proc.stderr.read()
|
||||
cleanup(filepath)
|
||||
cleanup(ocr_output)
|
||||
fs_cleanup(filepath)
|
||||
fs_cleanup(ocr_output)
|
||||
if language:
|
||||
# If tesseract gives an error with a language parameter
|
||||
# re-run it with no parameter again
|
||||
@@ -43,14 +45,3 @@ class Tesseract(BackendBase):
|
||||
os.unlink(filepath)
|
||||
|
||||
return text
|
||||
|
||||
|
||||
# TODO: Reduntant, also in api.py
|
||||
def cleanup(filename):
|
||||
"""
|
||||
Try to remove the given filename, ignoring non-existent files
|
||||
"""
|
||||
try:
|
||||
os.remove(filename)
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
Reference in New Issue
Block a user