# Path: jobs/sge_jobs.py
# class DbJob(db.UserDb):
#     def __init__(self):
#     def get_job_table(self, state_filter, limit = 50, offset = 0,
#                       cmd_filter = None):
#     def get_accounting_table(self, limit = 50, offset = 0, job_ids = None):
#     def pending_request(self, limit = 16, offset = 0):
#     def _add_request(self, jobname, run_cmd, args, max_vmem, cpu_bound, force):
#     def add_request(self, jobname, run_cmd, args,  max_vmem,
#                     cpu_bound = True, force = False):
#     def exec_request(self, r):
#     def run_batch(self, nr_running, limit = 16):
#     def _exec_check(self, request):
#     def check_running(self):
#     def search_accounting(self, jobs, last_time_day = 2):
#     def update_accounting(self):
# def quote_arg(arg):
# def job_cmdline_arg(request, cmd):
# def sge_cmdline_arg(request):
# 
# 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 json
import pdf_to_djvu
import sys
import os
    import cgi
from jobs import sge_jobs
from common import utils
    from flup.server.cgi import WSGIServer
def handle_query(params, start_response):
    print >> sys.stderr, params

    answer = '200 OK'

    if not params['ia_id']:
        answer = '400 BAD REQUEST'
        text = json.dumps({
            'error' : 1,
            'text' : 'No ia identifier provided in request',
        })
    else:
        # Check it now to provide directly feedback to user
        ia_files = pdf_to_djvu.get_ia_files(params['ia_id'])
        if not ia_files.get('pdf', None) or not ia_files.get('xml', None):
            answer = '400 BAD REQUEST'
            text = json.dumps(
                { 'error' : 2,
                  'text' : "invalid ia identifier, I can't locate needed files",
              })

    if answer == '200 OK':
        queue_pdf_to_djvu(params['ia_id'])
        text = json.dumps({
            'error' : 0,
            'text' : 'item conversion will start soon',
        })

    start_response(answer, [('Content-Type', 'text/plain; charset=UTF-8'),
                            ('Content-Length', str(len(text))),

# Path: jobs/sge_jobs.py
# class DbJob(db.UserDb):
#     def __init__(self):
#     def get_job_table(self, state_filter, limit = 50, offset = 0,
#                       cmd_filter = None):
#     def get_accounting_table(self, limit = 50, offset = 0, job_ids = None):
#     def pending_request(self, limit = 16, offset = 0):
#     def _add_request(self, jobname, run_cmd, args, max_vmem, cpu_bound, force):
#     def add_request(self, jobname, run_cmd, args,  max_vmem,
#                     cpu_bound = True, force = False):
#     def exec_request(self, r):
#     def run_batch(self, nr_running, limit = 16):
#     def _exec_check(self, request):
#     def check_running(self):
#     def search_accounting(self, jobs, last_time_day = 2):
#     def update_accounting(self):
# def quote_arg(arg):
# def job_cmdline_arg(request, cmd):
# def sge_cmdline_arg(request):
# 
# 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/pdf_to_djvu_cgi.py
import json
import pdf_to_djvu
import sys
import os
    import cgi
from jobs import sge_jobs
from common import utils
    from flup.server.cgi import WSGIServer

def handle_query(params, start_response):
    print >> sys.stderr, params

    answer = '200 OK'

    if not params['ia_id']:
        answer = '400 BAD REQUEST'
        text = json.dumps({
            'error' : 1,
            'text' : 'No ia identifier provided in request',
        })
    else:
        # Check it now to provide directly feedback to user
        ia_files = pdf_to_djvu.get_ia_files(params['ia_id'])
        if not ia_files.get('pdf', None) or not ia_files.get('xml', None):
            answer = '400 BAD REQUEST'
            text = json.dumps(
                { 'error' : 2,
                  'text' : "invalid ia identifier, I can't locate needed files",
              })

    if answer == '200 OK':
        queue_pdf_to_djvu(params['ia_id'])
        text = json.dumps({
            'error' : 0,
            'text' : 'item conversion will start soon',
        })

    start_response(answer, [('Content-Type', 'text/plain; charset=UTF-8'),
                            ('Content-Length', str(len(text))),

                            ('Access-Control-Allow-Origin', '*')])