Test Failed
Push — master ( b668b0...91d406 )
by Nicolas
07:51 queued 03:03
created

glances.outputs.glances_bars.Bar.get()   A

Complexity

Conditions 3

Size

Total Lines 11
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 10
nop 1
dl 0
loc 11
rs 9.9
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 bars for Glances output."""
21
22
from __future__ import division
23
24
from math import modf
0 ignored issues
show
introduced by
import missing from __future__ import absolute_import
Loading history...
25
26
curses_bars = [' ', ' ', ' ', ' ', '|', '|', '|', '|', '|']
0 ignored issues
show
Coding Style Naming introduced by
The name curses_bars 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...
27
28
29
class Bar(object):
0 ignored issues
show
best-practice introduced by
Too many instance attributes (10/7)
Loading history...
30
31
    r"""Manage bar (progression or status).
32
33
    import sys
34
    import time
35
    b = Bar(10)
36
    for p in range(0, 100):
37
        b.percent = p
38
        print("\r%s" % b),
39
        time.sleep(0.1)
40
        sys.stdout.flush()
41
    """
42
43
    def __init__(self, size, pre_char='[', post_char=']', empty_char=' ', with_text=True):
0 ignored issues
show
Coding Style introduced by
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
Too many arguments (6/5)
Loading history...
44
        # Bar size
45
        self.__size = size
46
        # Bar current percent
47
        self.__percent = 0
48
        # Min and max value
49
        self.min_value = 0
50
        self.max_value = 100
51
        # Char used for the decoration
52
        self.__pre_char = pre_char
53
        self.__post_char = post_char
54
        self.__empty_char = empty_char
55
        self.__with_text = with_text
56
57
    @property
58
    def size(self, with_decoration=False):
0 ignored issues
show
Coding Style introduced by
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...
59
        # Return the bar size, with or without decoration
60
        if with_decoration:
61
            return self.__size
62
        if self.__with_text:
63
            return self.__size - 6
64
65
    @property
66
    def percent(self):
0 ignored issues
show
Coding Style introduced by
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 self.__percent
68
69
    @percent.setter
70
    def percent(self, value):
71
        if value <= self.min_value:
72
            value = self.min_value
73
        if value >= self.max_value:
74
            value = self.max_value
75
        self.__percent = value
76
77
    @property
78
    def pre_char(self):
0 ignored issues
show
Coding Style introduced by
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...
79
        return self.__pre_char
80
81
    @property
82
    def post_char(self):
0 ignored issues
show
Coding Style introduced by
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.__post_char
84
85
    def get(self):
86
        """Return the bars."""
87
        frac, whole = modf(self.size * self.percent / 100.0)
88
        ret = curses_bars[8] * int(whole)
89
        if frac > 0:
90
            ret += curses_bars[int(frac * 8)]
91
            whole += 1
92
        ret += self.__empty_char * int(self.size - whole)
93
        if self.__with_text:
94
            ret = '{}{:5.1f}%'.format(ret, self.percent)
95
        return ret
96
97
    def __str__(self):
98
        """Return the bars."""
99
        return self.get()
100