Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
Power
=====

Crossplatform (Windows, Linux, Mac OS X, FreeBSD) python module to access power capabilities of the system.
Linux python module to access power capabilities of the system.

- Power source type: AC, Battery or UPS
- Battery warning level: no warning (None), less than 22% of battery (Early), less than 10min (Final)
- Time remaining estimate
- AC system status report: status code, time remaining to full/empty whether system is charging or discharging, battery capacity.
- Fault tolerant: if for some reason power capabilities cannot be extracted, falls back to AC
- Support for multiple battries
- Power changes can be observed (Mac OS X only for now)
- Very easy to extand to support new features or new systems
- Support for multiple battries ( stats reported capacity is average of batteries in system with more than one battery)
- Very easy to extend to support new features or new systems
28 changes: 16 additions & 12 deletions power/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# coding=utf-8
"""
Provides crossplatform checking of current power source, battery warning level and battery time remaining estimate.
Provides checking of current power source, battery warning level, ac status, battery
time remaining estimate and battery capacity.
Allows you to add observer for power notifications if platform supports it.

Usage:
Expand All @@ -16,24 +17,27 @@ def on_time_remaining_change(self, power_management):
# class Observer(object):
# ...
# PowerManagementObserver.register(Observer)

Another example:
import power

p = power.PowerManagement()

ac_status, time_remaining, bat_capacity = p.get_ac_status()

print('AC system status :', ac_status)
print('Time remaining :', time_remaining, 'minutes')
print('Capacity of batteries :', bat_capacity, '\n%')

"""
__version__ = '1.4'
__version__ = '1.5'

from sys import platform
from power.common import *


try:
if platform.startswith('darwin'):
from power.darwin import PowerManagement
elif platform.startswith('freebsd'):
from power.freebsd import PowerManagement
elif platform.startswith('win32'):
from power.win32 import PowerManagement
elif platform.startswith('linux'):
from power.linux import PowerManagement
else:
raise RuntimeError("{platform} is not supported.".format(platform=platform))
from power.linux import PowerManagement
except RuntimeError as e:
import warnings
warnings.warn("Unable to load PowerManagement for {platform}. No-op PowerManagement class is used: {error}".format(error=str(e), platform=platform))
Expand Down
61 changes: 53 additions & 8 deletions power/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,17 @@
@var TIME_REMAINING_UNLIMITED: Indicates that the system is connected to an external power source, without time limit.
@type TIME_REMAINING_UNKNOWN: float
@type TIME_REMAINING_UNLIMITED: float

@type STATUS_UNKNOWN: int
@type STATUS_AC: int
@type STATUS_CHARGING: int
@type STATUS_DISCHARGING: int
@type STATUS_FULL: int
@type STATUS_FULL_ONAC: int

"""
__author__ = 'kulakov.ilya@gmail.com'
__maintainer__ = 'oskari.rauta@gmail.com'

from abc import ABCMeta, abstractmethod
import weakref
Expand All @@ -39,6 +48,12 @@
'LOW_BATTERY_WARNING_FINAL',
'TIME_REMAINING_UNKNOWN',
'TIME_REMAINING_UNLIMITED',
'STATUS_UNKNOWN',
'STATUS_AC',
'STATUS_CHARGING',
'STATUS_DISCHARGING',
'STATUS_FULL',
'STATUS_FULL_ONAC',
'PowerManagementObserver'
]

Expand All @@ -61,6 +76,17 @@

TIME_REMAINING_UNLIMITED = -2.0

STATUS_UNKNOWN = 0

STATUS_AC = 1

STATUS_CHARGING = 2

STATUS_DISCHARGING = 3

STATUS_FULL = 4

STATUS_FULL_ONAC = 5

class PowerManagementBase(object):
"""
Expand Down Expand Up @@ -101,13 +127,32 @@ def get_low_battery_warning_level(self):
pass

@abstractmethod
def get_time_remaining_estimate(self):
def get_ac_status(self):
"""
Returns the estimated minutes remaining until all power sources (battery and/or UPS) are empty.
Looks through all power sources.

Returns AC's current status, time remaining in minutes for all power sources either to
empty or to full, whether system is charging or not and average of capacity on all
batteries (always 100 on AC only system).

@return: Special values for status
- STATUS_UNKNOWN (0 - on error)
- STATUS_AC (1 - AC only system)
- STATUS_CHARGING (2)
- STATUS_DISCHARGING (3)
- STATUS_FULL (4 - batteries are full, system not on AC)
- STATUS_FULL_ONAC (5 - batteries are full, system is on AC)

@rtype: int

@return: Special values for time remaining
- TIME_REMAINING_UNKNOWN (-1.0)
- TIME_REMAINING_UNLIMITED (-2.0)

@rtype: float

@return: capacity

@return: Special values:
- TIME_REMAINING_UNKNOWN
- TIME_REMAINING_UNLIMITED
@rtype: float
"""
pass
Expand Down Expand Up @@ -182,11 +227,11 @@ def get_low_battery_warning_level(self):
"""
return LOW_BATTERY_WARNING_NONE

def get_time_remaining_estimate(self):
def get_ac_status(self):
"""
@return: Always TIME_REMAINING_UNLIMITED
@return: Always STATUS_AC, TIME_REMAINING_UNLIMITED, 100
"""
return TIME_REMAINING_UNLIMITED
return STATUS_AC, TIME_REMAINING_UNLIMITED, 100

def add_observer(self, observer):
"""
Expand Down
Loading