GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

LogFileMessageEventHandler.handle()   F
last analyzed

Complexity

Conditions 11

Size

Total Lines 63

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 132

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
c 1
b 0
f 0
dl 0
loc 63
ccs 0
cts 33
cp 0
crap 132
rs 3.9705

How to fix   Long Method    Complexity   

Long Method

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:

Complexity

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
"""
2
Enarksh
3
4
Copyright 2013-2016 Set Based IT Consultancy
5
6
Licence MIT
7
"""
8
import logging
9
import os
10
11
from enarksh.DataLayer import DataLayer
12
13
14
class LogFileMessageEventHandler:
15
    """
16
    An event handler for a LogFileMessage received events.
17
    """
18
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
83
# ----------------------------------------------------------------------------------------------------------------------
84