Test Failed
Push — develop ( d7cf39...faa4bd )
by Nicolas
04:34 queued 10s
created

glances/thresholds.py (13 issues)

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
"""
21
Thresholds classes: OK, CAREFUL, WARNING, CRITICAL
22
"""
23
24
import sys
0 ignored issues
show
import missing from __future__ import absolute_import
Loading history...
25
from functools import total_ordering
0 ignored issues
show
import missing from __future__ import absolute_import
Loading history...
26
27
28
class GlancesThresholds(object):
29
    """Class to manage thresholds dict for all Glances plugins:
30
    key: Glances stats (example: cpu_user)
31
    value: Threasold* instance
32
    """
33
34
    threshold_list = ['OK', 'CAREFUL', 'WARNING', 'CRITICAL']
35
36
    def __init__(self):
37
        self.current_module = sys.modules[__name__]
38
        self._thresholds = {}
39
40
    def get(self, stat_name=None):
41
        """Return the threshold dict.
42
        If stat_name is None, return the threshold for all plugins (dict of Threshold*)
0 ignored issues
show
This line is too long as per the coding-style (87/80).

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

Loading history...
43
        Else return the Threshold* instance for the given plugin
44
        """
45
        if stat_name is None:
46
            return self._thresholds
47
48
        if stat_name in self._thresholds:
0 ignored issues
show
Unnecessary "else" after "return"
Loading history...
49
            return self._thresholds[stat_name]
50
        else:
51
            return {}
52
53
    def add(self, stat_name, threshold_description):
54
        """Add a new threshold to the dict (key = stat_name)"""
55
        if threshold_description not in self.threshold_list:
0 ignored issues
show
Unnecessary "else" after "return"
Loading history...
56
            return False
57
        else:
58
            self._thresholds[stat_name] = getattr(self.current_module,
59
                                                  'GlancesThreshold' + threshold_description.capitalize())()
0 ignored issues
show
This line is too long as per the coding-style (108/80).

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

Loading history...
60
            return True
61
62
63
# Global variable uses to share thresholds between Glances componants
64
glances_thresholds = GlancesThresholds()
0 ignored issues
show
Coding Style Naming introduced by
The name glances_thresholds 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...
65
66
67
@total_ordering
0 ignored issues
show
Implementing __eq__ without also implementing __hash__
Loading history...
68
class _GlancesThreshold(object):
69
70
    """Father class for all other Thresholds"""
71
72
    def description(self):
0 ignored issues
show
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...
73
        return self._threshold['description']
0 ignored issues
show
The Instance of _GlancesThreshold does not seem to have a member named _threshold.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
74
75
    def value(self):
0 ignored issues
show
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...
76
        return self._threshold['value']
0 ignored issues
show
The Instance of _GlancesThreshold does not seem to have a member named _threshold.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
77
78
    def __repr__(self):
79
        return str(self._threshold)
0 ignored issues
show
The Instance of _GlancesThreshold does not seem to have a member named _threshold.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
80
81
    def __str__(self):
82
        return self.description()
83
84
    def __lt__(self, other):
85
        return self.value() < other.value()
86
87
    def __eq__(self, other):
88
        return self.value() == other.value()
89
90
91
class GlancesThresholdOk(_GlancesThreshold):
92
93
    """Ok Threshold class"""
94
95
    _threshold = {'description': 'OK',
96
                  'value': 0}
97
98
99
class GlancesThresholdCareful(_GlancesThreshold):
100
101
    """Careful Threshold class"""
102
103
    _threshold = {'description': 'CAREFUL',
104
                  'value': 1}
105
106
107
class GlancesThresholdWarning(_GlancesThreshold):
108
109
    """Warning Threshold class"""
110
111
    _threshold = {'description': 'WARNING',
112
                  'value': 2}
113
114
115
class GlancesThresholdCritical(_GlancesThreshold):
116
117
    """Warning Threshold class"""
118
119
    _threshold = {'description': 'CRITICAL',
120
                  'value': 3}
121