├── .gitignore ├── .travis.yml ├── AUTHORS ├── LICENSE ├── MANIFEST.in ├── NEWS ├── README.fr ├── README.md ├── batinfo ├── __init__.py └── battery.py ├── setup.py ├── test ├── AC │ ├── online │ ├── power │ │ ├── autosuspend_delay_ms │ │ ├── control │ │ ├── runtime_active_time │ │ ├── runtime_status │ │ └── runtime_suspended_time │ ├── type │ └── uevent ├── BAT1 │ ├── alarm │ ├── capacity │ ├── charge_full │ ├── charge_full_design │ ├── charge_now │ ├── current_now │ ├── cycle_count │ ├── manufacturer │ ├── model_name │ ├── present │ ├── serial_number │ ├── status │ ├── technology │ ├── type │ ├── uevent │ ├── voltage_min_design │ └── voltage_now └── battery │ ├── alarm │ ├── capacity │ ├── charge_full │ ├── charge_full_design │ ├── charge_now │ ├── current_now │ ├── cycle_count │ ├── manufacturer │ ├── model_name │ ├── present │ ├── serial_number │ ├── status │ ├── technology │ ├── type │ ├── uevent │ ├── voltage_min_design │ └── voltage_now ├── test_batinfo.py └── uninstall.sh /.gitignore: -------------------------------------------------------------------------------- 1 | *.py[cod] 2 | 3 | # C extensions 4 | *.so 5 | 6 | # Packages 7 | *.egg 8 | *.egg-info 9 | dist 10 | build 11 | eggs 12 | parts 13 | bin 14 | var 15 | sdist 16 | develop-eggs 17 | .installed.cfg 18 | lib 19 | lib64 20 | 21 | # Installer logs 22 | pip-log.txt 23 | 24 | # Unit test / coverage reports 25 | .coverage 26 | .tox 27 | nosetests.xml 28 | 29 | # Translations 30 | *.mo 31 | 32 | # Mr Developer 33 | .mr.developer.cfg 34 | .project 35 | .pydevproject 36 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: python 2 | python: 3 | - "2.6" 4 | - "2.7" 5 | - "3.2" 6 | - "3.3" 7 | install: 8 | - pip install --use-mirrors 9 | script: python setup.py install 10 | -------------------------------------------------------------------------------- /AUTHORS: -------------------------------------------------------------------------------- 1 | ========== 2 | Developers 3 | ========== 4 | 5 | Nicolas Hennion (aka) Nicolargo 6 | http://blog.nicolargo.com 7 | https://twitter.com/nicolargo 8 | nicolashennion@gmail.com 9 | PGP Fingerprint: 835F C447 3BCD 60E9 9200 2778 ABA4 D1AB 9731 6A3C 10 | PGP Public key: gpg --keyserver pgp.mit.edu --recv-keys 0xaba4d1ab97316a3c 11 | 12 | ============ 13 | Contributors 14 | ============ 15 | 16 | Alexis Metaireau / https://github.com/ametaireau 17 | 18 | ========= 19 | Packagers 20 | ========= 21 | 22 | Philip Lacroix (philnx@posteo.de) for the Slackware (SlackBuild) package 23 | @fabaff for the Fedora package 24 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU LESSER GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | 9 | This version of the GNU Lesser General Public License incorporates 10 | the terms and conditions of version 3 of the GNU General Public 11 | License, supplemented by the additional permissions listed below. 12 | 13 | 0. Additional Definitions. 14 | 15 | As used herein, "this License" refers to version 3 of the GNU Lesser 16 | General Public License, and the "GNU GPL" refers to version 3 of the GNU 17 | General Public License. 18 | 19 | "The Library" refers to a covered work governed by this License, 20 | other than an Application or a Combined Work as defined below. 21 | 22 | An "Application" is any work that makes use of an interface provided 23 | by the Library, but which is not otherwise based on the Library. 24 | Defining a subclass of a class defined by the Library is deemed a mode 25 | of using an interface provided by the Library. 26 | 27 | A "Combined Work" is a work produced by combining or linking an 28 | Application with the Library. The particular version of the Library 29 | with which the Combined Work was made is also called the "Linked 30 | Version". 31 | 32 | The "Minimal Corresponding Source" for a Combined Work means the 33 | Corresponding Source for the Combined Work, excluding any source code 34 | for portions of the Combined Work that, considered in isolation, are 35 | based on the Application, and not on the Linked Version. 36 | 37 | The "Corresponding Application Code" for a Combined Work means the 38 | object code and/or source code for the Application, including any data 39 | and utility programs needed for reproducing the Combined Work from the 40 | Application, but excluding the System Libraries of the Combined Work. 41 | 42 | 1. Exception to Section 3 of the GNU GPL. 43 | 44 | You may convey a covered work under sections 3 and 4 of this License 45 | without being bound by section 3 of the GNU GPL. 46 | 47 | 2. Conveying Modified Versions. 48 | 49 | If you modify a copy of the Library, and, in your modifications, a 50 | facility refers to a function or data to be supplied by an Application 51 | that uses the facility (other than as an argument passed when the 52 | facility is invoked), then you may convey a copy of the modified 53 | version: 54 | 55 | a) under this License, provided that you make a good faith effort to 56 | ensure that, in the event an Application does not supply the 57 | function or data, the facility still operates, and performs 58 | whatever part of its purpose remains meaningful, or 59 | 60 | b) under the GNU GPL, with none of the additional permissions of 61 | this License applicable to that copy. 62 | 63 | 3. Object Code Incorporating Material from Library Header Files. 64 | 65 | The object code form of an Application may incorporate material from 66 | a header file that is part of the Library. You may convey such object 67 | code under terms of your choice, provided that, if the incorporated 68 | material is not limited to numerical parameters, data structure 69 | layouts and accessors, or small macros, inline functions and templates 70 | (ten or fewer lines in length), you do both of the following: 71 | 72 | a) Give prominent notice with each copy of the object code that the 73 | Library is used in it and that the Library and its use are 74 | covered by this License. 75 | 76 | b) Accompany the object code with a copy of the GNU GPL and this license 77 | document. 78 | 79 | 4. Combined Works. 80 | 81 | You may convey a Combined Work under terms of your choice that, 82 | taken together, effectively do not restrict modification of the 83 | portions of the Library contained in the Combined Work and reverse 84 | engineering for debugging such modifications, if you also do each of 85 | the following: 86 | 87 | a) Give prominent notice with each copy of the Combined Work that 88 | the Library is used in it and that the Library and its use are 89 | covered by this License. 90 | 91 | b) Accompany the Combined Work with a copy of the GNU GPL and this license 92 | document. 93 | 94 | c) For a Combined Work that displays copyright notices during 95 | execution, include the copyright notice for the Library among 96 | these notices, as well as a reference directing the user to the 97 | copies of the GNU GPL and this license document. 98 | 99 | d) Do one of the following: 100 | 101 | 0) Convey the Minimal Corresponding Source under the terms of this 102 | License, and the Corresponding Application Code in a form 103 | suitable for, and under terms that permit, the user to 104 | recombine or relink the Application with a modified version of 105 | the Linked Version to produce a modified Combined Work, in the 106 | manner specified by section 6 of the GNU GPL for conveying 107 | Corresponding Source. 108 | 109 | 1) Use a suitable shared library mechanism for linking with the 110 | Library. A suitable mechanism is one that (a) uses at run time 111 | a copy of the Library already present on the user's computer 112 | system, and (b) will operate properly with a modified version 113 | of the Library that is interface-compatible with the Linked 114 | Version. 115 | 116 | e) Provide Installation Information, but only if you would otherwise 117 | be required to provide such information under section 6 of the 118 | GNU GPL, and only to the extent that such information is 119 | necessary to install and execute a modified version of the 120 | Combined Work produced by recombining or relinking the 121 | Application with a modified version of the Linked Version. (If 122 | you use option 4d0, the Installation Information must accompany 123 | the Minimal Corresponding Source and Corresponding Application 124 | Code. If you use option 4d1, you must provide the Installation 125 | Information in the manner specified by section 6 of the GNU GPL 126 | for conveying Corresponding Source.) 127 | 128 | 5. Combined Libraries. 129 | 130 | You may place library facilities that are a work based on the 131 | Library side by side in a single library together with other library 132 | facilities that are not Applications and are not covered by this 133 | License, and convey such a combined library under terms of your 134 | choice, if you do both of the following: 135 | 136 | a) Accompany the combined library with a copy of the same work based 137 | on the Library, uncombined with any other library facilities, 138 | conveyed under the terms of this License. 139 | 140 | b) Give prominent notice with the combined library that part of it 141 | is a work based on the Library, and explaining where to find the 142 | accompanying uncombined form of the same work. 143 | 144 | 6. Revised Versions of the GNU Lesser General Public License. 145 | 146 | The Free Software Foundation may publish revised and/or new versions 147 | of the GNU Lesser General Public License from time to time. Such new 148 | versions will be similar in spirit to the present version, but may 149 | differ in detail to address new problems or concerns. 150 | 151 | Each version is given a distinguishing version number. If the 152 | Library as you received it specifies that a certain numbered version 153 | of the GNU Lesser General Public License "or any later version" 154 | applies to it, you have the option of following the terms and 155 | conditions either of that published version or of any later version 156 | published by the Free Software Foundation. If the Library as you 157 | received it does not specify a version number of the GNU Lesser 158 | General Public License, you may choose any version of the GNU Lesser 159 | General Public License ever published by the Free Software Foundation. 160 | 161 | If the Library as you received it specifies that a proxy can decide 162 | whether future versions of the GNU Lesser General Public License shall 163 | apply, that proxy's public statement of acceptance of any version is 164 | permanent authorization for you to choose that version for the 165 | Library. 166 | -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | include AUTHORS 2 | include README.md 3 | include LICENSE 4 | include test_batinfo.py 5 | recursive-include test * 6 | -------------------------------------------------------------------------------- /NEWS: -------------------------------------------------------------------------------- 1 | * version 0.4.2 - Correct an issue on MANIFEST.in file 2 | * version 0.4.1 - Correct an issue on setup.py file 3 | * version 0.4 - Update for package manager 4 | * version 0.3 - PEP8 improvement 5 | * version 0.2 - No log on read file 6 | * version 0.1.9 - Correct an import issue with Python 3 7 | * Set log level to CRITICAL (by Nicolargo) 8 | * Merge pull request #3 from webplate/master (by Nicolas Hennion) 9 | * version 0.1.7 - Do not log if arg is not found (by Nicolas Hennion) 10 | * version 0.1.6 (by Nicolas Hennion) 11 | * version 0.1.5 (by Nicolas Hennion) 12 | * Correct issue #2 (by Nicolas Hennion) 13 | * version 0.1.4 / Correct setup script (by Nicolas Hennion) 14 | * version 0.1.3 (by Nicolas Hennion) 15 | * Add the MANIFEST.in file (by Nicolas Hennion) 16 | * Correct setup (by Nicolas Hennion) 17 | * version 0.1.2 (by Nicolas Hennion) 18 | * version 0.1.1 Correct setup.py (by Nicolas Hennion) 19 | -------------------------------------------------------------------------------- /README.fr: -------------------------------------------------------------------------------- 1 | BatInfo 2 | ======= 3 | 4 | Je souhaitais ajouter dans Glances un plugin permettant de superviser l'état des batteries. J'ai donc commencé à chercher une librairie Python permettant de s'acquiter le plus simplement possible de cette tache qui sous un système GNU/Linux consiste à analyser le répertoire /sys/class/power_supply maintenu à jour par le noyau Linux. 5 | Comme je n'ai pas trouvé mon bonheur, j'ai donc décidé de développer un librairie Python: BatInfo. 6 | 7 | Les sources de cette librairie sont disponible sur Github en licence LGPL. L'installation sur votre système peut se faire simplement via la librairie Pypi: 8 | 9 | sudo pip install batinfo 10 | 11 | L'utilisation de la librairie dans vos développement Python est la suivante: 12 | 13 | On commence par inclure la librairie: 14 | 15 | import batinfo 16 | 17 | Puis on créé une instance (bat) de la classe principale (batteries): 18 | 19 | bat = batinfo.Batteries() 20 | 21 | On peut récupérer les données brutes (format JSON) en utilisant: 22 | 23 | bat.stat 24 | [{"status": "Full", "capacity": 50, "name": "CMB1", "uevent": "POWER_SUPPLY_NAME=CMB1\nPOWER_SUPPLY_STATUS=Full\nPOWER_SUPPLY_PRESENT=1\nPOWER_SUPPLY_TECHNOLOGY=Li-ion\nPOWER_SUPPLY_CYCLE_COUNT=0\nPOWER_SUPPLY_VOLTAGE_MIN_DESIGN=10800000\nPOWER_SUPPLY_VOLTAGE_NOW=12496000\nPOWER_SUPPLY_CURRENT_NOW=0\nPOWER_SUPPLY_CHARGE_FULL_DESIGN=5800000\nPOWER_SUPPLY_CHARGE_FULL=5800000\nPOWER_SUPPLY_CHARGE_NOW=3900000\nPOWER_SUPPLY_CAPACITY=100\nPOWER_SUPPLY_MODEL_NAME=CP293550-01\nPOWER_SUPPLY_MANUFACTURER=Fujitsu\nPOWER_SUPPLY_SERIAL_NUMBER=01A-Z100320001158Z", "alarm": 0, "charge_full": 5800000, "voltage_now": 12496000, "serial_number": "01A-Z100320001158Z", "cycle_count": 0, "current_now": 0, "charge_now": 3900000, "voltage_min_design": 10800000, "path": "/sys/class/power_supply/CMB1", "technology": "Li-ion", "manufacturer": "Fujitsu", "type": "Battery", "model_name": "CP293550-01", "present": 1, "charge_full_design": 5800000}] 25 | 26 | Les données brutes se présentent sous la forme d'une liste de dictionnaire (un dictionnaire par batterie présente sur votre système). Comme la plupart du temps les machines (portable) ont une seule batterie, on peut avoir le dictionnaire associé à cette première batterie avec: 27 | 28 | bat.stat[0] 29 | {"status": "Full", "capacity": 100, "name": "CMB1", "uevent": "POWER_SUPPLY_NAME=CMB1\nPOWER_SUPPLY_STATUS=Full\nPOWER_SUPPLY_PRESENT=1\nPOWER_SUPPLY_TECHNOLOGY=Li-ion\nPOWER_SUPPLY_CYCLE_COUNT=0\nPOWER_SUPPLY_VOLTAGE_MIN_DESIGN=10800000\nPOWER_SUPPLY_VOLTAGE_NOW=12496000\nPOWER_SUPPLY_CURRENT_NOW=0\nPOWER_SUPPLY_CHARGE_FULL_DESIGN=5800000\nPOWER_SUPPLY_CHARGE_FULL=5800000\nPOWER_SUPPLY_CHARGE_NOW=3900000\nPOWER_SUPPLY_CAPACITY=100\nPOWER_SUPPLY_MODEL_NAME=CP293550-01\nPOWER_SUPPLY_MANUFACTURER=Fujitsu\nPOWER_SUPPLY_SERIAL_NUMBER=01A-Z100320001158Z", "alarm": 0, "charge_full": 5800000, "voltage_now": 12496000, "serial_number": "01A-Z100320001158Z", "cycle_count": 0, "current_now": 0, "charge_now": 3900000, "voltage_min_design": 10800000, "path": "/sys/class/power_supply/CMB1", "technology": "Li-ion", "manufacturer": "Fujitsu", "type": "Battery", "model_name": "CP293550-01", "present": 1, "charge_full_design": 5800000} 30 | 31 | Les statistiques présentes dans le dictionnaire dépende de votre batterie. Mais on retrouve un certain nombre d'informations génériques comme par exemple la capacité restante (en %): 32 | 33 | bat.stat[0].capacity 34 | 50 35 | 36 | la capacité maximale: 37 | 38 | bat.stat[0].charge_full 39 | 5800000 40 | 41 | ou encore la capacité courante: 42 | 43 | bat.stat[0].charge_now 44 | 3900000 45 | 46 | On peut aussi avoir des informations constructeurs: 47 | 48 | bat.stat[0].manufacturer 49 | 'Fujitsu' 50 | bat.stat[0].technology 51 | 'Li-ion' 52 | 53 | Pour mettre à jour les stats: 54 | 55 | bat.update() 56 | 57 | En espérant que cette librairie soit utile à certains. Il ne me reste plus, pour ma part, qu'à intégrer cela dans Glances. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | BatInfo 2 | ======= 3 | 4 | A simple Python module to retrieve battery information on Linux-based operating system. 5 | No ACPI or external software is needed. 6 | Only the Linux kernel and its `/sys/class/power_supply` folder. 7 | 8 | A simple example says more than thousand words: 9 | 10 | In [1]: import batinfo 11 | In [2]: bat = batinfo.Batteries() 12 | In [3]: bat 13 | Out[3]: 14 | In [4]: bat.stat 15 | Out[4]: [{"status": "Full", "capacity": 50, "name": "CMB1", "uevent": "POWER_SUPPLY_NAME=CMB1\nPOWER_SUPPLY_STATUS=Full\nPOWER_SUPPLY_PRESENT=1\nPOWER_SUPPLY_TECHNOLOGY=Li-ion\nPOWER_SUPPLY_CYCLE_COUNT=0\nPOWER_SUPPLY_VOLTAGE_MIN_DESIGN=10800000\nPOWER_SUPPLY_VOLTAGE_NOW=12496000\nPOWER_SUPPLY_CURRENT_NOW=0\nPOWER_SUPPLY_CHARGE_FULL_DESIGN=5800000\nPOWER_SUPPLY_CHARGE_FULL=5800000\nPOWER_SUPPLY_CHARGE_NOW=3900000\nPOWER_SUPPLY_CAPACITY=100\nPOWER_SUPPLY_MODEL_NAME=CP293550-01\nPOWER_SUPPLY_MANUFACTURER=Fujitsu\nPOWER_SUPPLY_SERIAL_NUMBER=01A-Z100320001158Z", "alarm": 0, "charge_full": 5800000, "voltage_now": 12496000, "serial_number": "01A-Z100320001158Z", "cycle_count": 0, "current_now": 0, "charge_now": 3900000, "voltage_min_design": 10800000, "path": "/sys/class/power_supply/CMB1", "technology": "Li-ion", "manufacturer": "Fujitsu", "type": "Battery", "model_name": "CP293550-01", "present": 1, "charge_full_design": 5800000}] 16 | In [6]: bat.stat[0] 17 | Out[6]: {"status": "Full", "capacity": 100, "name": "CMB1", "uevent": "POWER_SUPPLY_NAME=CMB1\nPOWER_SUPPLY_STATUS=Full\nPOWER_SUPPLY_PRESENT=1\nPOWER_SUPPLY_TECHNOLOGY=Li-ion\nPOWER_SUPPLY_CYCLE_COUNT=0\nPOWER_SUPPLY_VOLTAGE_MIN_DESIGN=10800000\nPOWER_SUPPLY_VOLTAGE_NOW=12496000\nPOWER_SUPPLY_CURRENT_NOW=0\nPOWER_SUPPLY_CHARGE_FULL_DESIGN=5800000\nPOWER_SUPPLY_CHARGE_FULL=5800000\nPOWER_SUPPLY_CHARGE_NOW=3900000\nPOWER_SUPPLY_CAPACITY=100\nPOWER_SUPPLY_MODEL_NAME=CP293550-01\nPOWER_SUPPLY_MANUFACTURER=Fujitsu\nPOWER_SUPPLY_SERIAL_NUMBER=01A-Z100320001158Z", "alarm": 0, "charge_full": 5800000, "voltage_now": 12496000, "serial_number": "01A-Z100320001158Z", "cycle_count": 0, "current_now": 0, "charge_now": 3900000, "voltage_min_design": 10800000, "path": "/sys/class/power_supply/CMB1", "technology": "Li-ion", "manufacturer": "Fujitsu", "type": "Battery", "model_name": "CP293550-01", "present": 1, "charge_full_design": 5800000} 18 | In [7]: bat.stat[0].capacity 19 | Out[7]: 50 20 | In [8]: print bat.stat[0] 21 | 100 22 | In [9]: bat.stat[0].manufacturer 23 | Out[9]: 'Fujitsu' 24 | In [9]: bat.stat[0].technology 25 | Out[9]: 'Li-ion' 26 | In [11]: bat.stat[0].charge_full 27 | Out[11]: 5800000 28 | In [12]: bat.stat[0].charge_now 29 | Out[12]: 3900000 30 | In [12]: bat.update() 31 | > Refresh the stats 32 | 33 | Have fun... 34 | -------------------------------------------------------------------------------- /batinfo/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # BatInfo 4 | # A simple Python lib to retrieve battery information 5 | # 6 | # Copyright (C) 2016 Nicolargo 7 | # 8 | # BatInfo is free software; you can redistribute it and/or modify 9 | # it under the terms of the GNU Lesser General Public License as published by 10 | # the Free Software Foundation, either version 3 of the License, or 11 | # (at your option) any later version. 12 | # 13 | # BatInfo is distributed in the hope that it will be useful, 14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | # GNU Lesser General Public License for more details. 17 | # 18 | # You should have received a copy of the GNU Lesser General Public License 19 | # along with this program. If not, see . 20 | 21 | __appname__ = "batinfo" 22 | __version__ = "0.4.2" 23 | __author__ = "Nicolas Hennion " 24 | __licence__ = "LGPLv3" 25 | 26 | __all__ = ['batteries', 'battery', 'Batteries', 'Battery'] 27 | 28 | from batinfo.battery import Battery, Batteries 29 | 30 | # Let the old names for compatibility 31 | battery = Battery 32 | batteries = Batteries 33 | -------------------------------------------------------------------------------- /batinfo/battery.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # Batinfo 4 | # A simple Python lib to retrieve battery information 5 | # 6 | # Copyright (C) 2014 Nicolargo 7 | # 8 | # BatInfo is free software; you can redistribute it and/or modify 9 | # it under the terms of the GNU Lesser General Public License as published by 10 | # the Free Software Foundation, either version 3 of the License, or 11 | # (at your option) any later version. 12 | # 13 | # BatInfo is distributed in the hope that it will be useful, 14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | # GNU Lesser General Public License for more details. 17 | # 18 | # You should have received a copy of the GNU Lesser General Public License 19 | # along with this program. If not, see . 20 | 21 | import json 22 | import logging 23 | import os 24 | 25 | logging.basicConfig() 26 | log = logging.getLogger(__name__) 27 | log.setLevel(logging.CRITICAL) 28 | 29 | SMAPI = '/sys/devices/platform/smapi' 30 | 31 | 32 | class Battery(object): 33 | """ 34 | Battery stats 35 | """ 36 | 37 | def __init__(self, path="/sys/class/power_supply", name="BAT0"): 38 | self.path = os.path.join(path, name) 39 | self.name = name 40 | self.__update__() 41 | 42 | def __str__(self): 43 | self.__update__() 44 | if 'capacity' in repr(self): 45 | return str(self.capacity) 46 | else: 47 | return "" 48 | 49 | def __repr__(self): 50 | self.__update__() 51 | return json.dumps(self, default=lambda o: o.__dict__) 52 | 53 | def __getattr__(self, stat): 54 | """ 55 | Catch message if attribute did not exist 56 | """ 57 | log.error("Attribute %s did not exist" % stat) 58 | return "" 59 | 60 | def __get_stat__(self, stat): 61 | """ 62 | Read stat from the Linux kernel 63 | """ 64 | try: 65 | # Check for thinkpad smapi (cycle count) 66 | f = '%s/%s/cycle_count' % (SMAPI, self.name) 67 | if 'cycle_count' in stat and os.path.isfile(f): 68 | return open(f).read().strip() 69 | with open(os.path.join(self.path, stat), 'r') as f: 70 | return f.read().strip() 71 | except Exception: 72 | # log.error("Can not read file %s" % stat) 73 | return "" 74 | 75 | def __update__(self): 76 | """ 77 | Update the stats 78 | """ 79 | # Get all file in the battery system folder 80 | stats = [f for f in os.listdir(self.path) 81 | if os.path.isfile(os.path.join(self.path, f))] 82 | for stat in stats: 83 | # ~ print("%s = %s" % (stat, self.__get_stat__(stat))) 84 | value = self.__get_stat__(stat) 85 | try: 86 | # Try to convert to integer 87 | value = int(value) 88 | except ValueError: 89 | # Not possible, not a problem 90 | pass 91 | setattr(self, stat, value) 92 | if ('capacity' not in stats and 93 | 'charge_full' in stats and 94 | 'charge_now' in stats): 95 | value = self.charge_now*100/self.charge_full 96 | setattr(self, 'capacity', value) 97 | 98 | 99 | class Batteries(object): 100 | """ 101 | Class to retreive stats of all the batteries 102 | List of battery (class) 103 | """ 104 | 105 | def __init__(self, bat_root_path="/sys/class/power_supply"): 106 | # Root path for batteries stats 107 | self.bat_root_path = bat_root_path 108 | # Update stat 109 | self.stat = [] 110 | self.update() 111 | 112 | def update(self): 113 | # Init the batteries stat list 114 | self.stat = [] 115 | # and update it... 116 | # Find all the batteries in the bat_root_path folder 117 | # It's a battery if the file "type" exist 118 | # and contain "Battery" 119 | for dirname in os.listdir(self.bat_root_path): 120 | type_file = os.path.join(self.bat_root_path, dirname, "type") 121 | if (os.path.isfile(type_file)): 122 | try: 123 | with open(type_file, 'r') as f: 124 | is_bat = (f.read().strip() == "Battery") 125 | except Exception: 126 | log.error("Can not read file %s" % type_file) 127 | if (is_bat): 128 | # It is a battery, let's add it to the list 129 | # print("Add the battery %s to the list" % dirname) 130 | self.stat.append(Battery(self.bat_root_path, dirname)) 131 | 132 | def __len__(self): 133 | return len(self.stat) 134 | 135 | def __iter__(self): 136 | return iter(self.stat) 137 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | # import os 4 | # import sys 5 | # import glob 6 | 7 | from setuptools import setup 8 | 9 | 10 | def get_data_files(): 11 | data_files = [ 12 | ('share/doc/batinfo', ['AUTHORS', 'README.md', 'LICENSE']) 13 | ] 14 | 15 | return data_files 16 | 17 | setup( 18 | name='batinfo', 19 | version='0.4.2', 20 | description="A simple Python module to retrieve battery information", 21 | author='Nicolas Hennion', 22 | author_email='nicolas@nicolargo.com', 23 | url='https://github.com/nicolargo/batinfo', 24 | license="LGPLv3", 25 | keywords="lib battery", 26 | packages=['batinfo'], 27 | include_package_data=True, 28 | data_files=get_data_files(), 29 | classifiers=[ 30 | 'Development Status :: 5 - Production/Stable', 31 | 'Environment :: Console', 32 | 'License :: OSI Approved :: GNU Lesser General Public License v3 (LGPLv3)', 33 | 'Operating System :: OS Independent', 34 | 'Natural Language :: English', 35 | 'Operating System :: OS Independent', 36 | 'Programming Language :: Python :: 2', 37 | 'Programming Language :: Python :: 3', 38 | 'Topic :: System :: Monitoring', 39 | 'Topic :: System :: Systems Administration', 40 | 'Topic :: Utilities', 41 | 'Topic :: Software Development :: Libraries :: Python Modules', 42 | 'Intended Audience :: Developers', 43 | 'Intended Audience :: System Administrators' 44 | ] 45 | ) 46 | -------------------------------------------------------------------------------- /test/AC/online: -------------------------------------------------------------------------------- 1 | 0 2 | -------------------------------------------------------------------------------- /test/AC/power/autosuspend_delay_ms: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nicolargo/batinfo/60f934948eede2485b023096b6e1f3c112541d5a/test/AC/power/autosuspend_delay_ms -------------------------------------------------------------------------------- /test/AC/power/control: -------------------------------------------------------------------------------- 1 | auto 2 | -------------------------------------------------------------------------------- /test/AC/power/runtime_active_time: -------------------------------------------------------------------------------- 1 | 0 2 | -------------------------------------------------------------------------------- /test/AC/power/runtime_status: -------------------------------------------------------------------------------- 1 | unsupported 2 | -------------------------------------------------------------------------------- /test/AC/power/runtime_suspended_time: -------------------------------------------------------------------------------- 1 | 0 2 | -------------------------------------------------------------------------------- /test/AC/type: -------------------------------------------------------------------------------- 1 | Mains 2 | -------------------------------------------------------------------------------- /test/AC/uevent: -------------------------------------------------------------------------------- 1 | POWER_SUPPLY_NAME=AC 2 | POWER_SUPPLY_ONLINE=0 3 | -------------------------------------------------------------------------------- /test/BAT1/alarm: -------------------------------------------------------------------------------- 1 | 0 2 | -------------------------------------------------------------------------------- /test/BAT1/capacity: -------------------------------------------------------------------------------- 1 | 34 2 | -------------------------------------------------------------------------------- /test/BAT1/charge_full: -------------------------------------------------------------------------------- 1 | 5800000 2 | -------------------------------------------------------------------------------- /test/BAT1/charge_full_design: -------------------------------------------------------------------------------- 1 | 5800000 2 | -------------------------------------------------------------------------------- /test/BAT1/charge_now: -------------------------------------------------------------------------------- 1 | 1972000 2 | -------------------------------------------------------------------------------- /test/BAT1/current_now: -------------------------------------------------------------------------------- 1 | 1366000 2 | -------------------------------------------------------------------------------- /test/BAT1/cycle_count: -------------------------------------------------------------------------------- 1 | 0 2 | -------------------------------------------------------------------------------- /test/BAT1/manufacturer: -------------------------------------------------------------------------------- 1 | Fujitsu 2 | -------------------------------------------------------------------------------- /test/BAT1/model_name: -------------------------------------------------------------------------------- 1 | CP293550-01 2 | -------------------------------------------------------------------------------- /test/BAT1/present: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /test/BAT1/serial_number: -------------------------------------------------------------------------------- 1 | 01A-Z100320001158Z 2 | -------------------------------------------------------------------------------- /test/BAT1/status: -------------------------------------------------------------------------------- 1 | Discharging 2 | -------------------------------------------------------------------------------- /test/BAT1/technology: -------------------------------------------------------------------------------- 1 | Li-ion 2 | -------------------------------------------------------------------------------- /test/BAT1/type: -------------------------------------------------------------------------------- 1 | Battery 2 | -------------------------------------------------------------------------------- /test/BAT1/uevent: -------------------------------------------------------------------------------- 1 | POWER_SUPPLY_NAME=CMB1 2 | POWER_SUPPLY_STATUS=Discharging 3 | POWER_SUPPLY_PRESENT=1 4 | POWER_SUPPLY_TECHNOLOGY=Li-ion 5 | POWER_SUPPLY_CYCLE_COUNT=0 6 | POWER_SUPPLY_VOLTAGE_MIN_DESIGN=10800000 7 | POWER_SUPPLY_VOLTAGE_NOW=10659000 8 | POWER_SUPPLY_CURRENT_NOW=1366000 9 | POWER_SUPPLY_CHARGE_FULL_DESIGN=5800000 10 | POWER_SUPPLY_CHARGE_FULL=5800000 11 | POWER_SUPPLY_CHARGE_NOW=1972000 12 | POWER_SUPPLY_CAPACITY=34 13 | POWER_SUPPLY_MODEL_NAME=CP293550-01 14 | POWER_SUPPLY_MANUFACTURER=Fujitsu 15 | POWER_SUPPLY_SERIAL_NUMBER=01A-Z100320001158Z 16 | -------------------------------------------------------------------------------- /test/BAT1/voltage_min_design: -------------------------------------------------------------------------------- 1 | 10800000 2 | -------------------------------------------------------------------------------- /test/BAT1/voltage_now: -------------------------------------------------------------------------------- 1 | 10659000 2 | -------------------------------------------------------------------------------- /test/battery/alarm: -------------------------------------------------------------------------------- 1 | 0 2 | -------------------------------------------------------------------------------- /test/battery/capacity: -------------------------------------------------------------------------------- 1 | 53 2 | -------------------------------------------------------------------------------- /test/battery/charge_full: -------------------------------------------------------------------------------- 1 | 5800000 2 | -------------------------------------------------------------------------------- /test/battery/charge_full_design: -------------------------------------------------------------------------------- 1 | 5800000 2 | -------------------------------------------------------------------------------- /test/battery/charge_now: -------------------------------------------------------------------------------- 1 | 3074000 2 | -------------------------------------------------------------------------------- /test/battery/current_now: -------------------------------------------------------------------------------- 1 | 972000 2 | -------------------------------------------------------------------------------- /test/battery/cycle_count: -------------------------------------------------------------------------------- 1 | 0 2 | -------------------------------------------------------------------------------- /test/battery/manufacturer: -------------------------------------------------------------------------------- 1 | Fujitsu 2 | -------------------------------------------------------------------------------- /test/battery/model_name: -------------------------------------------------------------------------------- 1 | CP293550-01 2 | -------------------------------------------------------------------------------- /test/battery/present: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /test/battery/serial_number: -------------------------------------------------------------------------------- 1 | 01A-Z100320001158Z 2 | -------------------------------------------------------------------------------- /test/battery/status: -------------------------------------------------------------------------------- 1 | Discharging 2 | -------------------------------------------------------------------------------- /test/battery/technology: -------------------------------------------------------------------------------- 1 | Li-ion 2 | -------------------------------------------------------------------------------- /test/battery/type: -------------------------------------------------------------------------------- 1 | Battery 2 | -------------------------------------------------------------------------------- /test/battery/uevent: -------------------------------------------------------------------------------- 1 | POWER_SUPPLY_NAME=CMB1 2 | POWER_SUPPLY_STATUS=Discharging 3 | POWER_SUPPLY_PRESENT=1 4 | POWER_SUPPLY_TECHNOLOGY=Li-ion 5 | POWER_SUPPLY_CYCLE_COUNT=0 6 | POWER_SUPPLY_VOLTAGE_MIN_DESIGN=10800000 7 | POWER_SUPPLY_VOLTAGE_NOW=11086000 8 | POWER_SUPPLY_CURRENT_NOW=972000 9 | POWER_SUPPLY_CHARGE_FULL_DESIGN=5800000 10 | POWER_SUPPLY_CHARGE_FULL=5800000 11 | POWER_SUPPLY_CHARGE_NOW=3074000 12 | POWER_SUPPLY_CAPACITY=53 13 | POWER_SUPPLY_MODEL_NAME=CP293550-01 14 | POWER_SUPPLY_MANUFACTURER=Fujitsu 15 | POWER_SUPPLY_SERIAL_NUMBER=01A-Z100320001158Z 16 | -------------------------------------------------------------------------------- /test/battery/voltage_min_design: -------------------------------------------------------------------------------- 1 | 10800000 2 | -------------------------------------------------------------------------------- /test/battery/voltage_now: -------------------------------------------------------------------------------- 1 | 11086000 2 | -------------------------------------------------------------------------------- /test_batinfo.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | # 4 | # BatInfo test suite 5 | # A simple Python lib to retreive battery information 6 | # 7 | # Copyright (C) 2013 Nicolargo 8 | # 9 | # BatInfo is free software; you can redistribute it and/or modify 10 | # it under the terms of the GNU Lesser General Public License as published by 11 | # the Free Software Foundation, either version 3 of the License, or 12 | # (at your option) any later version. 13 | # 14 | # BatInfo is distributed in the hope that it will be useful, 15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | # GNU Lesser General Public License for more details. 18 | # 19 | # You should have received a copy of the GNU Lesser General Public License 20 | # along with this program. If not, see . 21 | 22 | from batinfo import Batteries 23 | import unittest 24 | 25 | 26 | class TestBatInfo(unittest.TestCase): 27 | 28 | def setUp(self): 29 | self.bat = Batteries(bat_root_path="./test") 30 | 31 | def test_batinfo_get(self): 32 | self.assertTrue(len(self.bat) == 2) 33 | 34 | def test_batinfo_name_default(self): 35 | # print("Battery name: %s" % self.bat.stat[0].name) 36 | self.assertTrue(type(self.bat.stat[0].name) == str) 37 | self.assertTrue(self.bat.stat[0].name == "battery") 38 | 39 | def test_batinfo_capacity(self): 40 | # print("Battery capacity: %s" % self.bat.stat[0].capacity) 41 | self.assertTrue(type(self.bat.stat[0].capacity) == int) 42 | self.assertTrue(self.bat.stat[0].capacity == 53) 43 | 44 | def test_batinfo_charge_now(self): 45 | # print("Battery 2 charge_now: %s" % self.bat.stat[1].charge_now) 46 | self.assertTrue(type(self.bat.stat[1].charge_now) == int) 47 | self.assertTrue(self.bat.stat[1].charge_now == 1972000) 48 | 49 | def test_the_batteries_api_is_iterable(self): 50 | batteries_count = 0 51 | for bat in self.bat: 52 | batteries_count += 1 53 | self.assertEquals(batteries_count, 2) 54 | 55 | 56 | if __name__ == '__main__': 57 | unittest.main() 58 | -------------------------------------------------------------------------------- /uninstall.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | if [ $(id -u) -ne 0 ]; then 4 | echo -e "* ERROR: User $(whoami) is not root, and does not have sudo privileges" 5 | exit 1 6 | fi 7 | 8 | if [ ! -f "setup.py" ]; then 9 | echo -e "* ERROR: Setup file doesn't exist" 10 | exit 1 11 | fi 12 | 13 | 14 | 15 | python setup.py install --record install.record 16 | 17 | for i in $(cat install.record); do 18 | rm $i 19 | done 20 | 21 | echo -e "\n\n* SUCCESS: Uninstall complete." 22 | rm install.record 23 | --------------------------------------------------------------------------------