import logging
import os
import re

import subprocess
from concurrent import futures

from spreads.plugin import HookPlugin

logger = logging.getLogger('spreadsplug.tesseract')


class TesseractPlugin(HookPlugin):
    @classmethod
    def add_arguments(cls, command, parser):
        if command == 'postprocess':
            parser.add_argument("--language", "-l",
                                dest="language", action="store_true",
                                help="OCR language (3-letter language code")

    def __init__(self, config):
        self.config = config['postprocess']

    def process(self, path):
        logger.info("Performing OCR")
        img_dir = os.path.join(path, 'done')
        with futures.ProcessPoolExecutor() as executor:
            for img in os.listdir(img_dir):
                if not img.endswith('tif'):
                    continue
                executor.submit(
                    subprocess.check_call,
                    ["tesseract", os.path.join(img_dir, img),
                     os.path.join(img_dir, os.path.splitext(img)[0]),
                     "-l", "fra", "hocr"],
                    stderr=subprocess.STDOUT
                )
        # NOTE: This modifies the hOCR files to make them compatible with
        #       pdfbeads.
        #       See the following bugreport for more information:
        #       http://rubyforge.org/tracker/index.php?func=detail&aid=29737&group_id=9752&atid=37737
        for hocr in os.listdir(img_dir):
            if not hocr.endswith('html'):
                continue
            fname = os.path.join(img_dir, hocr)
            with open(fname, 'r') as fp:
                new_content = re.sub(r'(<span[^>]*>(<strong>)? +(<\/strong>)?'
                                     r'<\/span>*)(<span[^>]*>(<strong>)? '
                                     r'+(<\/strong>)?<\/span> *)',
                                     r'\g<1>', fp.read())
            with open(fname, 'w') as fp:
                fp.write(new_content)

@@ -28,17 +28,16 @@
 port re%0A
-%0A
 import s
@@ -46,16 +46,53 @@
 process%0A
+import xml.etree.cElementTree as ET%0A%0A
 from con
@@ -2041,8 +2041,1427 @@
 ontent)%0A
+%0A    def output(self, path):%0A        outfile = os.path.join(path, 'out', %22%7B0%7D.hocr%22%0A                                            .format(os.path.basename(path)))%0A        path = os.path.join(path, 'done')%0A        out_root = ET.Element('html')%0A        ET.SubElement(out_root, 'head')%0A        body = ET.SubElement(out_root, 'body')%0A%0A        for idx, page in enumerate(sorted(%5Bos.path.join(path, x)%0A                                   for x in os.listdir(path)%0A                                   if x.endswith('.html')%5D)):%0A            # NOTE: Fixe some things from hOCR output so we can parse it...%0A            with open(page, 'r+') as fp:%0A                content = re.sub(r'%3Cem%3E%3C%5C/em%3E', '', fp.read())%0A                content = re.sub(r'%3Cstrong%3E%3C%5C/strong%3E', '', content)%0A            page = ET.fromstring(content)%0A            page_elem = page.find(%22xhtml:body/xhtml:div%5B@class='ocr_page'%5D%22,%0A                                  dict(xhtml=%22http://www.w3.org/1999/xhtml%22))%0A            # Correct page_number%0A            page_elem.set('id', 'page_%7B0%7D'.format(idx))%0A%0A            # And tack it onto the output file%0A            if page_elem:%0A                body.append(page_elem)%0A        with open(outfile, 'w') as fp:%0A            # Strip those annoying namespace tags...%0A            out_string = re.sub(r%22(%3C%5C/*)html:%22, %22%5Cg%3C1%3E%22,%0A                                ET.tostring(out_root))%0A            fp.write(out_string.encode(%22UTF-8%22))%0A

Combine hOCR files into a single file during output stage