trycatchslot()   B
last analyzed

Complexity

Conditions 5

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 23
rs 8.2508
cc 5
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 or a signal
13
    called error in 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