import logging
import threading
import time

from pkg_resources import run_script

import spr.runspr as runspr

logger = logging.getLogger("spr.thread")


class RunSprThread(threading.Thread):
    def __init__(self, group=None, target=None, name=None,
                 args=(), kwargs=None, *, daemon=None):
        super(RunSprThread, self).__init__(group=group,
                                           target=target,
                                           name=name,
                                           daemon=daemon)
        self._stop_event = threading.Event()
        self.args = args
        self.kwargs = kwargs
        return

    def run(self):

        t = time.time()
        runspr.main(*self.args)
        elapsed_time = time.time() - t
        et = time.strftime("%H:%M:%S", time.gmtime(elapsed_time))
        logger.info('Tiempo de ejecución %s' % et)

    def stop(self):
        self._stop_event.set()