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

glances/outputs/glances_sparklines.py (1 issue)

Checks whether class has too many instance attributes, try to reduce this to get a simpler (and so easier to use) class.

best-practice Informational
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 sparklines for Glances output."""
21
22
from __future__ import unicode_literals
23
from __future__ import division
24
import sys
25
from math import modf
26
from glances.logger import logger
27
from glances.compat import nativestr
28
29
sparklines_module = True
30
31
try:
32
    from sparklines import sparklines
33
except ImportError as e:
34
    logger.warning("Sparklines module not found ({})".format(e))
35
    sparklines_module = False
36
37
try:
38
    '┌┬┐╔╦╗╒╤╕╓╥╖│║─═├┼┤╠╬╣╞╪╡╟╫╢└┴┘╚╩╝╘╧╛╙╨╜'.encode(sys.stdout.encoding)
39
except (UnicodeEncodeError, TypeError):
40
    logger.warning("UTF-8 is mandatory for sparklines ({})".format(e))
41
    sparklines_module = False
42
43
44
class Sparkline(object):
0 ignored issues
show
Too many instance attributes (9/7)
Loading history...
45
46
    r"""Manage sparklines (see https://pypi.org/project/sparklines/)."""
47
48
    def __init__(self, size, pre_char='[', post_char=']', empty_char=' ', with_text=True):
49
        # If the sparklines python module available ?
50
        self.__available = sparklines_module
51
        # Sparkline size
52
        self.__size = size
53
        # Sparkline current percents list
54
        self.__percent = []
55
        # Char used for the decoration
56
        self.__pre_char = pre_char
57
        self.__post_char = post_char
58
        self.__empty_char = empty_char
59
        self.__with_text = with_text
60
61
    @property
62
    def available(self):
63
        return self.__available
64
65
    @property
66
    def size(self, with_decoration=False):
67
        # Return the sparkine size, with or without decoration
68
        if with_decoration:
69
            return self.__size
70
        if self.__with_text:
71
            return self.__size - 6
72
73
    @property
74
    def percents(self):
75
        return self.__percent
76
77
    @percents.setter
78
    def percents(self, value):
79
        self.__percent = value
80
81
    @property
82
    def pre_char(self):
83
        return self.__pre_char
84
85
    @property
86
    def post_char(self):
87
        return self.__post_char
88
89
    def get(self):
90
        """Return the sparkline."""
91
        ret = sparklines(self.percents)[0]
92
        if self.__with_text:
93
            percents_without_none = [x for x in self.percents if x is not None]
94
            if len(percents_without_none) > 0:
95
                ret = '{}{:5.1f}%'.format(ret, percents_without_none[-1])
96
        return nativestr(ret)
97
98
    def __str__(self):
99
        """Return the sparkline."""
100
        return self.get()
101