# 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
#
# @author Philippe Elie


def query_params(environ):
    field = cgi.FieldStorage(fp = environ['wsgi.input'], environ = environ)
    rdict = {
        'format' : 'html',
        'cmd' : 'status',
        'title' : '',
        'lang' : ''
        }
    for name in field:
        if type(field[name]) == types.ListType:
            rdict[name] = field[name][-1].value
        else:
            rdict[name] = field[name].value

    for key in rdict:
        rdict[key] = unicode(rdict[key], 'utf-8')

    return rdict

def return_response(start_response, obj, to_json, ret_code, mime_type):
    if to_json:
        try:
            text = json.dumps(obj)
        except UnicodeDecodeError:
            print >> sys.stderr, obj
            ret_code = '400 Bad 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: 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

#
# @author Philippe Elie


def query_params(environ):
    field = cgi.FieldStorage(fp = environ['wsgi.input'], environ = environ)
    rdict = {
        'format' : 'html',
        'cmd' : 'status',
        'title' : '',
        'lang' : ''
        }
    for name in field:
        if type(field[name]) == types.ListType:
            rdict[name] = field[name][-1].value
        else:
            rdict[name] = field[name].value

    for key in rdict:
        rdict[key] = unicode(rdict[key], 'utf-8')

    return rdict

def return_response(start_response, obj, to_json, ret_code, mime_type):
    if to_json:
        try:
            text = json.dumps(obj)
        except UnicodeDecodeError:
            print >> sys.stderr, obj
            ret_code = '400 Bad Request'

            text = json.dumps({ 'error' : 1, 'text' : ret_code })