glances_batpercent.py 3.3 KB
Newer Older
A
Alessio Sergi 已提交
1 2
# -*- coding: utf-8 -*-
#
3
# This file is part of Glances.
A
Alessio Sergi 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#
# Copyright (C) 2014 Nicolargo <nicolas@nicolargo.com>
#
# Glances is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Glances is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
A
PEP 257  
Alessio Sergi 已提交
19 20

"""Battery plugin."""
A
Alessio Sergi 已提交
21

N
Nicolargo 已提交
22 23 24 25
# Import Glances libs
from glances.core.glances_globals import logger
from glances.plugins.glances_plugin import GlancesPlugin

A
Alessio Sergi 已提交
26
# Batinfo library (optional; Linux-only)
A
Alessio Sergi 已提交
27 28
try:
    import batinfo
N
Nicolas Hennion 已提交
29
except ImportError:
A
Alessio Sergi 已提交
30 31
    logger.debug("Batinfo library not found. Glances cannot grab battery info.")
    pass
A
Alessio Sergi 已提交
32 33 34


class Plugin(GlancesPlugin):
A
PEP 257  
Alessio Sergi 已提交
35 36

    """Glances' battery capacity plugin.
A
Alessio Sergi 已提交
37 38 39 40

    stats is a list
    """

41
    def __init__(self, args=None):
A
PEP 257  
Alessio Sergi 已提交
42
        """Init the plugin."""
43
        GlancesPlugin.__init__(self, args=args)
A
Alessio Sergi 已提交
44 45

        # Init the sensor class
A
Alessio Sergi 已提交
46
        self.glancesgrabbat = GlancesGrabBat()
A
Alessio Sergi 已提交
47

N
Nicolargo 已提交
48 49 50 51
        # We do not want to display the stat in a dedicated area
        # The HDD temp is displayed within the sensors plugin
        self.display_curse = False

52
        # Init stats
A
Alessio Sergi 已提交
53
        self.reset()
54 55

    def reset(self):
A
PEP 257  
Alessio Sergi 已提交
56
        """Reset/init the stats."""
57 58
        self.stats = []

59
    def update(self):
A
PEP 257  
Alessio Sergi 已提交
60
        """Update battery capacity stats using the input method."""
61 62 63
        # Reset stats
        self.reset()

64
        if self.get_input() == 'local':
N
Nicolargo 已提交
65 66
            # Update stats
            self.glancesgrabbat.update()
N
Nicolargo 已提交
67
            self.stats = self.glancesgrabbat.get()
68

69
        elif self.get_input() == 'snmp':
70 71 72
            # Update stats using SNMP
            # Not avalaible
            pass
A
Alessio Sergi 已提交
73

74
        return self.stats
A
Alessio Sergi 已提交
75

A
Alessio Sergi 已提交
76

A
Alessio Sergi 已提交
77
class GlancesGrabBat(object):
A
PEP 257  
Alessio Sergi 已提交
78 79

    """Get batteries stats using the batinfo library."""
A
Alessio Sergi 已提交
80 81

    def __init__(self):
A
PEP 257  
Alessio Sergi 已提交
82
        """Init batteries stats."""
A
Alessio Sergi 已提交
83 84 85
        try:
            self.bat = batinfo.batteries()
            self.initok = True
N
Nicolas Hennion 已提交
86
            self.bat_list = []
87
            self.update()
88
        except Exception as e:
A
Alessio Sergi 已提交
89
            self.initok = False
A
Alessio Sergi 已提交
90
            logger.debug("Cannot init GlancesGrabBat class (%s)" % e)
A
Alessio Sergi 已提交
91

92
    def update(self):
A
PEP 257  
Alessio Sergi 已提交
93
        """Update the stats."""
A
Alessio Sergi 已提交
94
        if self.initok:
95
            self.bat.update()
N
Nicolargo 已提交
96
            self.bat_list = [{'label': _("Battery (%)"), 'value': self.getcapacitypercent()}]
A
Alessio Sergi 已提交
97 98 99 100
        else:
            self.bat_list = []

    def get(self):
A
PEP 257  
Alessio Sergi 已提交
101
        """Get the stats."""
A
Alessio Sergi 已提交
102 103 104
        return self.bat_list

    def getcapacitypercent(self):
A
PEP 257  
Alessio Sergi 已提交
105
        """Get batteries capacity percent."""
D
desbma 已提交
106
        if not self.initok or not self.bat.stat:
A
Alessio Sergi 已提交
107
            return []
N
Nicolargo 已提交
108

N
Nicolargo 已提交
109
        # Init the bsum (sum of percent)
A
Alessio Sergi 已提交
110 111
        # and Loop over batteries (yes a computer could have more than 1 battery)
        bsum = 0
N
Nicolargo 已提交
112
        for b in self.bat.stat:
A
Alessio Sergi 已提交
113
            try:
N
Nicolargo 已提交
114
                bsum = bsum + int(b.capacity)
A
Alessio Sergi 已提交
115 116
            except ValueError:
                return []
N
Nicolargo 已提交
117

A
Alessio Sergi 已提交
118
        # Return the global percent
N
Nicolargo 已提交
119
        return int(bsum / len(self.bat.stat))