Test Failed
Push — master ( aea994...69b639 )
by Nicolas
03:44 queued 10s
created

GlancesStandalone.__serve_once()   A

Complexity

Conditions 3

Size

Total Lines 35
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 14
nop 1
dl 0
loc 35
rs 9.7
c 0
b 0
f 0
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
23
import time
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
30
from glances.outputs.glances_stdout import GlancesStdout
31
from glances.outputs.glances_stdout_csv import GlancesStdoutCsv
32
from glances.outputs.glances_stdout_issue import GlancesStdoutIssue
33
from glances.outdated import Outdated
34
from glances.timer import Counter
35
36
37
class GlancesStandalone(object):
38
39
    """This class creates and manages the Glances standalone session."""
40
41
    def __init__(self, config=None, args=None):
42
        self.config = config
43
        self.args = args
44
45
        # Quiet mode
46
        self._quiet = args.quiet
47
        self.refresh_time = args.time
48
49
        # Init stats
50
        start_duration = Counter()
51
        start_duration.reset()
52
        self.stats = GlancesStats(config=config, args=args)
53
        logger.debug("Plugins initialisation duration: {} seconds".format(start_duration.get()))
54
55
        # Modules (plugins and exporters) are loaded at this point
56
        # Glances can display the list if asked...
57
        if args.modules_list:
58
            self.display_modules_list()
59
            sys.exit(0)
60
61
        # If process extended stats is disabled by user
62
        if not args.enable_process_extended:
63
            logger.debug("Extended stats for top process are disabled")
64
            glances_processes.disable_extended()
65
        else:
66
            logger.debug("Extended stats for top process are enabled")
67
            glances_processes.enable_extended()
68
69
        # Manage optionnal process filter
70
        if args.process_filter is not None:
71
            glances_processes.process_filter = args.process_filter
72
73
        if (not WINDOWS) and args.no_kernel_threads:
74
            # Ignore kernel threads in process list
75
            glances_processes.disable_kernel_threads()
76
77
        # Initial system informations update
78
        start_duration.reset()
79
        self.stats.update()
80
        logger.debug("First stats update duration: {} seconds".format(start_duration.get()))
81
82
        if self.quiet:
83
            logger.info("Quiet mode is ON, nothing will be displayed")
84
            # In quiet mode, nothing is displayed
85
            glances_processes.max_processes = 0
86
        elif args.stdout_issue:
87
            logger.info("Issue mode is ON")
88
            # Init screen
89
            self.screen = GlancesStdoutIssue(config=config, args=args)
90
        elif args.stdout:
91
            logger.info("Stdout mode is ON, following stats will be displayed: {}".format(args.stdout))
92
            # Init screen
93
            self.screen = GlancesStdout(config=config, args=args)
94
        elif args.stdout_csv:
95
            logger.info("Stdout CSV mode is ON, following stats will be displayed: {}".format(args.stdout))
96
            # Init screen
97
            self.screen = GlancesStdoutCsv(config=config, args=args)
98
        else:
99
            # Default number of processes to displayed is set to 50
100
            glances_processes.max_processes = 50
101
102
            # Init screen
103
            self.screen = GlancesCursesStandalone(config=config, args=args)
104
105
        # Check the latest Glances version
106
        self.outdated = Outdated(config=config, args=args)
107
108
    @property
109
    def quiet(self):
110
        return self._quiet
111
112
    def display_modules_list(self):
113
        """Display modules list"""
114
        print("Plugins list: {}".format(
115
            ', '.join(sorted(self.stats.getPluginsList(enable=False)))))
116
        print("Exporters list: {}".format(
117
            ', '.join(sorted(self.stats.getExportsList(enable=False)))))
118
119
    def serve_issue(self):
120
        """Special mode for the --issue option
121
        Update is done in the sceen.update function
122
        """
123
        ret = not self.screen.update(self.stats)
124
        self.end()
125
        return ret
126
127
    def __serve_once(self):
128
        """Main loop for the CLI.
129
130
        return True if we should continue (no exit key has been pressed)
131
        """
132
        # Start a counter used to compute the time needed for
133
        # update and export the stats
134
        counter = Counter()
135
136
        # Update stats
137
        self.stats.update()
138
        logger.debug('Stats updated duration: {} seconds'.format(counter.get()))
139
140
        # Export stats
141
        counter_export = Counter()
142
        self.stats.export(self.stats)
143
        logger.debug('Stats exported duration: {} seconds'.format(counter_export.get()))
144
145
        # Patch for issue1326 to avoid < 0 refresh
146
        adapted_refresh = self.refresh_time - counter.get()
147
        adapted_refresh = adapted_refresh if adapted_refresh > 0 else 0
148
149
        # Display stats
150
        # and wait refresh_time - counter
151
        if not self.quiet:
152
            # The update function return True if an exit key 'q' or 'ESC'
153
            # has been pressed.
154
            ret = not self.screen.update(self.stats, duration=adapted_refresh)
155
        else:
156
            # Nothing is displayed
157
            # Break should be done via a signal (CTRL-C)
158
            time.sleep(adapted_refresh)
159
            ret = True
160
161
        return ret
162
163
    def serve_forever(self):
164
        """Wrapper to the serve_forever function."""
165
        loop = True
166
        while loop:
167
            loop = self.__serve_once()
168
        self.end()
169
170
    def end(self):
171
        """End of the standalone CLI."""
172
        if not self.quiet:
173
            self.screen.end()
174
175
        # Exit from export modules
176
        self.stats.end()
177
178
        # Check Glances version versus PyPI one
179
        if self.outdated.is_outdated():
180
            print("You are using Glances version {}, however version {} is available.".format(
181
                self.outdated.installed_version(), self.outdated.latest_version()))
182
            print("You should consider upgrading using: pip install --upgrade glances")
183