# coding=utf8

import pprint
import six


class MyPrettyPrinter(pprint.PrettyPrinter):
    def format(self, object, context, maxlevels, level):
        if six.PY2:
            if isinstance(object, unicode):
                return (object.encode("utf8"), True, False)
        return pprint.PrettyPrinter.format(self, object, context, maxlevels, level)


def remove_space(s):
    return s.replace(u"　", "").strip()


def minguo2ce(minguo):
    pattern = "(\d+)\.([ 0-9]{1,2})\.([ 0-9]{1,2})"
    s = re.search(pattern, minguo)
    if s:
        yy = int(s.group(1)) + 1911

        try:
            mm = int(s.group(2))
        except:
            mm = 1

        try:
            dd = int(s.group(3))
        except:
            dd = 1

        return date(yy, mm, dd)

    pattern = "(\d+)/([ 0-9]{1,2})/([ 0-9]{1,2})"
    s = re.search(pattern, minguo)
    if s:
        yy = int(s.group(1)) + 1911

        try:
            mm = int(s.group(2))
        except:
            mm = 1

        try:
            dd = int(s.group(3))
        except:
            dd = 1

        return date(yy, mm, dd)

    return


def xstr(s):
    return "" if s is None else s


def jprint(a):
    import json

    print(json.dumps(a, ensure_ascii=False, indent=4, sort_keys=True))


def PrintException():
    exc_type, exc_obj, tb = sys.exc_info()
    f = tb.tb_frame
    lineno = tb.tb_lineno
    filename = f.f_code.co_filename
    linecache.checkcache(filename)
    line = linecache.getline(filename, lineno, f.f_globals)
    print(
        'EXCEPTION IN ({}, LINE {} "{}"): {}'.format(
            filename, lineno, line.strip(), exc_obj
        )
    )
