| Total Complexity | 8 |
| Total Lines | 42 |
| Duplicated Lines | 0 % |
| Coverage | 24% |
| Changes | 0 | ||
| 1 | 1 | from plugin.core.constants import PMS_PATH |
|
| 8 | 1 | class ErrorHasher(object): |
|
| 9 | 1 | @staticmethod |
|
| 10 | def exc_type(type): |
||
|
|
|||
| 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 |
||
| 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 |
It is generally discouraged to redefine built-ins as this makes code very hard to read.