aboutsummaryrefslogtreecommitdiff
path: root/parrot_zik/utils.py
blob: 86f52cdf0a9b7f9d64c4d02934f0a9b7a6ca022d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import functools
from threading import Lock

import gtk


class repeat(object):
    def __init__(self, f):
        self.f = f
        self.id = None
        self.lock = Lock()

    def __call__(self, cls):
        self.f(cls)

    def start(self, cls, frequency):
        self.lock.acquire()
        if not self.id:
            @functools.wraps(self.f)
            def run():
                self.f(cls)
                return True
            self.id = gtk.timeout_add(frequency, run)
        self.lock.release()

    def stop(self):
        self.lock.acquire()
        if self.id:
            gtk.timeout_remove(self.id)
            self.id = None
        self.lock.release()