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

glances/standalone.py (24 issues)

1
# -*- coding: utf-8 -*-
2
#
3
# This file is part of Glances.
4
#
5
# Copyright (C) 2019 Nicolargo <[email protected]>
6
#
7
# Glances is free software; you can redistribute it and/or modify
8
# it under the terms of the GNU Lesser General Public License as published by
9
# the Free Software Foundation, either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Glances is distributed in the hope that it will be useful,
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU Lesser General Public License for more details.
16
#
17
# You should have received a copy of the GNU Lesser General Public License
18
# along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20
"""Manage the Glances standalone session."""
21
22
import sys
0 ignored issues
show
import missing from __future__ import absolute_import
Loading history...
23
import time
0 ignored issues
show
import missing from __future__ import absolute_import
Loading history...
24
25
from glances.globals import WINDOWS
26
from glances.logger import logger
27
from glances.processes import glances_processes
28
from glances.stats import GlancesStats
29
from glances.outputs.glances_curses import GlancesCursesStandalone
0 ignored issues
show
import missing from __future__ import absolute_import
Loading history...
30
from glances.outputs.glances_stdout import GlancesStdout
0 ignored issues
show
import missing from __future__ import absolute_import
Loading history...
31
from glances.outputs.glances_stdout_csv import GlancesStdoutCsv
0 ignored issues
show
import missing from __future__ import absolute_import
Loading history...
32
from glances.outdated import Outdated
0 ignored issues
show
import missing from __future__ import absolute_import
Loading history...
33
from glances.timer import Counter
0 ignored issues
show
import missing from __future__ import absolute_import
Loading history...
34
35
36
class GlancesStandalone(object):
37
38
    """This class creates and manages the Glances standalone session."""
39
40
    def __init__(self, config=None, args=None):
41
        self.config = config
42
        self.args = args
43
44
        # Quiet mode
45
        self._quiet = args.quiet
46
        self.refresh_time = args.time
47
48
        # Init stats
49
        start_duration = Counter()
50
        start_duration.reset()
51
        self.stats = GlancesStats(config=config, args=args)
52
        logger.debug("Plugins initialisation duration: {} seconds".format(start_duration.get()))
0 ignored issues
show
This line is too long as per the coding-style (96/80).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
Use formatting in logging functions and pass the parameters as arguments
Loading history...
53
54
        # Modules (plugins and exporters) are loaded at this point
55
        # Glances can display the list if asked...
56
        if args.modules_list:
57
            self.display_modules_list()
58
            sys.exit(0)
59
60
        # If process extended stats is disabled by user
61
        if not args.enable_process_extended:
62
            logger.debug("Extended stats for top process are disabled")
63
            glances_processes.disable_extended()
64
        else:
65
            logger.debug("Extended stats for top process are enabled")
66
            glances_processes.enable_extended()
67
68
        # Manage optionnal process filter
69
        if args.process_filter is not None:
70
            glances_processes.process_filter = args.process_filter
71
72
        if (not WINDOWS) and args.no_kernel_threads:
73
            # Ignore kernel threads in process list
74
            glances_processes.disable_kernel_threads()
75
76
        # Initial system informations update
77
        start_duration.reset()
78
        self.stats.update()
79
        logger.debug("First stats update duration: {} seconds".format(start_duration.get()))
0 ignored issues
show
This line is too long as per the coding-style (92/80).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
Use formatting in logging functions and pass the parameters as arguments
Loading history...
80
81
        if self.quiet:
82
            logger.info("Quiet mode is ON, nothing will be displayed")
83
            # In quiet mode, nothing is displayed
84
            glances_processes.max_processes = 0
85
        elif args.stdout:
86
            logger.info("Stdout mode is ON, following stats will be displayed: {}".format(args.stdout))
0 ignored issues
show
This line is too long as per the coding-style (103/80).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
Use formatting in logging functions and pass the parameters as arguments
Loading history...
87
            # Init screen
88
            self.screen = GlancesStdout(config=config, args=args)
89
        elif args.stdout_csv:
90
            logger.info("Stdout CSV mode is ON, following stats will be displayed: {}".format(args.stdout))
0 ignored issues
show
This line is too long as per the coding-style (107/80).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
Use formatting in logging functions and pass the parameters as arguments
Loading history...
91
            # Init screen
92
            self.screen = GlancesStdoutCsv(config=config, args=args)
93
        else:
94
            # Default number of processes to displayed is set to 50
95
            glances_processes.max_processes = 50
96
97
            # Init screen
98
            self.screen = GlancesCursesStandalone(config=config, args=args)
99
100
        # Check the latest Glances version
101
        self.outdated = Outdated(config=config, args=args)
102
103
    @property
104
    def quiet(self):
0 ignored issues
show
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
105
        return self._quiet
106
107
    def display_modules_list(self):
108
        """Display modules list"""
109
        print("Plugins list: {}".format(
0 ignored issues
show
print statement used
Loading history...
110
            ', '.join(sorted(self.stats.getPluginsList(enable=False)))))
111
        print("Exporters list: {}".format(
0 ignored issues
show
print statement used
Loading history...
112
            ', '.join(sorted(self.stats.getExportsList(enable=False)))))
113
114
    def __serve_forever(self):
115
        """Main loop for the CLI.
116
117
        return True if we should continue (no exit key has been pressed)
118
        """
119
        # Start a counter used to compute the time needed for
120
        # update and export the stats
121
        counter = Counter()
122
123
        # Update stats
124
        self.stats.update()
125
        logger.debug('Stats updated duration: {} seconds'.format(counter.get()))
0 ignored issues
show
Use formatting in logging functions and pass the parameters as arguments
Loading history...
126
127
        # Export stats
128
        counter_export = Counter()
129
        self.stats.export(self.stats)
130
        logger.debug('Stats exported duration: {} seconds'.format(counter_export.get()))
0 ignored issues
show
This line is too long as per the coding-style (88/80).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
Use formatting in logging functions and pass the parameters as arguments
Loading history...
131
132
        # Patch for issue1326 to avoid < 0 refresh
133
        adapted_refresh = self.refresh_time - counter.get()
134
        adapted_refresh = adapted_refresh if adapted_refresh > 0 else 0
135
136
        # Display stats
137
        # and wait refresh_time - counter
138
        if not self.quiet:
139
            # The update function return True if an exit key 'q' or 'ESC'
140
            # has been pressed.
141
            ret = not self.screen.update(self.stats, duration=adapted_refresh)
142
        else:
143
            # Nothing is displayed
144
            # Break should be done via a signal (CTRL-C)
145
            time.sleep(adapted_refresh)
146
            ret = True
147
148
        return ret
149
150
    def serve_forever(self):
151
        """Wrapper to the serve_forever function."""
152
        loop = True
153
        while loop:
154
            loop = self.__serve_forever()
155
        self.end()
156
157
    def end(self):
158
        """End of the standalone CLI."""
159
        if not self.quiet:
160
            self.screen.end()
161
162
        # Exit from export modules
163
        self.stats.end()
164
165
        # Check Glances version versus PyPI one
166
        if self.outdated.is_outdated():
167
            print("You are using Glances version {}, however version {} is available.".format(
168
                self.outdated.installed_version(), self.outdated.latest_version()))
169
            print("You should consider upgrading using: pip install --upgrade glances")
0 ignored issues
show
This line is too long as per the coding-style (87/80).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
Unused Code Coding Style introduced by
Unnecessary parens after u'print' keyword
Loading history...
print statement used
Loading history...
170