Passed
Push — develop ( 3d8444...ce4f6d )
by Dean
03:03
created

RelativePathProcessor.to_relative()   B

Complexity

Conditions 6

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 0
cts 15
cp 0
rs 7.5384
c 0
b 0
f 0
cc 6
crap 42
1
from raven.processors import Processor
2
import logging
3
import os
4
5
log = logging.getLogger(__name__)
6
7
8
class RelativePathProcessor(Processor):
9
    separator = 'plug-ins'
10
11
    def process(self, data, **kwargs):
12
        data = super(RelativePathProcessor, self).process(data, **kwargs)
13
14
        try:
15
            extra = data.get('extra', {})
16
17
            self.to_relative(extra, 'pathname')
18
            self.to_relative(extra.get('sys.argv', []))
19
        except Exception:
0 ignored issues
show
Best Practice introduced by
Catching very general exceptions such as Exception is usually not recommended.

Generally, you would want to handle very specific errors in the exception handler. This ensure that you do not hide other types of errors which should be fixed.

So, unless you specifically plan to handle any error, consider adding a more specific exception.

Loading history...
20
            log.warn('Exception raised in RelativePathProcessor.process()', exc_info=True)
21
22
        return data
23
24
    def filter_stacktrace(self, data, **kwargs):
0 ignored issues
show
Unused Code introduced by
The argument kwargs seems to be unused.
Loading history...
25
        for frame in data.get('frames', []):
26
            try:
27
                self.to_relative(frame, 'abs_path')
28
                self.to_relative(frame, 'filename')
29
            except Exception:
0 ignored issues
show
Best Practice introduced by
Catching very general exceptions such as Exception is usually not recommended.

Generally, you would want to handle very specific errors in the exception handler. This ensure that you do not hide other types of errors which should be fixed.

So, unless you specifically plan to handle any error, consider adding a more specific exception.

Loading history...
30
                log.warn('Exception raised in RelativePathProcessor.filter_stacktrace()', exc_info=True)
31
32
    def to_relative(self, d, key=None):
33
        if type(d) is list:
34
            if key is not None:
35
                value = d[key]
36
            else:
37
                # Run `to_relative` on each item
38
                for x in range(len(d)):
39
                    self.to_relative(d, x)
40
41
                return True
42
        else:
43
            value = d.get(key)
44
45
        if not value:
46
            return False
47
48
        # Find `separator` position
49
        pos = os.path.normcase(value).find(self.separator)
50
51
        if pos == -1:
52
            return False
53
54
        # Update `d[key]` with relative path
55
        d[key] = value[pos:]
56
57
        return True
58