psutil

Форк
0
/
battery.py 
50 строк · 1.1 Кб
1
#!/usr/bin/env python3
2

3
# Copyright (c) 2009, Giampaolo Rodola'. All rights reserved.
4
# Use of this source code is governed by a BSD-style license that can be
5
# found in the LICENSE file.
6

7
"""Show battery information.
8

9
$ python3 scripts/battery.py
10
charge:     74%
11
left:       2:11:31
12
status:     discharging
13
plugged in: no
14
"""
15

16
from __future__ import print_function
17

18
import sys
19

20
import psutil
21

22

23
def secs2hours(secs):
24
    mm, ss = divmod(secs, 60)
25
    hh, mm = divmod(mm, 60)
26
    return "%d:%02d:%02d" % (hh, mm, ss)
27

28

29
def main():
30
    if not hasattr(psutil, "sensors_battery"):
31
        return sys.exit("platform not supported")
32
    batt = psutil.sensors_battery()
33
    if batt is None:
34
        return sys.exit("no battery is installed")
35

36
    print("charge:     %s%%" % round(batt.percent, 2))
37
    if batt.power_plugged:
38
        print(
39
            "status:     %s"
40
            % ("charging" if batt.percent < 100 else "fully charged")
41
        )
42
        print("plugged in: yes")
43
    else:
44
        print("left:       %s" % secs2hours(batt.secsleft))
45
        print("status:     %s" % "discharging")
46
        print("plugged in: no")
47

48

49
if __name__ == '__main__':
50
    main()
51

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.