#!/usr/bin/python

# Copyright (C) 2001 John Leach <john@johnleach.co.uk>

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

import sys,os,string,re
import fileinput

debug = 0
kelv_to_celc = 273.15
acpi_proc_root_path = "/proc/acpi/"
acpi_proc_batt_path = acpi_proc_root_path + "battery/"
acpi_proc_thermal = acpi_proc_root_path + "thermal/"

def debugmsg(msg):
	if debug==1:
		print "Debug: %s" % msg

def merge_dict(dict1,dict2):
	for key in dict2.keys():
		dict1[key] = dict2[key]
	return dict1

def get_num(s):
	#if re.match("([0-9]+) ",s):
	regexp = re.compile("([0-9]+) ")
	regs = regexp.match(s)
	if regs:
		num = string.atol(regs.group(1))
		debugmsg("get_num(%s) returned %s" % (s,num))
		return num
	else:
		debugmsg("get_num(%s) failed, returned 1" % s)
		return 1
	#return string.atol(string.split(s)[0])
	#else:
	#	return 1

def parse_proc_file(fname):
	"parse the specified file and return a dictionary of the contents"
	regexp = re.compile("([0-9a-zA-Z ]+)\:\s+(.*)")
	dict = {}
	for line in fileinput.input(fname):
		regs = regexp.match(line);
		if regs:
			debugmsg("Parsed: %s = %s" %(regs.group(1),regs.group(2)))
			dict[regs.group(1)] = regs.group(2)
	return dict
			
def list_batteries():
	"return a list of batt dictionaries"
	batt_list = []
	for battno in os.listdir(acpi_proc_batt_path):
		batt_dict = {}
		debugmsg("Found a battery (%s)" % battno)
		# parse info and status file and merge into same dict
		batt_dict["number"		] = battno
		merge_dict(batt_dict, parse_proc_file(acpi_proc_batt_path + battno + "/info"))
		merge_dict(batt_dict, parse_proc_file(acpi_proc_batt_path + battno + "/status"))
		# append batt dict to the batt list
		batt_list.append(batt_dict)

	return batt_list
		
def print_batteries(batt_list):
	print "--[ Battery Status ]-------------------------------"
	for batt in batt_list:
		if (batt.has_key("Present") and (batt["Present"]=="yes")):
			print " Slot %s: %s %s  (type: %s  ser: %s)" % (
					batt["number"],
					batt["Model Number"],
					batt["OEM Info"],
					batt["Battery Type"],
					batt["Serial Number"])

			print "  State:\t%s" % batt["State"]
			full = get_num(batt["Design Capacity"])
			curr = get_num(batt["Remaining Capacity"])
			print "  Capacity:\t%i/%imWh (%i%%)" % (
					curr,
					full,
					(curr * 100) / full)

			rate = get_num(batt["Present Rate"])
			remain = float(curr) / float(rate)
			print "  Rate: \t%imW\t(%.2f minutes left)" % (
					rate,
					(remain * 60))
			
		else:
			print " Slot %s: Not Present" % (batt["number"])


def list_thermal():
	"return a list of thermal sensor dictionaries"
	sensor_list = []
	for sensorno in os.listdir(acpi_proc_thermal):
		sensordict = {}
		debugmsg("Found a thermal sensor (%s)" % sensorno)
		sensordict["number"] = sensorno
		merge_dict(sensordict,parse_proc_file(acpi_proc_thermal + sensorno + "/status"))
		sensor_list.append(sensordict)
	return sensor_list
		
def print_thermal(therm_list):
	print "--[ Thermal Sensor Status ]------------------------"
	for sensor in therm_list:
		if (sensor.has_key("Temperature")):
			ktemp = get_num(sensor["Temperature"])
			ctemp = (ktemp / 10) - kelv_to_celc
			print " Sensor %s: %s (%s Celcius)" % (sensor["number"],sensor["Temperature"],ctemp) 
	
batt_list = list_batteries()
print_batteries(batt_list)

thermal_list = list_thermal()
print_thermal(thermal_list)


