File: gui_loop.py

package info (click to toggle)
pyxrd 0.8.4-5
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 7,644 kB
  • sloc: python: 26,501; sh: 301; makefile: 128
file content (98 lines) | stat: -rw-r--r-- 3,119 bytes parent folder | download | duplicates (2)
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
__add_idle_call = None
__remove_idle_call = None
__add_timeout_call = None
__remove_timeout_call = None
__setup_event_loop = None
__start_event_loop = None
__stop_event_loop = None

__idle_call_dict = {}
__timeout_call_dict = {}

def add_idle_call(func, *args, **kwargs):
    global __add_idle_call
    global __idle_call_dict
    if __add_idle_call is not None:
        __idle_call_dict[func] = __add_idle_call(func, *args, **kwargs)
    else: # toolkit does not support this or is not loaded:
        func(*args, **kwargs)
        
def remove_idle_call(func):
    global __remove_idle_call
    global __idle_call_dict
    if __remove_idle_call is not None:
        __remove_idle_call(__idle_call_dict[func])
        
def add_timeout_call(timeout, func, *args, **kwargs):
    global __add_timeout_call
    global __timeout_call_dict
    if __add_timeout_call is not None:
        __timeout_call_dict[func] = __add_timeout_call(timeout, func, *args, **kwargs)
    else: # toolkit does not support this or is not loaded:
        func(*args, **kwargs)

def remove_timeout_call(func):
    global __remove_timeout_call
    global __timeout_call_dict
    if __remove_timeout_call is not None:
        __remove_timeout_call(__timeout_call_dict[func])

def start_event_loop():
    global __start_event_loop
    if __start_event_loop is not None:
        return __start_event_loop()

def stop_event_loop():
    global __stop_event_loop
    if __stop_event_loop is not None:
        return __stop_event_loop()    


def load_toolkit_functions(
        add_idle_call, 
        remove_idle_call,
        add_timeout_call,
        remove_timeout_call,
        start_event_loop,
        stop_event_loop):
    """
        'add_idle_call' should take a function as 1st argument, the return 
        value is passed back to 'remove_idle_call'. Internally a cache is maintained
        in which keys are functions and values are return values.
        'add_timeout_call' and 'remove_timeout_call' work analogously
        start_event_loop and stop_event_loop don't take arguments and should be self-explanatory.
    """
    global __add_idle_call
    global __remove_idle_call
    global __add_timeout_call
    global __remove_timeout_call
    global __start_event_loop
    global __stop_event_loop
    assert callable(add_idle_call)
    assert callable(remove_idle_call)
    assert callable(add_timeout_call)
    assert callable(remove_timeout_call)
    assert callable(start_event_loop)
    assert callable(stop_event_loop)
    __add_idle_call = add_idle_call
    __remove_idle_call = remove_idle_call
    __add_timeout_call = add_timeout_call
    __remove_timeout_call = remove_timeout_call
    __start_event_loop = start_event_loop
    __stop_event_loop = stop_event_loop

"""
    Decorators:
"""

def run_when_idle(func):
    def callback(*args, **kwargs):
        return add_idle_call(func, *args, **kwargs)
    return callback
    
def run_every(timeout):
    def wrapper(func):
        def callback(*args, **kwargs):
            return add_timeout_call(func, timeout, *args, **kwargs)
        return callback
    return wrapper