glances_batpercent.py 2.7 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/>.
N
Nicolas Hennion 已提交
19 20 21
"""
Batinfo (batterie) plugin
"""
A
Alessio Sergi 已提交
22 23 24 25 26

# Import system libs
# batinfo library (optional; Linux-only)
try:
    import batinfo
N
Nicolas Hennion 已提交
27
except ImportError:
A
Alessio Sergi 已提交
28 29
    pass

N
Nicolas Hennion 已提交
30 31
# Import Glances libs
from glances.plugins.glances_plugin import GlancesPlugin
A
Alessio Sergi 已提交
32 33 34 35 36 37 38 39 40 41 42 43


class Plugin(GlancesPlugin):
    """
    Glances's batterie capacity Plugin

    stats is a list
    """

    def __init__(self):
        GlancesPlugin.__init__(self)

N
Nicolas Hennion 已提交
44 45
        #!!! TODO: display plugin...

A
Alessio Sergi 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
        # Init the sensor class
        self.glancesgrabbat = glancesGrabBat()

    def update(self):
        """
        Update batterie capacity stats
        """

        self.stats = self.glancesgrabbat.getcapacitypercent()


class glancesGrabBat:
    """
    Get batteries stats using the Batinfo librairie
    """

    def __init__(self):
        """
        Init batteries stats
        """
        try:
            self.bat = batinfo.batteries()
            self.initok = True
N
Nicolas Hennion 已提交
69
            self.bat_list = []
A
Alessio Sergi 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
            self.__update__()
        except Exception:
            self.initok = False

    def __update__(self):
        """
        Update the stats
        """
        if self.initok:
            try:
                self.bat.update()
            except Exception:
                self.bat_list = []
            else:
                self.bat_list = self.bat.stat
        else:
            self.bat_list = []

    def get(self):
        # Update the stats
        self.__update__()
        return self.bat_list

    def getcapacitypercent(self):
        if not self.initok or self.bat_list == []:
            return []
        # Init the bsum (sum of percent) and bcpt (number of batteries)
        # and Loop over batteries (yes a computer could have more than 1 battery)
        bsum = 0
        for bcpt in range(len(self.get())):
            try:
                bsum = bsum + int(self.bat_list[bcpt].capacity)
            except ValueError:
                return []
N
Nicolas Hennion 已提交
104
            bcpt = bcpt + 1
A
Alessio Sergi 已提交
105 106
        # Return the global percent
        return int(bsum / bcpt)