Test Failed
Push — develop ( 66c9ff...e21229 )
by Nicolas
05:06
created

glances/exports/glances_json.py (11 issues)

1
"""JSON interface class."""
2
3
import sys
0 ignored issues
show
import missing from __future__ import absolute_import
Loading history...
4
import json
0 ignored issues
show
import missing from __future__ import absolute_import
Loading history...
5
6
from glances.compat import PY3, listkeys
0 ignored issues
show
import missing from __future__ import absolute_import
Loading history...
7
from glances.logger import logger
0 ignored issues
show
import missing from __future__ import absolute_import
Loading history...
8
from glances.exports.glances_export import GlancesExport
0 ignored issues
show
import missing from __future__ import absolute_import
Loading history...
9
10
11
class Export(GlancesExport):
12
13
    """This class manages the JSON export module."""
14
15
    def __init__(self, config=None, args=None):
16
        """Init the JSON export IF."""
17
        super(Export, self).__init__(config=config, args=args)
18
19
        # JSON file name
20
        self.json_filename = args.export_json_file
21
22
        # Set the JSON output file
23
        try:
24
            if PY3:
25
                self.json_file = open(self.json_filename, 'w')
26
            else:
27
                self.json_file = open(self.json_filename, 'wb')
28
        except IOError as e:
0 ignored issues
show
Coding Style Naming introduced by
The name e does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
29
            logger.critical("Cannot create the JSON file: {}".format(e))
0 ignored issues
show
Use formatting in logging functions and pass the parameters as arguments
Loading history...
30
            sys.exit(2)
31
32
        logger.info("Exporting stats to file: {}".format(self.json_filename))
0 ignored issues
show
Use formatting in logging functions and pass the parameters as arguments
Loading history...
33
34
        self.export_enable = True
35
36
        # Buffer for dict of stats
37
        self.buffer = {}
38
39
    def exit(self):
40
        """Close the JSON file."""
41
        logger.debug("Finalise export interface %s" % self.export_name)
0 ignored issues
show
Coding Style Best Practice introduced by
Specify string format arguments as logging function parameters
Loading history...
42
        self.json_file.close()
43
44
    def export(self, name, columns, points):
45
        """Export the stats to the JSON file."""
46
47
        # Check for completion of loop for all exports
48
        if name == self.plugins_to_export()[0] and self.buffer != {}:
49
            # One whole loop has been completed
50
            # Flush stats to file
51
            logger.debug("Exporting stats ({}) to JSON file ({})".format(
0 ignored issues
show
Use formatting in logging functions and pass the parameters as arguments
Loading history...
52
                listkeys(self.buffer),
53
                self.json_filename)
54
            )
0 ignored issues
show
Wrong continued indentation (add 12 spaces).
Loading history...
55
56
            # Export stats to JSON file
57
            data_json = json.dumps(self.buffer)
58
            self.json_file.write("{}\n".format(data_json))
59
60
            # Reset buffer
61
            self.buffer = {}
62
63
        # Add current stat to the buffer
64
        self.buffer[name] = dict(zip(columns, points))
65