#
#   PyGUI - Tasks - Macintosh
#

from Applications import application
from GTasks import Task as GTask

class Task(GTask):

    def __init__(self, proc, interval, repeat = 0, start = 1):
        self._proc = proc
        self._interval = interval
        self._repeat = repeat
        self._idle_task = None
        if start:
            self.start()

    def start(self):
        app = application()
        app.add_idle_task(self, self._interval, '_fire')
    
    def stop(self):
        app = application()
        app.remove_idle_task(self, '_fire')
    
    def _fire(self):
        try:
            self._proc()
        finally:
            if not self._repeat:
                self.stop()
