glances_batpercent.py 3.2 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:
N
Nicolargo 已提交
30
    logger.debug(_("Cannot grab battery sensor. Missing BatInfo library."))
A
Alessio Sergi 已提交
31 32 33


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

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

    stats is a list
    """

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

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

N
Nicolargo 已提交
47 48 49 50
        # 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

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

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

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

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

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

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

A
Alessio Sergi 已提交
75

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

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

    def __init__(self):
A
PEP 257  
Alessio Sergi 已提交
81
        """Init batteries stats."""
A
Alessio Sergi 已提交
82 83 84
        try:
            self.bat = batinfo.batteries()
            self.initok = True
N
Nicolas Hennion 已提交
85
            self.bat_list = []
86
            self.update()
A
Alessio Sergi 已提交
87
        except Exception:
A
Alessio Sergi 已提交
88 89
            self.initok = False

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

    def get(self):
A
PEP 257  
Alessio Sergi 已提交
98
        """Get the stats."""
A
Alessio Sergi 已提交
99 100 101
        return self.bat_list

    def getcapacitypercent(self):
A
PEP 257  
Alessio Sergi 已提交
102
        """Get batteries capacity percent."""
N
Nicolargo 已提交
103
        if not self.initok or self.bat.stat == []:
A
Alessio Sergi 已提交
104
            return []
N
Nicolargo 已提交
105

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

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