from datetime import datetime, timedelta
from inspect import isgenerator

try:
    from collections import OrderedDict
except ImportError:  # pragma: no cover
    from ordereddict import OrderedDict

from sqlalchemy import Integer, UnicodeText, Float, DateTime, Boolean, types, Table, event


def guess_type(sample):
    if isinstance(sample, bool):
        return Boolean
    elif isinstance(sample, int):
        return Integer
    elif isinstance(sample, float):
        return Float
    elif isinstance(sample, datetime):
        return DateTime
    return UnicodeText


def convert_row(row):
    if row is None:
        return None
    return OrderedDict(row.items())


class ResultIter(object):
    """ SQLAlchemy ResultProxies are not iterable to get a
    list of dictionaries. This is to wrap them. """

    def __init__(self, result_proxies):
        if not isgenerator(result_proxies):
            result_proxies = iter((result_proxies, ))
        self.result_proxies = result_proxies
        self.count = 0
        self.rp = None

    def _next_rp(self):
        try:
            self.rp = next(self.result_proxies)
            self.count += self.rp.rowcount
            self.keys = list(self.rp.keys())
            return True
        except StopIteration:
            return False

    def __next__(self):
        if self.rp is None:
            if not self._next_rp():
                raise StopIteration
        row = self.rp.fetchone()
        if row is None:
            if self._next_rp():
                return next(self)
            else:
                # stop here
                raise StopIteration
        return convert_row(row)

    next = __next__

    def __iter__(self):
        return self


# def sqlite_datetime_fix():
#     class SQLiteDateTimeType(types.TypeDecorator):
#         impl = types.Integer
#         epoch = datetime(1970, 1, 1, 0, 0, 0)

#         def process_bind_param(self, value, dialect):
#             if value is None:
#                 return None
#             if isinstance(value, datetime):
#                 return value
#             return (value / 1000 - self.epoch).total_seconds()

#         def process_result_value(self, value, dialect):
#             if value is None:
#                 return None
#             if isinstance(value, int):
#                 return self.epoch + timedelta(seconds=value / 1000)
#             return value

#     def is_sqlite(inspector):
#         return inspector.engine.dialect.name == "sqlite"

#     def is_datetime(column_info):
#         return isinstance(column_info['type'], types.DateTime)

#     @event.listens_for(Table, "column_reflect")
#     def setup_epoch(inspector, table, column_info):
#         if is_sqlite(inspector) and is_datetime(column_info):
#             column_info['type'] = SQLiteDateTimeType()

@@ -1720,1107 +1720,4 @@
 elf%0A
-%0A%0A# def sqlite_datetime_fix():%0A#     class SQLiteDateTimeType(types.TypeDecorator):%0A#         impl = types.Integer%0A#         epoch = datetime(1970, 1, 1, 0, 0, 0)%0A%0A#         def process_bind_param(self, value, dialect):%0A#             if value is None:%0A#                 return None%0A#             if isinstance(value, datetime):%0A#                 return value%0A#             return (value / 1000 - self.epoch).total_seconds()%0A%0A#         def process_result_value(self, value, dialect):%0A#             if value is None:%0A#                 return None%0A#             if isinstance(value, int):%0A#                 return self.epoch + timedelta(seconds=value / 1000)%0A#             return value%0A%0A#     def is_sqlite(inspector):%0A#         return inspector.engine.dialect.name == %22sqlite%22%0A%0A#     def is_datetime(column_info):%0A#         return isinstance(column_info%5B'type'%5D, types.DateTime)%0A%0A#     @event.listens_for(Table, %22column_reflect%22)%0A#     def setup_epoch(inspector, table, column_info):%0A#         if is_sqlite(inspector) and is_datetime(column_info):%0A#             column_info%5B'type'%5D = SQLiteDateTimeType()%0A

Remove datetime fix code.