Conditions | 22 |
Total Lines | 86 |
Lines | 0 |
Ratio | 0 % |
Tests | 0 |
CRAP Score | 506 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like ErrorReporter._emit() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | from plugin.core.constants import PLUGIN_VERSION_BASE, PLUGIN_VERSION_BRANCH |
||
15 | |||
16 | VERSION = '.'.join([str(x) for x in PLUGIN_VERSION_BASE]) |
||
17 | |||
18 | PARAMS = { |
||
19 | # Message processors + filters |
||
20 | 'processors': [ |
||
21 | 'raven.processors.RemoveStackLocalsProcessor', |
||
22 | 'plugin.raven.processors.RelativePathProcessor' |
||
23 | ], |
||
24 | |||
25 | # Plugin + System details |
||
26 | 'release': VERSION, |
||
27 | 'tags': { |
||
28 | # Plugin |
||
29 | 'plugin.version': VERSION, |
||
30 | 'plugin.branch': PLUGIN_VERSION_BRANCH, |
||
31 | |||
32 | # System |
||
33 | 'os.system': platform.system(), |
||
34 | 'os.release': platform.release(), |
||
35 | 'os.version': platform.version() |
||
36 | } |
||
37 | } |
||
38 | |||
39 | |||
40 | class ErrorReporter(Client): |
||
41 | server = 'sentry.skipthe.net' |
||
42 | key = '6bd64b4a32ac4ce280e809db6845210d:c7051e89810f4f4f9e543a0f142f533b' |
||
43 | project = 1 |
||
44 | |||
45 | def __init__(self, dsn=None, raise_send_errors=False, **options): |
||
46 | # Build URI |
||
47 | if dsn is None: |
||
48 | dsn = self.build_dsn() |
||
49 | |||
50 | # Construct raven client |
||
51 | super(ErrorReporter, self).__init__(dsn, raise_send_errors, **options) |
||
52 | |||
53 | def build_dsn(self, protocol='requests+http'): |
||
54 | return '%s://%s@%s/%s' % ( |
||
55 | protocol, |
||
56 | self.key, |
||
57 | self.server, |
||
58 | self.project |
||
59 | ) |
||
60 | |||
61 | def set_protocol(self, protocol): |
||
62 | # Build new DSN URI |
||
63 | dsn = self.build_dsn(protocol) |
||
64 | |||
65 | # Update client DSN |
||
66 | self.set_dsn(dsn) |
||
67 | |||
68 | |||
69 | class ErrorReporterHandler(SentryHandler): |
||
70 | def _emit(self, record, **kwargs): |
||
71 | data = { |
||
72 | 'user': {'id': self.client.name} |
||
73 | } |
||
74 | |||
75 | extra = getattr(record, 'data', None) |
||
76 | if not isinstance(extra, dict): |
||
77 | if extra: |
||
78 | extra = {'data': extra} |
||
79 | else: |
||
80 | extra = {} |
||
81 | |||
82 | for k, v in six.iteritems(vars(record)): |
||
83 | if k in RESERVED: |
||
84 | continue |
||
85 | if k.startswith('_'): |
||
86 | continue |
||
87 | if '.' not in k and k not in ('culprit', 'server_name'): |
||
88 | extra[k] = v |
||
89 | else: |
||
90 | data[k] = v |
||
91 | |||
92 | stack = getattr(record, 'stack', None) |
||
93 | if stack is True: |
||
94 | stack = iter_stack_frames() |
||
95 | |||
96 | if stack: |
||
97 | stack = self._get_targetted_stack(stack, record) |
||
98 | |||
99 | date = datetime.datetime.utcfromtimestamp(record.created) |
||
100 | event_type = 'raven.events.Message' |
||
101 | handler_kwargs = { |
||
167 |