|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import os |
| 3 | +import subprocess |
| 4 | +import time |
| 5 | +from argparse import ArgumentParser |
| 6 | + |
| 7 | +import matplotlib.pyplot as plt |
| 8 | +import yaml |
| 9 | + |
| 10 | + |
| 11 | +def execute_terminal_command(command): |
| 12 | + """ |
| 13 | + THIS FUNCTION EXECUTES A TERMINAL COMMAND USING SUBPROCESS MODULE |
| 14 | + """ |
| 15 | + try: |
| 16 | + return subprocess.check_output(command).decode("utf-8").strip() |
| 17 | + except subprocess.CalledProcessError: |
| 18 | + pass |
| 19 | + |
| 20 | + |
| 21 | +def update_pie_chart(labels, sizes, log_dir): |
| 22 | + """ |
| 23 | + THIS FUNCTION SAVES/UPDATES PIE CHART USING: labels, sizes |
| 24 | + AND SAVES IT INTO log_dir |
| 25 | + """ |
| 26 | + # Plotting pie chart |
| 27 | + fig = plt.figure() |
| 28 | + plt.pie(sizes, labels=labels, autopct="%1.1f%%", shadow=True, startangle=140) |
| 29 | + plt.axis("equal") |
| 30 | + plt.savefig(logdir + "/time_tracking_pie_chart.png") |
| 31 | + plt.close(fig) |
| 32 | + |
| 33 | + |
| 34 | +if __name__ == "__main__": |
| 35 | + parser = ArgumentParser() |
| 36 | + parser.add_argument( |
| 37 | + "--config", |
| 38 | + type=str, |
| 39 | + required=False, |
| 40 | + default="config.yaml", |
| 41 | + help="path to config", |
| 42 | + ) |
| 43 | + args = parser.parse_args() |
| 44 | + |
| 45 | + with open(args.config, mode="r") as fp: |
| 46 | + config = yaml.safe_load(fp) |
| 47 | + |
| 48 | + update_frequency = config["update_frequency"] # Uodate time in seconds |
| 49 | + pie_chart_update_frequency = config[ |
| 50 | + "pie_chart_update_frequency" |
| 51 | + ] # Uodate time in seconds |
| 52 | + applist = [] # List that stores all apps names |
| 53 | + winlist = [] # List that stores corresponding information to those apps in applist |
| 54 | + |
| 55 | + logdir = os.getcwd() # Logs folder within the same working directory |
| 56 | + |
| 57 | + while True: |
| 58 | + time.sleep(update_frequency) # Wait for the update frequency |
| 59 | + frpid = execute_terminal_command( |
| 60 | + ["xdotool", "getactivewindow", "getwindowpid"] |
| 61 | + ) # Get the current opened window process id |
| 62 | + frname = execute_terminal_command( |
| 63 | + ["xdotool", "getactivewindow", "getwindowname"] |
| 64 | + ) # Get the current opened window name |
| 65 | + |
| 66 | + app = execute_terminal_command( |
| 67 | + ["ps", "-p", frpid, "-o", "comm="] |
| 68 | + ) # Obtain the app name using ps terminal command |
| 69 | + |
| 70 | + # adding the app to the app list |
| 71 | + if not app in applist: |
| 72 | + applist.append(app) |
| 73 | + checklist = [item[1] for item in winlist] |
| 74 | + if not frname in checklist: |
| 75 | + winlist.append([app, frname, 1 * update_frequency]) |
| 76 | + else: |
| 77 | + winlist[checklist.index(frname)][2] = ( |
| 78 | + winlist[checklist.index(frname)][2] + 1 * update_frequency |
| 79 | + ) |
| 80 | + |
| 81 | + # TODO: Make pie-chart update is based on pie_chart_update_frequency variable |
| 82 | + # TODO: Organize pie charts and maybe don't delete previous pie charts and appreviate from them instead |
| 83 | + update_pie_chart(applist, [x[-1] for x in winlist], logdir) |
0 commit comments