# 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
    db_obj = sge_jobs.DbJob()

    text = common_html.get_head('hocr', css = 'shared.css').encode('utf-8') + '\n  <body>\n'

    html, jobs = job_table(db_obj, state_filter, limit, offset,
                           default_limit, max_limit, cmd_filter)
    text += html

    text += accounting_table(db_obj, jobs, state_filter, limit, offset,
                             default_limit, max_limit)

    text += '  </body>\n</html>'

    start_response('200 OK', [('Content-Type',
                               'text/html; charset=UTF-8'),
                              ('Content-Length', str(len(text))),
                              ('Access-Control-Allow-Origin', '*')])
    return [ text ]

def gen_hocr_request(params):
    job_req = {
        'jobname' : 'hocr',
        'run_cmd' : 'python',
        'args' : [
            os.path.expanduser('~/phe/hocr/hocr.py'),
            '-lang:' + params['lang'],
            '-book:' + params['book']
            ],
        'max_vmem' : 1024,
        }

# 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

    db_obj = sge_jobs.DbJob()

    text = common_html.get_head('hocr', css = 'shared.css').encode('utf-8') + '\n  <body>\n'

    html, jobs = job_table(db_obj, state_filter, limit, offset,
                           default_limit, max_limit, cmd_filter)
    text += html

    text += accounting_table(db_obj, jobs, state_filter, limit, offset,
                             default_limit, max_limit)

    text += '  </body>\n</html>'

    start_response('200 OK', [('Content-Type',
                               'text/html; charset=UTF-8'),
                              ('Content-Length', str(len(text))),
                              ('Access-Control-Allow-Origin', '*')])
    return [ text ]

def gen_hocr_request(params):
    job_req = {
        'jobname' : 'hocr',
        'run_cmd' : 'python',
        'args' : [
            os.path.expanduser('~/phe/hocr/hocr.py'),
            '-lang:' + params['lang'],
            '-book:' + params['book']
            ],
        'max_vmem' : 1024,
        }

    db_obj = sge_jobs.DbJob()