# 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/common_html.py
# def get_head(title, css = None, html5 = False):
# 
# 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 os
import sys
import json
import time
import types
import hocr
    import cgi
from jobs import sge_jobs
from common import common_html
from common import utils
    from flup.server.cgi import WSGIServer

    try:
        text = json.dumps(result)
    except UnicodeDecodeError:
        log(result)
        ret_code = '400 Bad Request'
        text = json.dumps({ 'error' : 1, 'text' : ret_code })

    start_response(ret_code, [('Content-Type',
                               'application/json' + '; charset=UTF-8'),
                              ('Content-Length', str(len(text))),
                              ('Access-Control-Allow-Origin', '*')])
    return [ text ]

def myapp(environ, start_response):
    params = query_params(environ)

    if params['cmd'] == 'ping':
        return handle_ping(start_response)
    elif params['cmd'] == 'hocr':
        return handle_query(params, start_response)
    else:
        return handle_status(params, start_response)

if __name__ == "__main__":
    sys.stderr = open(os.path.expanduser('~/log/hocr_cgi.err'), 'a')

    try:
        WSGIServer(myapp).run()
    except BaseException:

# 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/common_html.py
# def get_head(title, css = None, html5 = False):
# 
# 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: hocr/hocr_cgi.py
import os
import sys
import json
import time
import types
import hocr
    import cgi
from jobs import sge_jobs
from common import common_html
from common import utils
    from flup.server.cgi import WSGIServer


    try:
        text = json.dumps(result)
    except UnicodeDecodeError:
        log(result)
        ret_code = '400 Bad Request'
        text = json.dumps({ 'error' : 1, 'text' : ret_code })

    start_response(ret_code, [('Content-Type',
                               'application/json' + '; charset=UTF-8'),
                              ('Content-Length', str(len(text))),
                              ('Access-Control-Allow-Origin', '*')])
    return [ text ]

def myapp(environ, start_response):
    params = query_params(environ)

    if params['cmd'] == 'ping':
        return handle_ping(start_response)
    elif params['cmd'] == 'hocr':
        return handle_query(params, start_response)
    else:
        return handle_status(params, start_response)

if __name__ == "__main__":
    sys.stderr = open(os.path.expanduser('~/log/hocr_cgi.err'), 'a')

    try:
        WSGIServer(myapp).run()
    except BaseException:

        utils.print_traceback()