Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python3
- #-*- coding: utf-8 -*-
- import os, time, psutil, sys
- from datetime import datetime as dt, timedelta
- def bold(input_data, output_length):
- return "__" + str(input_data)[:output_length] + "__"
- def get_info_from_file(file_path):
- with open(file_path, "rt") as status_file:
- charge_number = status_file.read()
- status_file.close()
- return charge_number
- def update_last_line(file_lines, new_last_line):
- file_lines[len(file_lines) - 1] = new_last_line
- return file_lines
- def check_log_file(file_path):
- if not os.path.isfile(file_path):
- new_file = open(file_path, 'w')
- new_file.write("Liczenie zużycia baterii\n========================")
- new_file.close()
- # klasa czasu
- # skladaja sie na nia dwie liczby z plikow
- # z folderu linuxa o baterii
- # ma metody aktualizujaca stan baterii
- # i obliczajaca procent stanu baterii
- class battery_state:
- def __init__(self):
- self.battery_full = int(get_info_from_file("/sys/class/power_supply/BATC/charge_full"))
- self.battery_charge = int(get_info_from_file("/sys/class/power_supply/BATC/charge_now"))
- self.battery_charge_on_start = self.battery_charge
- self.battery_status = get_info_from_file("/sys/class/power_supply/BATC/status")
- def get_percent_charge(self):
- battery_percent_charge = self.battery_charge / self.battery_full
- return int(str(battery_percent_charge * 100)[:2])
- def update_battery_charge(self):
- self.battery_charge = int(get_info_from_file("/sys/class/power_supply/BATC/charge_now"))
- self.battery_status = get_info_from_file("/sys/class/power_supply/BATC/status")
- def get_current_battery_discharge(self):
- self.update_battery_charge()
- return self.battery_charge_on_start - self.battery_charge
- def is_battery_charging(self):
- self.update_battery_charge()
- if not "Dischar" in self.battery_status:
- result = True
- else:
- result = False
- return result
- class script_running_time:
- def __init__(self, start_time):
- self.run_time = dt.now() - start_time
- self.start_time = start_time
- def update(self):
- self.run_time = dt.now() - self.start_time
- class laptop_usage:
- def __init__(self):
- self.cpu_usage_sum = 0
- self.ram_usage_sum = 0
- self.brightness_sum = 0
- self.times_added = 0
- # self.brightness_added = 0
- # self.cpu_times_added = 0
- # self.ram_times_added = 0
- # type of data
- # 1 dla CPU
- # 2 dla RAM
- # 3 dla JASNOŚCI
- def get_average_percent(self, type_of_data):
- if type_of_data == 1:
- average_percent = self.cpu_usage_sum / self.times_added
- elif type_of_data == 2:
- average_percent = self.ram_usage_sum / self.times_added
- elif type_of_data == 3:
- average_percent = self.brightness_sum / self.times_added
- #print(str(self.cpu_usage_sum) + " / " + str(self.cpu_times_added) + " = " + str(average_percent)[:4])
- return average_percent
- def update_usage(self):
- self.times_added += 1
- self.cpu_usage_sum += psutil.cpu_percent()
- self.ram_usage_sum += psutil.phymem_usage().percent
- self.brightness_sum += float(os.popen("xbacklight").read())
- def discharge_time(self, script_uptime, battery_object):
- # liczymy z proporcji, skoro:
- # current_battery_discharge = script_uptime.total_seconds()
- # battery_full = nasz_wynik
- # wiec wynika z tego, ze:
- # nasz_wynik = (battery_full * script_uptime.total_seconds()) / current_battery_discharge
- result_seconds = battery_object.battery_full * script_uptime.total_seconds()
- if battery_object.get_current_battery_discharge() > 0:
- result_seconds /= battery_object.get_current_battery_discharge()
- if debug:
- print(str(result_seconds) + "=" +
- str(battery_object.battery_full) + "*" +
- str(script_uptime.total_seconds()) + "/" +
- str(battery_object.get_current_battery_discharge()))
- return timedelta(seconds=result_seconds)
- # def add_usage(self, new_usage, is_cpu):
- # if is_cpu:
- # self.cpu_usage_sum += new_usage
- # self.cpu_times_added += 1
- # else:
- # self.ram_usage_sum += new_usage
- # self.ram_times_added += 1
- # poczatek dzialan
- my_battery_state = battery_state()
- script_start_time = dt.now()
- laptop_uptime = script_running_time(script_start_time)
- laptop_stats = laptop_usage()
- log_file_path = os.getenv("HOME") + "/login_time.md"
- # file_content_on_start
- state_of_laptop = ""
- last_line_of_log = ""
- ram_usage = psutil.phymem_usage().percent
- is_loading = my_battery_state.is_battery_charging()
- # fix dla openboxa, brak nazwy
- if "XDG_CURRENT_DESKTOP" in os.environ:
- user_desktop = os.environ['XDG_CURRENT_DESKTOP']
- else:
- user_desktop = "Openbox"
- debug = False
- # jesli sie laduje to nie ma po co zapisywac
- if my_battery_state.is_battery_charging():
- sys.exit("Battery is charging")
- # sprawdzamy czy plik istnieje
- check_log_file(log_file_path)
- # na poczatek tworzymy nowa linie
- # w ktorej sa zapisane informacje o stanie komputera na starcie
- with open(log_file_path, "a") as out_file:
- start_string = "\nStart " + user_desktop + " o "
- start_string += str(script_start_time.hour) + ":"
- start_string += str(script_start_time.minute)
- start_string += ", stan baterii wynosił "
- start_string += str(my_battery_state.get_percent_charge()) + "%"
- start_string += "(" + str(my_battery_state.battery_charge) + ")"
- start_string += "\n".ljust(len(start_string), "-") + "\nXDDD"
- out_file.write(start_string)
- out_file.close()
- with open(log_file_path, "rt") as out_file:
- file_content_on_start = out_file.readlines()
- while True:
- my_battery_state.update_battery_charge()
- laptop_uptime.update()
- laptop_stats.update_usage()
- if my_battery_state.is_battery_charging():
- sys.exit("Started battery charging. Good bye!")
- last_line_of_log = "System działał " + bold(laptop_uptime.run_time, 10)
- last_line_of_log += ", a stan baterii wynosił "
- last_line_of_log += bold(my_battery_state.get_percent_charge(),15) + "%"
- last_line_of_log += "(" + bold(my_battery_state.battery_charge, 15) + ")"
- last_line_of_log += " przy średnim zużyciu CPU równym "
- last_line_of_log += bold(laptop_stats.get_average_percent(1),4) + "%"
- last_line_of_log += ", RAM równemu "
- last_line_of_log += bold(laptop_stats.get_average_percent(2),4) + "%"
- last_line_of_log += " ze średnią jasnością ekranu równą "
- last_line_of_log += bold(laptop_stats.get_average_percent(3),4) + "%."
- if laptop_stats.times_added > 8:
- last_line_of_log += "Z takim zużyciem rozladowanie od 100% do 0 zajęłoby "
- last_line_of_log += bold(laptop_stats.discharge_time(
- laptop_uptime.run_time, my_battery_state),10)
- file_content = update_last_line(file_content_on_start, last_line_of_log)
- with open(log_file_path, "wt") as input_file:
- input_file.writelines(file_content)
- input_file.close()
- time.sleep(5)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement