Test Failed
Push — develop ( 2f2c0e...2adb06 )
by Dean
02:41
created

ErrorHasher.exc_message()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 1
dl 0
loc 3
rs 10
1
from plugin.core.constants import PMS_PATH
2
3
import hashlib
4
import os
5
import traceback
6
7
8
class ErrorHasher(object):
9
    @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
    @staticmethod
14
    def exc_message(exception):
15
        return getattr(exception, 'message', None)
16
17
    @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
    @classmethod
28
    def hash(cls, exception=None, exc_info=None):
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
        m.update(str(tb))
46
47
        return m.hexdigest()
48