Test Failed
Push — develop ( abf64f...1d1151 )
by Nicolas
02:59
created

glances.outputs.glances_sparklines.Sparkline.get()   A

Complexity

Conditions 3

Size

Total Lines 8
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nop 1
dl 0
loc 8
rs 10
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 sparklines for Glances output."""
21
22
from __future__ import unicode_literals
23
from __future__ import division
24
import sys
0 ignored issues
show
introduced by Nicolas Hennion
import missing from __future__ import absolute_import
Loading history...
25
from math import modf
0 ignored issues
show
introduced by Nicolas Hennion
import missing from __future__ import absolute_import
Loading history...
Unused Code introduced by Nicolas Hennion
Unused modf imported from math
Loading history...
26
from glances.logger import logger
0 ignored issues
show
introduced by Pawel
import missing from __future__ import absolute_import
Loading history...
27
from glances.compat import nativestr
0 ignored issues
show
introduced by Nicolas Hennion
import missing from __future__ import absolute_import
Loading history...
28
29
sparklines_module = True
0 ignored issues
show
Coding Style Naming introduced by Nicolas Hennion
The name sparklines_module does not conform to the constant naming conventions ((([A-Z_][A-Z0-9_]*)|(__.*__))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
30
31
try:
32
    from sparklines import sparklines
0 ignored issues
show
introduced by Nicolas Hennion
import missing from __future__ import absolute_import
Loading history...
33
except ImportError as e:
0 ignored issues
show
Coding Style Naming introduced by Nicolas Hennion
The name e does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
34
    logger.warning("Sparklines module not found ({})".format(e))
0 ignored issues
show
introduced by Nicolas Hennion
Use formatting in logging functions and pass the parameters as arguments
Loading history...
35
    sparklines_module = False
0 ignored issues
show
Coding Style Naming introduced by Nicolas Hennion
The name sparklines_module does not conform to the constant naming conventions ((([A-Z_][A-Z0-9_]*)|(__.*__))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
36
37
try:
38
    '┌┬┐╔╦╗╒╤╕╓╥╖│║─═├┼┤╠╬╣╞╪╡╟╫╢└┴┘╚╩╝╘╧╛╙╨╜'.encode(sys.stdout.encoding)
39
except (UnicodeEncodeError, TypeError):
40
    logger.warning("UTF-8 is mandatory for sparklines ({})".format(e))
0 ignored issues
show
introduced by Nicolas Hennion
Use formatting in logging functions and pass the parameters as arguments
Loading history...
Comprehensibility Best Practice introduced by Nicolas Hennion
The variable e does not seem to be defined.
Loading history...
41
    sparklines_module = False
0 ignored issues
show
Coding Style Naming introduced by Nicolas Hennion
The name sparklines_module does not conform to the constant naming conventions ((([A-Z_][A-Z0-9_]*)|(__.*__))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
42
43
44
class Sparkline(object):
0 ignored issues
show
best-practice introduced by Nicolas Hennion
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):
0 ignored issues
show
Coding Style introduced by Nicolas Hennion
This line is too long as per the coding-style (90/80).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
best-practice introduced by Nicolas Hennion
Too many arguments (6/5)
Loading history...
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):
0 ignored issues
show
Coding Style introduced by Nicolas Hennion
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
63
        return self.__available
64
65
    @property
66
    def size(self, with_decoration=False):
0 ignored issues
show
Coding Style introduced by Nicolas Hennion
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
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):
0 ignored issues
show
Coding Style introduced by Alessio Sergi
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
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):
0 ignored issues
show
Coding Style introduced by Nicolas Hennion
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
83
        return self.__pre_char
84
85
    @property
86
    def post_char(self):
0 ignored issues
show
Coding Style introduced by Alessio Sergi
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
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:
0 ignored issues
show
introduced by Nicolas Hennion
Do not use len(SEQUENCE) as condition value
Loading history...
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