Completed
Push — master ( bcff18...adb900 )
by Nicolas
01:15
created

glances.GlancesStandalone   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 87
Duplicated Lines 0 %
Metric Value
dl 0
loc 87
rs 10
wmc 15

5 Methods

Rating   Name   Duplication   Size   Complexity  
D __init__() 0 44 8
A quiet() 0 3 1
A __serve_forever() 0 15 3
A serve_forever() 0 10 1
A end() 0 7 2
1
# -*- coding: utf-8 -*-
2
#
3
# This file is part of Glances.
4
#
5
# Copyright (C) 2015 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
from time import sleep
23
24
from glances.globals import WINDOWS
25
from glances.logger import logger
26
from glances.processes import glances_processes
27
from glances.stats import GlancesStats
28
from glances.outputs.glances_curses import GlancesCursesStandalone
29
30
31
class GlancesStandalone(object):
32
33
    """This class creates and manages the Glances standalone session."""
34
35
    def __init__(self, config=None, args=None):
36
        # Quiet mode
37
        self._quiet = args.quiet
38
        self.refresh_time = args.time
39
40
        # Init stats
41
        self.stats = GlancesStats(config=config, args=args)
42
43
        # If process extended stats is disabled by user
44
        if not args.enable_process_extended:
45
            logger.debug("Extended stats for top process are disabled")
46
            glances_processes.disable_extended()
47
        else:
48
            logger.debug("Extended stats for top process are enabled")
49
            glances_processes.enable_extended()
50
51
        # Manage optionnal process filter
52
        if args.process_filter is not None:
53
            glances_processes.process_filter = args.process_filter
54
55
        if (not WINDOWS) and args.no_kernel_threads:
56
            # Ignore kernel threads in process list
57
            glances_processes.disable_kernel_threads()
58
59
        try:
60
            if args.process_tree:
61
                # Enable process tree view
62
                glances_processes.enable_tree()
63
        except AttributeError:
64
            pass
65
66
        # Initial system informations update
67
        self.stats.update()
68
69
        if self.quiet:
70
            logger.info("Quiet mode is ON: Nothing will be displayed")
71
            # In quiet mode, nothing is displayed
72
            glances_processes.max_processes = 0
73
        else:
74
            # Default number of processes to displayed is set to 50
75
            glances_processes.max_processes = 50
76
77
            # Init screen
78
            self.screen = GlancesCursesStandalone(args=args)
79
80
    @property
81
    def quiet(self):
82
        return self._quiet
83
84
    def __serve_forever(self):
85
        """Main loop for the CLI."""
86
        while True:
87
            # Update system informations
88
            self.stats.update()
89
90
            if not self.quiet:
91
                # Update the screen
92
                self.screen.update(self.stats)
93
            else:
94
                # Wait...
95
                sleep(self.refresh_time)
96
97
            # Export stats using export modules
98
            self.stats.export(self.stats)
99
100
    def serve_forever(self):
101
        """Wrapper to the serve_forever function.
102
103
        This function will restore the terminal to a sane state
104
        before re-raising the exception and generating a traceback.
105
        """
106
        try:
107
            return self.__serve_forever()
108
        finally:
109
            self.end()
110
111
    def end(self):
112
        """End of the standalone CLI."""
113
        if not self.quiet:
114
            self.screen.end()
115
116
        # Exit from export modules
117
        self.stats.end()
118