# Path: memsql_loader/api/shared.py
# class SortDirection(SuperEnum):
#     DESC = SuperEnum.E
#     ASC = SuperEnum.E
# 
# Path: memsql_loader/util/super_json.py
# def simplejson_datetime_serializer(obj):
# def _set_defaults(kwargs):
# def dumps(data, **kwargs):
# def loads(data, **kwargs):
# def safe_loads(data, default, **kwargs):
# def pformat(d):


from prettytable import PrettyTable
from clark.super_enum import SuperEnum
from memsql_loader.api.shared import SortDirection
from memsql_loader.util import super_json as json
    TABLE = SuperEnum.E

# Wrapper around PrettyTable and json.dump
class PrettyPrinter(object):
    # Assumes that the format of the data is a list of dictionaries
    # similar to AttrDict's
    def __init__(self, data, columns=None, align={}, sort_by=None, sort_dir=SortDirection.DESC, format=TableFormat.TABLE):
        if columns is not None:
            # Filter fields of rows in data by columns
            self.data = [ { key: row[key] for key in columns } for row in data ]
            self.columns = columns
        elif columns is None and len(data) > 0:
            self.data = data
            self.columns = data[0].keys()
        else:
            self.data = []
            self.columns = []

        self.align = align
        self.sort_by = sort_by
        self.reverse_sort = sort_dir == SortDirection.DESC
        self.tablefmt = format

    def format(self):
        if self.tablefmt == TableFormat.JSON:
            # TODO(cary) Patch clark.super_enum to support JSON serialization
            printable_data = [
                { k: str(v) if isinstance(v, SuperEnum.Element) else v for k, v in row.iteritems() }
                for row in self.data
            ]

# Path: memsql_loader/api/shared.py
# class SortDirection(SuperEnum):
#     DESC = SuperEnum.E
#     ASC = SuperEnum.E
# 
# Path: memsql_loader/util/super_json.py
# def simplejson_datetime_serializer(obj):
# def _set_defaults(kwargs):
# def dumps(data, **kwargs):
# def loads(data, **kwargs):
# def safe_loads(data, default, **kwargs):
# def pformat(d):


# Path: memsql_loader/util/pretty_printer.py
from prettytable import PrettyTable
from clark.super_enum import SuperEnum
from memsql_loader.api.shared import SortDirection
from memsql_loader.util import super_json as json

    TABLE = SuperEnum.E

# Wrapper around PrettyTable and json.dump
class PrettyPrinter(object):
    # Assumes that the format of the data is a list of dictionaries
    # similar to AttrDict's
    def __init__(self, data, columns=None, align={}, sort_by=None, sort_dir=SortDirection.DESC, format=TableFormat.TABLE):
        if columns is not None:
            # Filter fields of rows in data by columns
            self.data = [ { key: row[key] for key in columns } for row in data ]
            self.columns = columns
        elif columns is None and len(data) > 0:
            self.data = data
            self.columns = data[0].keys()
        else:
            self.data = []
            self.columns = []

        self.align = align
        self.sort_by = sort_by
        self.reverse_sort = sort_dir == SortDirection.DESC
        self.tablefmt = format

    def format(self):
        if self.tablefmt == TableFormat.JSON:
            # TODO(cary) Patch clark.super_enum to support JSON serialization
            printable_data = [
                { k: str(v) if isinstance(v, SuperEnum.Element) else v for k, v in row.iteritems() }
                for row in self.data
            ]

            return json.dumps(printable_data, sort_keys=True, indent=4 * ' ').encode('utf-8')