Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFE] Use XDG basedirs instead of hardcoding paths. #44

Open
ahmubashshir opened this issue Mar 22, 2023 · 1 comment
Open

[RFE] Use XDG basedirs instead of hardcoding paths. #44

ahmubashshir opened this issue Mar 22, 2023 · 1 comment

Comments

@ahmubashshir
Copy link

Use pyxdg to get paths

@ahmubashshir
Copy link
Author

From a85d7a39c15601ef5ec1e2c3bdc88abe8b6bd209 Mon Sep 17 00:00:00 2001
Message-Id: <a85d7a39c15601ef5ec1e2c3bdc88abe8b6bd209.1679478751.git.ahmubashshir@gmail.com>
From: Mubashshir <[email protected]>
Date: Wed, 22 Mar 2023 15:50:09 +0600
Subject: [PATCH] path: Use `xdg.BaseDirectory` to get cache path

Signed-off-by: Mubashshir <[email protected]>
---
 misc/export_to_2.0       | 11 ++++-------
 src/Watcher/analysis.py  |  7 +++----
 src/Watcher/commands.py  |  4 ++--
 src/Watcher/files.py     | 11 +++++++++++
 src/Watcher/watch_log.py | 18 +++++++++---------
 src/bin/watcher          |  5 ++++-
 6 files changed, 33 insertions(+), 23 deletions(-)
 mode change 100755 => 100644 src/Watcher/commands.py
 create mode 100644 src/Watcher/files.py

diff --git a/misc/export_to_2.0 b/misc/export_to_2.0
index ef30ffe..d6fe65c 100755
--- a/misc/export_to_2.0
+++ b/misc/export_to_2.0
@@ -5,12 +5,11 @@ sys.path.insert(0, "/usr/share/Watcher/")
 import csv
 from watch_log import get_date
 import datetime
+import files
 import time_operations as to
 
 def extract_data(date):
-    user = os.getlogin()
-    path = "/home/" + user +"/.cache/Watcher/raw_data/"
-    filename = path + date + ".csv"
+    filename = files.raw_log(date)
 
     l = list() # l = list of  (app_name, time spent on app on that session)
     d = dict()
@@ -64,7 +63,7 @@ def final_report(window_opened, time_spent):
 def export_to_new(date):
     window_opened, time_spent = extract_data(date)
     sorted_report = final_report(window_opened, time_spent)
-    filename = "/home/"+os.getlogin()+"/.cache/Watcher/daily_data/"+date+".csv"
+    filename = files.daily_log(date)
     overwrite_Data = []
     with open(filename, 'w') as csvfile:
         for x,y in sorted_report.items():
@@ -76,8 +75,6 @@ def export_to_new(date):
     del sorted_report
     del overwrite_Data
 
-path = "/home/" + os.getlogin() +"/.cache/Watcher/raw_data/"
+path = os.path.dirname(files.raw_log('empty'))
 for file in os.listdir(path):
     export_to_new(file[:-4])
-
-
diff --git a/src/Watcher/analysis.py b/src/Watcher/analysis.py
index 0e917f8..d43b1b4 100755
--- a/src/Watcher/analysis.py
+++ b/src/Watcher/analysis.py
@@ -3,11 +3,11 @@ import csv
 from watch_log import get_date
 import datetime
 import time_operations as to
+import files
 
 # creating dictionary to store time spend on each applicaitons on that particular day
 def final_report(date):
-    path = "/home/" + os.getlogin() +"/.cache/Watcher/daily_data/"
-    filename = path + date + ".csv"
+    filename = files.daily_log(date)
 
     report = dict()
     if os.path.isfile(filename):
@@ -64,7 +64,7 @@ def weekday_from_date(date):
 
 def weekly_logs(week = str(os.popen('''date +"W%V-%Y"''').read()[0:-1])):
     user = os.getlogin()
-    filename = "/home/"+user+"/.cache/Watcher/Analysis/"+week+".csv"
+    filename = files.weekly_log(week)
     with open(filename, "w") as csvfile:
         csvwriter = csv.writer(csvfile, delimiter='\t')
 
@@ -95,4 +95,3 @@ def weekly_logs(week = str(os.popen('''date +"W%V-%Y"''').read()[0:-1])):
 #testing
 if __name__ == "__main__":
     weekly_logs("W29-2022")
-
diff --git a/src/Watcher/commands.py b/src/Watcher/commands.py
old mode 100755
new mode 100644
index 8805dac..6234011
--- a/src/Watcher/commands.py
+++ b/src/Watcher/commands.py
@@ -1,5 +1,6 @@
 import os
 import csv
+import uuid
 import datetime
 from watch_log import get_date
 import analysis as anls
