summaryrefslogtreecommitdiff
path: root/src/decorators.py
blob: 3cb967b36002e770b7b4c054c3fae8c01b55222d (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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
"""
Module containing various decorators
"""

from functools import partial

class RefreshWrapper(object):
    def __init__(self):
        self.core = None

    def conditional(self, func):
        """
        Decorator to refresh the UI if the wrapped function
        returns True
        """
        def wrap(*args, **kwargs):
            ret = func(*args, **kwargs)
            if self.core and ret:
                self.core.refresh_window()
            return ret
        return wrap

    def always(self, func):
        """
        Decorator that refreshs the UI no matter what after the function
        """
        def wrap(*args, **kwargs):
            ret = func(*args, **kwargs)
            if self.core:
                self.core.refresh_window()
            return ret
        return wrap

    def update(self, funct):
        """
        Decorator that only updates the screen
        """
        def wrap(*args, **kwargs):
            ret = func(*args, **kwargs)
            if self.core:
                self.core.doupdate()
            return ret
        return wrap

def __completion(quoted, func):
    class Completion(object):
        quoted = quoted
        def __new__(cls, *args, **kwargs):
            return func(*args, **kwargs)
    return Completion


completion_quotes = partial(__completion, True)
completion_raw = partial(__completion, False)
refresh_wrapper = RefreshWrapper()