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