@@ -71,8 +72,7 @@ def daily_summary(date = get_date()):
         print("   " + Color.GREEN(f'{x:<22}') + '\t ',f'{to.format_time(y):>12}')
 
 def week_summary(week = os.popen('''date +"W%V-%Y"''').read()[:-1]):
-    user = os.getlogin()
-    filename = "/home/"+user+"/.cache/Watcher/Analysis/"+week+".csv"
+    filename = files.weekly_log(week)
     with open(filename, 'r') as file:
         csvreader = csv.reader(file, delimiter='\t')
         week_overview = dict()
diff --git a/src/Watcher/files.py b/src/Watcher/files.py
new file mode 100644
index 0000000..1f4c9e3
--- /dev/null
+++ b/src/Watcher/files.py
@@ -0,0 +1,11 @@
+from xdg.BaseDirectory import save_cache_path
+from os import getlogin, path
+def weekly_log(week):
+	return path.join(save_cache_path('Watcher/Analysis'), '%s.csv' % week)
+
+def daily_log(date):
+	return path.join(save_cache_path('Watcher/daily_data'), '%s.csv' % date)
+
+def raw_log(date):
+	return path.join(save_cache_path('Watcher/raw_data'), '%s.csv' % date)
+user = getlogin()
diff --git a/src/Watcher/watch_log.py b/src/Watcher/watch_log.py
index 6a44ca5..6119e45 100755
--- a/src/Watcher/watch_log.py
+++ b/src/Watcher/watch_log.py
@@ -1,6 +1,7 @@
 import os
 import csv
 import time
+import files
 import get_windows as x
 import afk as y
 from time_operations import time_difference, time_addition, convert
@@ -16,7 +17,7 @@ def get_date():
     return d[0:-1]
 
 def update_csv(date, Data):
-    filename = "/home/"+os.getlogin()+"/.cache/Watcher/daily_data/"+date+".csv"
+    filename = files.daily_log(date)
     overwrite_Data = []
     with open(filename, 'w') as csvfile:
         for x,y in Data.items():
@@ -43,9 +44,9 @@ def import_data(file):
 # TODO: AFK feature devlopement (it will be developed after completing alpha product (after whole project up end running)
 
 def log_creation():
-    filename = "/home/"+os.getlogin()+"/.cache/Watcher/daily_data/"+get_date()+".csv"
+    filename = files.daily_log(get_date())
     if not(os.path.isfile(filename)):
-        creat_file = "/home/"+os.getlogin()+"/.cache/Watcher/daily_data/"+get_date()+".csv"
+        creat_file = filename
         with open(creat_file, 'w') as fp:
             pass
 
@@ -54,7 +55,7 @@ def log_creation():
     data = import_data(filename)
     while True:
         date = get_date()
-        filename = "/home/"+os.getlogin()+"/.cache/Watcher/daily_data/"+date+".csv"
+        filename = files.daily_log(date)
         afk = y.is_afk(afkTimeout)
         print(data)
 
@@ -71,18 +72,17 @@ def log_creation():
 
             usage = time_addition("00:00:01", usage)
             data.update({active_window : usage})
-            if os.path.isfile("/home/"+os.getlogin()+"/.cache/Watcher/daily_data/"+get_date()+".csv"):
+            if os.path.isfile(files.daily_log(get_date())):
                 update_csv(get_date(), data)
-            elif not(os.path.isfile("/home/"+os.getlogin()+"/.cache/Watcher/daily_data/"+get_date()+".csv")):
-                new_filename = "/home/"+os.getlogin()+"/.cache/Watcher/daily_data/"+get_date()+".csv"
+            elif not(os.path.isfile(files.daily_log(get_date()))):
+                new_filename = files.daily_log(get_date())
                 with open(new_filename, 'w') as fp:
                     pass
 
                 data.clear()
-        
+
 
 if __name__ == "__main__":
     log_creation()
     #afk_time = int(round(int(os.popen("xprintidle").read()[:-1])/1000, 0))
     #print(afk_time)
-
diff --git a/src/bin/watcher b/src/bin/watcher
index 0dde744..ff91167 100755
--- a/src/bin/watcher
+++ b/src/bin/watcher
@@ -44,7 +44,10 @@ if len(arg) == 2:
         help_option()
     elif arg[1] == "--start" or arg[1] == "-s":
         print("Log creations started... \nif you wanna stop it, use keyboard-shortcut (Ctrl+Shift+C or Ctrl+C)")
-        x.log_creation()
+        try:
+            x.log_creation()
+        except KeyboardInterrupt:
+            exit(0)
     elif arg[1] == "--version" or arg[1] == "-v":
         print("Version: 2.0.0")
     else:
-- 
2.40.0

use git am to apply the patch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant