Passed
Push — develop ( b44848...2798d2 )
by Dean
02:44
created

ErrorHasher.hash()   B

Complexity

Conditions 4

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 14.993

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 23
ccs 2
cts 17
cp 0.1176
rs 8.7972
cc 4
crap 14.993
1 1
from plugin.core.constants import PMS_PATH
2
3 1
import hashlib
4 1
import os
5 1
import traceback
6
7
8 1
class ErrorHasher(object):
9 1
    @staticmethod
10
    def exc_type(type):
0 ignored issues
show
Bug Best Practice introduced by
This seems to re-define the built-in type.

It is generally discouraged to redefine built-ins as this makes code very hard to read.

Loading history...
11
        return type.__name__
12
13 1
    @staticmethod
14
    def exc_message(exception):
15
        return getattr(exception, 'message', None)
16
17 1
    @staticmethod
18
    def exc_traceback(tb):
19
        """Format traceback with relative paths"""
20
        tb_list = traceback.extract_tb(tb)
21
22
        return ''.join(traceback.format_list([
23
            (os.path.relpath(filename, PMS_PATH), line_num, name, line)
24
            for (filename, line_num, name, line) in tb_list
25
        ]))
26
27 1
    @classmethod
28 1
    def hash(cls, exception=None, exc_info=None, include_traceback=True):
29
        if exception is not None:
30
            # Retrieve hash parameters from `Exception` object
31
            type = exception.type
0 ignored issues
show
Bug Best Practice introduced by
This seems to re-define the built-in type.

It is generally discouraged to redefine built-ins as this makes code very hard to read.

Loading history...
32
            message = exception.message
33
            tb = exception.traceback
34
        elif exc_info is not None:
35
            # Build hash parameters from `exc_info`
36
            type = cls.exc_type(exc_info[0])
37
            message = cls.exc_message(exc_info[1])
38
            tb = cls.exc_traceback(exc_info[2])
39
        else:
40
            raise ValueError
41
42
        m = hashlib.md5()
43
        m.update(str(type))
44
        m.update(str(message))
45
46
        if include_traceback:
47
            m.update(str(tb))
48
49
        return m.hexdigest()
50