Conditions | 11 |
Total Lines | 62 |
Lines | 0 |
Ratio | 0 % |
Tests | 0 |
CRAP Score | 132 |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
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 LogFileMessageEventHandler.handle() 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 | """ |
||
17 | @staticmethod |
||
18 | # ------------------------------------------------------------------------------------------------------------------ |
||
19 | def handle(_event, message, _listener_data): |
||
20 | """ |
||
21 | Handles a LogFileMessage received event. |
||
22 | |||
|
|||
23 | :param * _event: Not used. |
||
24 | :param enarksh.message.logger.LogFileMessage.LogFileMessage message: The message. |
||
25 | :param * _listener_data: Not used. |
||
26 | """ |
||
27 | del _event, _listener_data |
||
28 | |||
29 | print("%s %s %s" % (message.rnd_id, message.name, message.total_size)) |
||
30 | |||
31 | DataLayer.connect() |
||
32 | |||
33 | if message.total_size > 0: |
||
34 | # Read the log file or log files and concatenate if necessary. |
||
35 | with open(message.filename1, 'rb') as f: |
||
36 | log = f.read() |
||
37 | |||
38 | if message.filename2: |
||
39 | with open(message.filename2, 'rb') as f: |
||
40 | buf2 = f.read() |
||
41 | else: |
||
42 | buf2 = '' |
||
43 | |||
44 | # Compute the number of skipped bytes. |
||
45 | skipped = message.total_size - len(log) - len(buf2) |
||
46 | |||
47 | if skipped != 0: |
||
48 | # Add a newline to the end of the buffer, if required. |
||
49 | if log[-1:] != '\n': |
||
50 | log += '\n' |
||
51 | |||
52 | # Note: This concatenation doesn't work for multi byte character sets. |
||
53 | log += '\n' |
||
54 | log += "Enarksh: Skipped {0} bytes.\n".format(skipped) |
||
55 | log += '\n' |
||
56 | |||
57 | log += buf2 |
||
58 | |||
59 | blb_id = DataLayer.enk_blob_insert_blob(message.name, 'text/plain', log) |
||
60 | |||
61 | else: |
||
62 | blb_id = None |
||
63 | |||
64 | if message.name == 'out': |
||
65 | DataLayer.enk_back_run_node_update_log(message.rnd_id, blb_id, message.total_size) |
||
66 | elif message.name == 'err': |
||
67 | DataLayer.enk_back_run_node_update_err(message.rnd_id, blb_id, message.total_size) |
||
68 | else: |
||
69 | raise ValueError("Unknown output name '%s'" % message.name) |
||
70 | |||
71 | # Remove the (temporary) log files. |
||
72 | if message.filename1: |
||
73 | os.unlink(message.filename1) |
||
74 | if message.filename2: |
||
75 | os.unlink(message.filename2) |
||
76 | |||
77 | DataLayer.commit() |
||
78 | DataLayer.disconnect() |
||
79 | |||
81 |