Completed
Push — master ( 8b993b...d268b8 )
by Olivier
01:01
created

trycatchslot()   B

Complexity

Conditions 5

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
c 1
b 0
f 0
dl 0
loc 23
rs 8.2508
1
2
import inspect
3
import logging
4
5
6
logger = logging.getLogger(__name__)
7
8
9
def trycatchslot(func):
10
    """
11
    wrap a at slot.
12
    log and call a method called show_error in 
13
    case of error
14
    """
15
    def wrapper(self, *args):
16
        # filter out excess args as qt signals do
17
        sig = inspect.signature(func)
18
        args = args[:(len(sig.parameters)-1)]
19
        result = None
20
        try:
21
            result = func(self, *args)
22
        except Exception as ex:
23
            logger.exception(ex)
24
            if hasattr(self, "show_error"):
25
                self.show_error(ex)
26
            elif hasattr(self, "error"):
27
                self.error.emit(ex)
28
            else:
29
                logger.warning("Error class % has no member show_error or error", self)
30
        return result
31
    return wrapper
32
33
34