# Path: common/utils.py
# def read_file(filename):
# def write_file(filename, text):
# def save_obj(filename, data):
# def load_obj(filename):
# def sha1(filename):
# def write_sha1(sha1, filename):
# def url_opener():
# def copy_file_from_url(url, out_file, expect_sha1 = None, max_retry = 4):
# def compress_file_data(out_filename, data, compress_type):
# def compress_file(out_filename, in_filename, compress_type):
# def uncompress_file(filename, compress_type):
# def _retry_on_eintr(func, *args):
# def safe_read(fd):
# def safe_write(fd, text):
# def print_traceback(*kwargs):
# def readline_backward(filename, buf_size=8192):


import sys
import re
import xml.etree.ElementTree as etree
import subprocess
import os
import resource
from common import utils
def parse_page(page, elem, page_nr):
    parse_page_recursive(page, elem)

def setrlimits():
    mega = 1 << 20
    resource.setrlimit(resource.RLIMIT_AS, (1024*mega, 1024*mega))
    resource.setrlimit(resource.RLIMIT_CORE, (128*mega, 128*mega))
    resource.setrlimit(resource.RLIMIT_CPU, (1*60*60, 1*60*60))

def do_parse(opt, filename):

    try:
        ls = subprocess.Popen([ djvutoxml, filename], stdout=subprocess.PIPE, preexec_fn=setrlimits, close_fds = True)

        page_nr = 1
        for event, elem in etree.iterparse(XmlFile(ls.stdout)):
            if elem.tag.lower() == 'object':
                page = OcrPage()
                if not opt.silent:
                    print >> sys.stderr, page_nr, '\r',
                page.start_page(elem)
                parse_page(page, elem, page_nr)
                page.end_page(elem)

                filename = opt.out_dir + 'page_%04d.hocr' % page_nr

                if opt.compress:
                    text = page.get_hocr_html().encode('utf-8')
                    utils.compress_file_data(filename, text, opt.compress)
                else:

# Path: common/utils.py
# def read_file(filename):
# def write_file(filename, text):
# def save_obj(filename, data):
# def load_obj(filename):
# def sha1(filename):
# def write_sha1(sha1, filename):
# def url_opener():
# def copy_file_from_url(url, out_file, expect_sha1 = None, max_retry = 4):
# def compress_file_data(out_filename, data, compress_type):
# def compress_file(out_filename, in_filename, compress_type):
# def uncompress_file(filename, compress_type):
# def _retry_on_eintr(func, *args):
# def safe_read(fd):
# def safe_write(fd, text):
# def print_traceback(*kwargs):
# def readline_backward(filename, buf_size=8192):


# Path: ocr/djvu_text_to_hocr.py
import sys
import re
import xml.etree.ElementTree as etree
import subprocess
import os
import resource
from common import utils

def parse_page(page, elem, page_nr):
    parse_page_recursive(page, elem)

def setrlimits():
    mega = 1 << 20
    resource.setrlimit(resource.RLIMIT_AS, (1024*mega, 1024*mega))
    resource.setrlimit(resource.RLIMIT_CORE, (128*mega, 128*mega))
    resource.setrlimit(resource.RLIMIT_CPU, (1*60*60, 1*60*60))

def do_parse(opt, filename):

    try:
        ls = subprocess.Popen([ djvutoxml, filename], stdout=subprocess.PIPE, preexec_fn=setrlimits, close_fds = True)

        page_nr = 1
        for event, elem in etree.iterparse(XmlFile(ls.stdout)):
            if elem.tag.lower() == 'object':
                page = OcrPage()
                if not opt.silent:
                    print >> sys.stderr, page_nr, '\r',
                page.start_page(elem)
                parse_page(page, elem, page_nr)
                page.end_page(elem)

                filename = opt.out_dir + 'page_%04d.hocr' % page_nr

                if opt.compress:
                    text = page.get_hocr_html().encode('utf-8')
                    utils.compress_file_data(filename, text, opt.compress)
                else:

                    utils.write_file(filename, text)