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

GlancesStdoutIssue.update()   B

Complexity

Conditions 5

Size

Total Lines 42
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 32
nop 3
dl 0
loc 42
rs 8.6453
c 0
b 0
f 0
1
# -*- coding: utf-8 -*-
2
#
3
# This file is part of Glances.
4
#
5
# Copyright (C) 2021 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
"""Issue interface class."""
21
22
import time
23
import sys
24
import shutil
25
26
from glances.logger import logger
27
from glances.compat import printandflush
28
from glances.timer import Counter
29
from glances import __version__, psutil_version
30
31
try:
32
    TERMINAL_WIDTH = shutil.get_terminal_size(fallback=(79, 24)).columns
33
except:
34
    TERMINAL_WIDTH = 79
35
36
37
class colors:
38
    RED = '\033[91m'
39
    GREEN = '\033[92m'
40
    ORANGE = '\033[93m'
41
    BLUE = '\033[94m'
42
    NO = '\033[0m'
43
44
    def disable(self):
45
        self.RED = ''
46
        self.GREEN = ''
47
        self.BLUE = ''
48
        self.ORANGE = ''
49
        self.NO = ''
50
51
52
class GlancesStdoutIssue(object):
53
54
    """
55
    This class manages the Issue display.
56
    """
57
58
    def __init__(self, config=None, args=None):
59
        # Init
60
        self.config = config
61
        self.args = args
62
63
    def end(self):
64
        pass
65
66
    def print_version(self):
67
        msg = 'Glances version {} with PsUtil {}'.format(
68
            colors.BLUE + __version__ + colors.NO, 
69
            colors.BLUE + psutil_version + colors.NO)
70
        sys.stdout.write('='*len(msg) + '\n')
71
        sys.stdout.write(msg)
72
        sys.stdout.write(colors.NO + '\n')
73
        sys.stdout.write('='*len(msg) + '\n')
74
        sys.stdout.flush()
75
76
    def print_issue(self, plugin, result, message):
77
        sys.stdout.write('{}{}{}'.format(
78
            colors.BLUE + plugin, result, message))
79
        sys.stdout.write(colors.NO + '\n')
80
        sys.stdout.flush()
81
82
    def update(self,
83
               stats,
84
               duration=3):
85
        """Display issue
86
        """
87
        self.print_version()
88
        for plugin in sorted(stats._plugins):
89
            if stats._plugins[plugin].is_disable():
90
                # If current plugin is disable
91
                # then continue to next plugin
92
                result = colors.ORANGE + '[N/A]'.rjust(19 - len(plugin))
93
                message = colors.NO
94
                self.print_issue(plugin, result, message)
95
                continue
96
            # Start the counter
97
            counter = Counter()
98
            counter.reset()
99
            stat = None
100
            stat_error = None
101
            try:
102
                # Update the stats
103
                stats._plugins[plugin].update()
104
                # Get the stats
105
                stat = stats.get_plugin(plugin).get_export()
106
            except Exception as e:
107
                stat_error = e
108
            if stat_error is None:
109
                result = (colors.GREEN +
110
                          '[OK]   ' +
111
                          colors.BLUE +
112
                          ' {:.4f}s '.format(counter.get())).rjust(40 - len(plugin))
113
                message = colors.NO + str(stat)[0:TERMINAL_WIDTH-40]
114
            else:
115
                result = (colors.RED +
116
                          '[ERROR]' +
117
                          colors.BLUE +
118
                          ' {:.4f}s '.format(counter.get())).rjust(40 - len(plugin))
119
                message = colors.NO + str(stat_error)[0:TERMINAL_WIDTH-40]
120
            self.print_issue(plugin, result, message)
121
122
        # Return True to exit directly (no refresh)
123
        return True
124