# -*- coding: utf-8 -*- from setuphelpers import * def install(): pass """ powercfg /batteryreport /? POWERCFG /BATTERYREPORT [/OUTPUT ] [/XML] [/TRANSFORMXML ] Description: Generates a report of battery usage characteristics over the lifetime of the system. The BATTERYREPORT command will generate an HTML report file in the current path. Parameter List: /OUTPUT Specify the path and filename to store the battery report HTML or XML file. /XML Format the report file as XML. /DURATION Specify the number of days to analyze for the report. /TRANSFORMXML Reformat an XML report file as HTML. Examples: POWERCFG /BATTERYREPORT POWERCFG /BATTERYREPORT /OUTPUT "batteryreport.html" POWERCFG /BATTERYREPORT /OUTPUT "batteryreport.xml" /XML POWERCFG /BATTERYREPORT /TRANSFORMXML "batteryreport.xml" POWERCFG /BATTERYREPORT /TRANSFORMXML "batteryreport.xml" /OUTPUT "batteryreport.html" """ def get_battery_capacities_from_xml(xml_file): def element_to_dict(element, remove_namespace=True): result = {} for child in element: tag = child.tag if remove_namespace: tag = tag.split("}", 1)[-1] if child: result[tag] = element_to_dict(child, remove_namespace) else: result[tag] = child.text return result import xml.etree.ElementTree as ET ET.register_namespace("", "b") tree = ET.parse(xml_file) root = tree.getroot() """ # Print element names and text content for debug elements = root.findall('.//*') for element in elements: print(f"{element.tag}: {element.text}") """ batteries_dict_list = [] for battery in root.findall("{http://schemas.microsoft.com/battery/2012}Batteries/{http://schemas.microsoft.com/battery/2012}Battery"): batteries_dict_list.append(element_to_dict(battery)) return batteries_dict_list def get_batteryreport(output_file): if "html" in output_file.lower(): run(f'POWERCFG /BATTERYREPORT /OUTPUT "{output_file}"') else: run(f'POWERCFG /BATTERYREPORT /OUTPUT "{output_file}" /XML') def audit(): audit_status = "OK" battery_report_file = get_private_persistent_package_file("batteryreport.xml") dmi_info_portable_battery = dmi_info().get("Portable_Battery", None) if dmi_info_portable_battery: get_batteryreport(battery_report_file) battery_report_dict = {} batteries_dict_list = get_battery_capacities_from_xml(battery_report_file) battery_count = len(batteries_dict_list) battery_report_dict.update({"battery_count": str(battery_count)}) # WAPT.write_audit_data_if_changed("audit-battery",, str(battery_count)) for i in range(0, battery_count, 1): battery_number_name = f"BATTERY_{str(i+1)}" # print(battery_number_name + ":") full_charge_capacity = batteries_dict_list[i]["FullChargeCapacity"] design_capacity = batteries_dict_list[i]["DesignCapacity"] battery_health = str(int(int(full_charge_capacity) * 100 / int(design_capacity))) + " %" batteries_dict_list[i].update({"Health": battery_health}) batteries_dict_list[i].update(dmi_info_portable_battery[i]) # WAPT.write_audit_data_if_changed("audit-battery", battery_number_name, batteries_dict_list[i]) # print(batteries_dict_list[i]) print( f'{battery_number_name} ({batteries_dict_list[i]["Location"]}) Health is {batteries_dict_list[i]["Health"]} for the Design_Capacity {batteries_dict_list[i]["Design_Capacity"]} with Design_Voltage {batteries_dict_list[i]["Design_Voltage"]}.' ) battery_report_dict.update({battery_number_name: batteries_dict_list[i]}) WAPT.write_audit_data_if_changed("audit-battery", "audit-battery", battery_report_dict) else: WAPT.write_audit_data_if_changed("audit-battery", "audit-battery", {"battery_count": "0"}) print("No battery detected.") return audit_status def get_battery_capacities_from_html(html_file): chars_to_delete = ["�������", "��\xa0", " mWh"] datas = {} # Opening the html file HTMLFile = open(html_file, "r") # Reading the file index = HTMLFile.read() # Creating a BeautifulSoup object and specifying the parser soup = BeautifulSoup(index, "html.parser") for div in soup.findAll("table"): rows = div.findAll("tr") for row in rows: if row.text.find("mWh") > -1: if "DESIGN CAPACITY" in (row.text): design_capacity = "".join([i for i in row.text.split("DESIGN CAPACITY")[-1].rstrip() if i.isdigit()]) for chars in chars_to_delete: design_capacity = design_capacity.replace(chars, "") datas["design_capacity"] = design_capacity if "FULL CHARGE CAPACITY" in (row.text): full_charge_capacity = "".join([i for i in row.text.split("FULL CHARGE CAPACITY")[-1].rstrip() if i.isdigit()]) for chars in chars_to_delete: full_charge_capacity = full_charge_capacity.replace(chars, "") datas["full_charge_capacity"] = full_charge_capacity datas["Health"] = f"{int(int(full_charge_capacity) * 100 / int(design_capacity))}%" return datas def get_private_persistent_package_file(file_name): file_name = os.path.basename(file_name) if control.package_uuid: if control.package_uuid == WAPT.is_installed(control.package)["package_uuid"]: return makepath(WAPT.persistent_root_dir, control.package_uuid, file_name) return makepath(os.getcwd(), "WAPT", "persistent", file_name)