1+ import re
2+ from datetime import datetime
13import logging
2- from wfc import paths
4+ from typing import Tuple
5+ from wfc import paths , popup
6+
37
48ENCODING = "utf-8"
59INFO = "info"
610ERROR = "error"
11+ DATE_FORMAT = "%d.%m.%Y %H:%M:%S"
712
813LOG : logging .Logger
914STARTUP = True
@@ -16,12 +21,65 @@ def log(log_type: str, text: str):
1621 LOG .info (text )
1722
1823
24+ def show_summary ():
25+ current_date = datetime .now ()
26+ size_saved = 0.0
27+ with open (paths .LOG_PATH , "r" , encoding = ENCODING ) as log_file :
28+ for line in log_file :
29+ if "Program startup" in line :
30+ continue
31+
32+ timestamp , _ , size , _ = extract_line_data (line )
33+ if timestamp == "" :
34+ continue
35+ timestamp_datetime = datetime .strptime (timestamp , DATE_FORMAT )
36+
37+ if (current_date - timestamp_datetime ).days <= 30 :
38+ size_saved += float (size [:- 3 ])
39+
40+ size_saved /= 1024.0
41+ popup .show_notification (
42+ "There's nothing to delete!" ,
43+ f"You have saved { size_saved :.2f} GB in the last 30 days." ,
44+ )
45+
46+
47+ def extract_line_data (line ) -> Tuple [str , str , str , str ]:
48+ # sourcery skip: extract-method, inline-variable, remove-unnecessary-else
49+ # Define regular expressions to match different parts of the log line
50+ timestamp_regex = r"(\d{2}\.\d{2}\.\d{4} \d{2}:\d{2}:\d{2})"
51+ filename_regex = r"FILE: (.*?) SIZE:"
52+ size_regex = r"SIZE: ([\d.]+) (.)B"
53+ reason_regex = r"REASON: (.+)"
54+
55+ # Compile the regex patterns
56+ timestamp_match = re .search (timestamp_regex , line )
57+ filename_match = re .search (filename_regex , line )
58+ size_match = re .search (size_regex , line )
59+ reason_match = re .search (reason_regex , line )
60+
61+ # Check if all parts were matched
62+ if timestamp_match and filename_match and size_match and reason_match :
63+ timestamp = timestamp_match [1 ]
64+ filename = filename_match [1 ]
65+ # Convert size to float (assuming MB for demonstration)
66+ size_value = float (size_match [1 ])
67+ if size_match [2 ] == "G" :
68+ size_value *= 1024 # Convert to MB if size is in GB
69+ size = f"{ size_value :.2f} MB"
70+ reason = reason_match [1 ]
71+ return timestamp , filename , size , reason
72+ else :
73+ # print(f"LINE FORMAT INVALID! {filename_match}")
74+ return "" , "" , "" , ""
75+
76+
1977if STARTUP :
2078 logging .basicConfig (
2179 filename = paths .LOG_PATH ,
2280 level = logging .INFO ,
2381 format = "%(asctime)s - %(message)s" ,
24- datefmt = "%d-%m-%Y %H:%M:%S" ,
82+ datefmt = DATE_FORMAT ,
2583 )
2684 LOG = logging .getLogger (__name__ )
2785 LOG .critical ("Program startup" )
0 commit comments