# 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
                task_id=self.task_id)

    def start_step(self, step_name):
        """ Start a step. """
        if self.finished is not None:
            raise AlreadyFinished()

        step_data = self._get_step(step_name)
        if step_data is not None:
            if 'stop' in step_data:
                raise StepAlreadyFinished()
            else:
                raise StepAlreadyStarted()

        steps = copy.deepcopy(self.steps)
        steps.append({
            "start": datetime.utcnow(),
            "name": step_name
        })
        self._save(steps=steps)

    def stop_step(self, step_name):
        """ Stop a step. """
        if self.finished is not None:
            raise AlreadyFinished()

        steps = copy.deepcopy(self.steps)

        step_data = self._get_step(step_name, steps=steps)
        if step_data is None:

# 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

                task_id=self.task_id)

    def start_step(self, step_name):
        """ Start a step. """
        if self.finished is not None:
            raise AlreadyFinished()

        step_data = self._get_step(step_name)
        if step_data is not None:
            if 'stop' in step_data:
                raise StepAlreadyFinished()
            else:
                raise StepAlreadyStarted()

        steps = copy.deepcopy(self.steps)
        steps.append({
            "start": datetime.utcnow(),
            "name": step_name
        })
        self._save(steps=steps)

    def stop_step(self, step_name):
        """ Stop a step. """
        if self.finished is not None:
            raise AlreadyFinished()

        steps = copy.deepcopy(self.steps)

        step_data = self._get_step(step_name, steps=steps)
        if step_data is None:

            raise StepNotStarted()