Completed
Push — master ( 2b80fa...6ea077 )
by Nicolas
01:22
created

GlancesStandalone.__init__()   C

Complexity

Conditions 8

Size

Total Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
c 1
b 0
f 0
dl 0
loc 51
rs 5.2591

How to fix   Long Method   

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:

1
# -*- coding: utf-8 -*-
2
#
3
# This file is part of Glances.
4
#
5
# Copyright (C) 2017 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 sched
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.outdated import Outdated
31
32
33
class GlancesStandalone(object):
34
35
    """This class creates and manages the Glances standalone session."""
36
37
    def __init__(self, config=None, args=None):
38
        # Quiet mode
39
        self._quiet = args.quiet
40
        self.refresh_time = args.time
41
42
        # Init stats
43
        self.stats = GlancesStats(config=config, args=args)
44
45
        # If process extended stats is disabled by user
46
        if not args.enable_process_extended:
47
            logger.debug("Extended stats for top process are disabled")
48
            glances_processes.disable_extended()
49
        else:
50
            logger.debug("Extended stats for top process are enabled")
51
            glances_processes.enable_extended()
52
53
        # Manage optionnal process filter
54
        if args.process_filter is not None:
55
            glances_processes.process_filter = args.process_filter
56
57
        if (not WINDOWS) and args.no_kernel_threads:
58
            # Ignore kernel threads in process list
59
            glances_processes.disable_kernel_threads()
60
61
        try:
62
            if args.process_tree:
63
                # Enable process tree view
64
                glances_processes.enable_tree()
65
        except AttributeError:
66
            pass
67
68
        # Initial system informations update
69
        self.stats.update()
70
71
        if self.quiet:
72
            logger.info("Quiet mode is ON: Nothing will be displayed")
73
            # In quiet mode, nothing is displayed
74
            glances_processes.max_processes = 0
75
        else:
76
            # Default number of processes to displayed is set to 50
77
            glances_processes.max_processes = 50
78
79
            # Init screen
80
            self.screen = GlancesCursesStandalone(config=config, args=args)
81
82
        # Check the latest Glances version
83
        self.outdated = Outdated(config=config, args=args)
84
85
        # Create the schedule instance
86
        self.schedule = sched.scheduler(
87
            timefunc=time.time, delayfunc=time.sleep)
88
89
    @property
90
    def quiet(self):
91
        return self._quiet
92
93
    def __serve_forever(self):
94
        """Main loop for the CLI."""
95
        # Reschedule the function inside itself
96
        self.schedule.enter(self.refresh_time, priority=0,
97
                            action=self.__serve_forever, argument=())
98
99
        # Update system informations
100
        self.stats.update()
101
102
        # Update the screen
103
        if not self.quiet:
104
            self.screen.update(self.stats)
105
106
        # Export stats using export modules
107
        self.stats.export(self.stats)
108
109
    def serve_forever(self):
110
        """Wrapper to the serve_forever function.
111
112
        This function will restore the terminal to a sane state
113
        before re-raising the exception and generating a traceback.
114
        """
115
        self.__serve_forever()
116
        try:
117
            self.schedule.run()
118
        finally:
119
            self.end()
120
121
    def end(self):
122
        """End of the standalone CLI."""
123
        if not self.quiet:
124
            self.screen.end()
125
126
        # Exit from export modules
127
        self.stats.end()
128
129
        # Check Glances version versus Pypi one
130
        if self.outdated.is_outdated():
131
            print("You are using Glances version {}, however version {} is available.".format(
132
                self.outdated.installed_version(), self.outdated.latest_version()))
133
            print("You should consider upgrading using: pip install --upgrade glances")
134