[docs]@serviceclassPowerManager():""" Utility class for power management. """def__init__(self,context):self.context=contextdef__parse_acpi_value(self,v):""" Extract the right energy value. :param v: Energy value with unit, e.g. 10 wh :type v: string :return: Only the value :rtype: float """value,unit=v.split()value=float(value)ifunit.startswith('m'):value/=1000returnvalue
[docs]defget_batteries(self):""" Parse directory /proc/acpi/battery for systems running on battery. :return: All batteries informations :rtype: list of dict """batteries=[]battery_dir='/proc/acpi/battery'ifnotos.path.exists(battery_dir):return[]fordinsorted(os.listdir(battery_dir)):props={}forpathin[os.path.join(battery_dir,d,'info'),os.path.join(battery_dir,d,'state'),]:forlinopen(path).read().splitlines():if':'inl:key,value=l.strip().split(':')props[key]=value.strip()battery={'name':d,'designCapacity':self.__parse_acpi_value(props.get('design capacity','0 Wh')),'fullCapacity':self.__parse_acpi_value(props.get('last full capacity','0 Wh')),'capacityLeft':self.__parse_acpi_value(props.get('remaining capacity','0 Wh')),'rate':self.__parse_acpi_value(props.get('present rate','0 W')),'voltage':self.__parse_acpi_value(props.get('present rate','0 V')),'type':props.get('battery type','Unknown'),'manufacturer':props.get('OEM info','Unknown'),'charging':props.get('charging state',None)=='charging',}batteries.append(battery)returnbatteries
[docs]defget_adapters(self):""" Read the list of adapters from /proc/acpi/ac_adapter. :return: List of adapters, one per dict :rtype: list of dict """adapters=[]adapter_dir='/proc/acpi/ac_adapter'ifnotos.path.exists(adapter_dir):return[]fordinsorted(os.listdir(adapter_dir)):props={}path=os.path.join(adapter_dir,d,'state')forlinopen(path).read().splitlines():if':'inl:key,value=l.strip().split(':')props[key]=value.strip()adapter={'name':d,'pluggedIn':props.get('state',None)=='on-line',}adapters.append(adapter)returnadapters
[docs]defpoweroff(self):""" Basically power off the system. """subprocess.check_call(['poweroff'])
[docs]defreboot(self):""" Basically reboot the system. """subprocess.check_call(['reboot'])
[docs]defsuspend(self):""" Basically suspend the system. """subprocess.check_call(['pm-suspend'])
[docs]defhibernate(self):""" Basically hibernate the system. """subprocess.check_call(['pm-hibernate'])