Skip to content

Commit f913df1

Browse files
committed
removed trayicon, added summary notification
1 parent bbb80f4 commit f913df1

5 files changed

Lines changed: 91 additions & 22 deletions

File tree

config.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[Settings]
2-
SEARCH_PATHS = C:\Users\Wreck\Downloads\,I:\Downloads,J:\Downloads,F:\Recordings
2+
SEARCH_PATHS = C:\Users\Wreck\Downloads\,I:\Downloads,J:\Downloads
33
EXTRACTABLE_EXTENSIONS = zip|rar
44
DAYS_UNTIL_DELETION = 14
55
START_WITH_WINDOWS = true

wfc/file.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
import re
66
from typing import List, Tuple
77

8-
from wfc.logger import log, INFO, ERROR
9-
from wfc import popup, config, registry, paths
8+
from wfc import popup, config, registry, paths, logger
109

1110
# TODO: Testmode toggle
1211
TESTMODE = True
@@ -29,7 +28,7 @@ def get_things_to_delete() -> (
2928

3029
for search_path in search_paths:
3130
if not os.path.exists(search_path):
32-
log(ERROR, f"Path {search_path} not found!")
31+
logger.log(logger.ERROR, f"Path {search_path} not found!")
3332
continue
3433

3534
for item in os.listdir(search_path):
@@ -113,7 +112,6 @@ def handle_startup() -> None:
113112
max_log_size_mb,
114113
) = config.data
115114

116-
registry.remove_from_autostart()
117115
if start_with_windows:
118116
registry.add_to_autostart()
119117

@@ -152,16 +150,19 @@ def process_things():
152150
else:
153151
type_label = "DIR"
154152

155-
log(
156-
INFO,
153+
logger.log(
154+
logger.INFO,
157155
f"{type_label}: {basename} SIZE: {bytes_to_string(size_bytes)} REASON: {reason}",
158156
)
159157

160158
bytes_to_delete += size_bytes
161159
delete_file_or_folder(path, is_file)
162160

163-
popup.show_notification(
164-
f"Deleted {bytes_to_string(bytes_to_delete)} from your computer.",
165-
f"{len(files_to_delete)} files and {len(folders_to_delete)} folders.\n"
166-
+ f"{amount_reason_unpacked} files already unpacked, {amount_reason_age} too old ({config.data[2]} days)",
167-
)
161+
if bytes_to_delete <= 0:
162+
logger.show_summary()
163+
else:
164+
popup.show_notification(
165+
f"Deleted {bytes_to_string(bytes_to_delete)} from your computer.",
166+
f"{len(files_to_delete)} files and {len(folders_to_delete)} folders.\n"
167+
+ f"{amount_reason_unpacked} files already unpacked, {amount_reason_age} too old ({config.data[2]} days)",
168+
)

wfc/logger.py

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1+
import re
2+
from datetime import datetime
13
import logging
2-
from wfc import paths
4+
from typing import Tuple
5+
from wfc import paths, popup
6+
37

48
ENCODING = "utf-8"
59
INFO = "info"
610
ERROR = "error"
11+
DATE_FORMAT = "%d.%m.%Y %H:%M:%S"
712

813
LOG: logging.Logger
914
STARTUP = 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+
1977
if 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")

wfc/paths.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@ def strip_package_name_from_path(path) -> str:
2121
EXE_FILE_NAME = f"{APP_NAME}.exe"
2222
CONFIG_FILE_NAME = "config.ini"
2323
LOG_FILE_NAME = "wfc.log"
24+
ICON_FILE_NAME = "clean.ico"
2425

2526
BASE_PATH = get_base_path()
2627
EXE_PATH = os.path.join(BASE_PATH, EXE_FILE_NAME)
2728

2829
CONFIG_PATH = os.path.join(BASE_PATH, CONFIG_FILE_NAME)
2930
LOG_PATH = os.path.join(BASE_PATH, LOG_FILE_NAME)
31+
ICON_PATH = os.path.join(BASE_PATH, ICON_FILE_NAME)

wfc/popup.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,31 @@
11
import os
22
import time
33
import win11toast
4+
from wfc import paths
45

6+
LOG_BUTTON_TXT = "Open Log"
7+
RECYCLE_BIN_BUTTON_TXT = "Open Recycle Bin"
58

6-
# from wfc import config
79

10+
def tray_clicked(button: dict):
11+
button_name_pressed = button["arguments"].split(":")[1].strip()
12+
if button_name_pressed == LOG_BUTTON_TXT:
13+
os.startfile(paths.LOG_PATH)
14+
elif button_name_pressed == RECYCLE_BIN_BUTTON_TXT:
15+
os.startfile("shell:RecycleBinFolder")
816

9-
def tray_clicked():
10-
# os.startfile(config.data[0][0]) -> Downloads
11-
# os.startfile("::{20D04FE0-3AEA-1069-A2D8-08002B30309D}") -> This PC
12-
os.startfile("shell:RecycleBinFolder")
1317

14-
15-
def show_notification(title: str, message: str) -> None:
18+
def show_notification(title: str, message: str):
1619
time.sleep(1)
20+
21+
icon = {"src": paths.ICON_PATH, "placement": "appLogoOverride"}
22+
1723
win11toast.toast(
1824
title,
1925
message,
2026
audio={"silent": "true"},
2127
duration="long",
22-
on_click=lambda args: tray_clicked(),
28+
on_click=lambda args: tray_clicked(args),
29+
buttons=[LOG_BUTTON_TXT, RECYCLE_BIN_BUTTON_TXT],
30+
icon=icon,
2331
)

0 commit comments

Comments
 (0)