# 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 modernization
import types
import json
    import cgi
from common import common_html
from common import utils
    from flup.server.cgi import WSGIServer
    return return_response(start_response, result, True, ret_code, 'application/json')

def handle_blacklist_query(params, start_response):
    if params['lang'] and params['blacklist']:
        try:
            modernize = modernization.Modernization(params['lang'])
            blacklist = json.loads(params['blacklist'])
            modernize.save_blacklist(blacklist)
            ret_code = '200 OK'
            result = { 'error' : 0, 'text' :'OK' }
        except:
            utils.print_traceback()
            ret_code = '500 Internal Server Error'
            result = { 'error' : 1, 'text' : ret_code }
    else:
        ret_code = '400 Bad Request'
        result = { 'error' : 1, 'text' : ret_code }

    return return_response(start_response, result, True, ret_code, 'application/json')

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

    print >> sys.stderr, params

    if params['cmd'] == 'ping':
        return handle_ping(start_response)
    elif params['cmd'] == 'suggest':
        return handle_suggest_query(params, start_response)
    elif params['cmd'] == 'blacklist':

# 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: modernization/modernization_cgi.py
import os
import sys
import modernization
import types
import json
    import cgi
from common import common_html
from common import utils
    from flup.server.cgi import WSGIServer

    return return_response(start_response, result, True, ret_code, 'application/json')

def handle_blacklist_query(params, start_response):
    if params['lang'] and params['blacklist']:
        try:
            modernize = modernization.Modernization(params['lang'])
            blacklist = json.loads(params['blacklist'])
            modernize.save_blacklist(blacklist)
            ret_code = '200 OK'
            result = { 'error' : 0, 'text' :'OK' }
        except:
            utils.print_traceback()
            ret_code = '500 Internal Server Error'
            result = { 'error' : 1, 'text' : ret_code }
    else:
        ret_code = '400 Bad Request'
        result = { 'error' : 1, 'text' : ret_code }

    return return_response(start_response, result, True, ret_code, 'application/json')

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

    print >> sys.stderr, params

    if params['cmd'] == 'ping':
        return handle_ping(start_response)
    elif params['cmd'] == 'suggest':
        return handle_suggest_query(params, start_response)
    elif params['cmd'] == 'blacklist':

        return handle_blacklist_query(params, start_response)