# Path: memsql_loader/util/apsw_helpers.py
# class _NoDefault(object):
# class _RowBase(object):
# class Row(_RowBase):
# class SelectResult(list):
#     def __init__(self, fields, values):
#     def get(self, name, default=_NoDefault):
#     def set(self, name, value):
#     def __getattr__(self, name):
#     def __setattr__(self, name, value):
#     def __getitem__(self, name):
#     def __setitem__(self, name, value):
#     def __contains__(self, name):
#     def __iter__(self):
#     def __len__(self):
#     def keys(self):
#     def values(self):
#     def items(self):
#     def __eq__(self, other):
#     def __ne__(self, other):
#     def as_dict(self):
#     def for_json(self):
#     def nope(self, *args, **kwargs):
#     def __init__(self, fields, rows, is_rows=False, RowClass=Row):
#     def width(self):
#     def __getitem__(self, i):
# def query(cursor, query, *params, **kwparams):
# def get(cursor, query, *params, **kwparams):
# 
# 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/apsw_sql_step_queue/errors.py
# class TaskDoesNotExist(APSWSQLStepQueueException):
#     pass
# 
# class StepAlreadyStarted(APSWSQLStepQueueException):
#     pass
# 
# class StepNotStarted(APSWSQLStepQueueException):
#     pass
# 
# class StepAlreadyFinished(APSWSQLStepQueueException):
#     pass
# 
# class StepRunning(APSWSQLStepQueueException):
#     pass
# 
# class AlreadyFinished(APSWSQLStepQueueException):
#     pass
# 
# Path: memsql_loader/util/apsw_sql_step_queue/time_helpers.py
# def unix_timestamp(dt):
#     return int(time.mktime(dt.timetuple()))


import copy
from datetime import datetime
from dateutil import parser
from contextlib import contextmanager
from memsql_loader.util import apsw_helpers, super_json as json
from memsql_loader.util.apsw_sql_step_queue.errors import TaskDoesNotExist, StepAlreadyStarted, StepNotStarted, StepAlreadyFinished, StepRunning, AlreadyFinished
from memsql_loader.util.apsw_sql_step_queue.time_helpers import unix_timestamp

        # NOTE: These fields are specific to the memsql-loader use case;
        # they are not necessary for the queue functionality.
        self.job_id = None
        self.file_id = None
        self.md5 = None
        self.bytes_total = None
        self.bytes_downloaded = None
        self.download_rate = None

        self.steps = None

        self._refresh()

    ###############################
    # Public Interface

    def valid(self):
        """ Check to see if we are still active. """
        if self.finished is not None:
            return False

        with self.storage.cursor() as cursor:
            row = apsw_helpers.get(cursor, '''
                SELECT (last_contact > datetime(:now, 'unixepoch', '-%s second')) AS valid
                FROM %s
                WHERE
                    id = :task_id
                    AND execution_id = :execution_id
            ''' % (self._queue.execution_ttl, self._queue.table_name),

# Path: memsql_loader/util/apsw_helpers.py
# class _NoDefault(object):
# class _RowBase(object):
# class Row(_RowBase):
# class SelectResult(list):
#     def __init__(self, fields, values):
#     def get(self, name, default=_NoDefault):
#     def set(self, name, value):
#     def __getattr__(self, name):
#     def __setattr__(self, name, value):
#     def __getitem__(self, name):
#     def __setitem__(self, name, value):
#     def __contains__(self, name):
#     def __iter__(self):
#     def __len__(self):
#     def keys(self):
#     def values(self):
#     def items(self):
#     def __eq__(self, other):
#     def __ne__(self, other):
#     def as_dict(self):
#     def for_json(self):
#     def nope(self, *args, **kwargs):
#     def __init__(self, fields, rows, is_rows=False, RowClass=Row):
#     def width(self):
#     def __getitem__(self, i):
# def query(cursor, query, *params, **kwparams):
# def get(cursor, query, *params, **kwparams):
# 
# 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/apsw_sql_step_queue/errors.py
# class TaskDoesNotExist(APSWSQLStepQueueException):
#     pass
# 
# class StepAlreadyStarted(APSWSQLStepQueueException):
#     pass
# 
# class StepNotStarted(APSWSQLStepQueueException):
#     pass
# 
# class StepAlreadyFinished(APSWSQLStepQueueException):
#     pass
# 
# class StepRunning(APSWSQLStepQueueException):
#     pass
# 
# class AlreadyFinished(APSWSQLStepQueueException):
#     pass
# 
# Path: memsql_loader/util/apsw_sql_step_queue/time_helpers.py
# def unix_timestamp(dt):
#     return int(time.mktime(dt.timetuple()))


# Path: memsql_loader/util/apsw_sql_step_queue/task_handler.py
import copy
from datetime import datetime
from dateutil import parser
from contextlib import contextmanager
from memsql_loader.util import apsw_helpers, super_json as json
from memsql_loader.util.apsw_sql_step_queue.errors import TaskDoesNotExist, StepAlreadyStarted, StepNotStarted, StepAlreadyFinished, StepRunning, AlreadyFinished
from memsql_loader.util.apsw_sql_step_queue.time_helpers import unix_timestamp


        # NOTE: These fields are specific to the memsql-loader use case;
        # they are not necessary for the queue functionality.
        self.job_id = None
        self.file_id = None
        self.md5 = None
        self.bytes_total = None
        self.bytes_downloaded = None
        self.download_rate = None

        self.steps = None

        self._refresh()

    ###############################
    # Public Interface

    def valid(self):
        """ Check to see if we are still active. """
        if self.finished is not None:
            return False

        with self.storage.cursor() as cursor:
            row = apsw_helpers.get(cursor, '''
                SELECT (last_contact > datetime(:now, 'unixepoch', '-%s second')) AS valid
                FROM %s
                WHERE
                    id = :task_id
                    AND execution_id = :execution_id
            ''' % (self._queue.execution_ttl, self._queue.table_name),

                now=unix_timestamp(datetime.utcnow()),