Skip to content

Commit e5c558c

Browse files
committed
Added Settings menu
1 parent 1171caa commit e5c558c

File tree

8 files changed

+204
-99
lines changed

8 files changed

+204
-99
lines changed

components/convert.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
1-
def toGB(s):
2-
return round(s/(1024*1024*1024),3)
3-
4-
def toKB(s):
5-
return round(s/(1024),3)
6-
def toMB(s):
7-
return round(s/(1024*1024),3)
1+
def convert_bytes(bytes):
2+
bytes = float(bytes)
3+
if bytes >= 1099511627776:
4+
terabytes = bytes / 1099511627776
5+
size = '%.2fTb' % terabytes
6+
elif bytes >= 1073741824:
7+
gigabytes = bytes / 1073741824
8+
size = '%.2fGb' % gigabytes
9+
elif bytes >= 1048576:
10+
megabytes = bytes / 1048576
11+
size = '%.2fMb' % megabytes
12+
elif bytes >= 1024:
13+
kilobytes = bytes / 1024
14+
size = '%.2fKb' % kilobytes
15+
else:
16+
size = '%.2fb' % bytes
17+
return size

components/diskutil.py

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import wmi
22
import os
3-
3+
from convert import convert_bytes
44

55
def get_disk_usage():
66
key = wmi.WMI()
@@ -16,40 +16,26 @@ def get_disk_usage():
1616

1717

1818

19-
def convert_bytes(bytes):
20-
bytes = float(bytes)
21-
if bytes >= 1099511627776:
22-
terabytes = bytes / 1099511627776
23-
size = '%.2fTb' % terabytes
24-
elif bytes >= 1073741824:
25-
gigabytes = bytes / 1073741824
26-
size = '%.2fGb' % gigabytes
27-
elif bytes >= 1048576:
28-
megabytes = bytes / 1048576
29-
size = '%.2fMb' % megabytes
30-
elif bytes >= 1024:
31-
kilobytes = bytes / 1024
32-
size = '%.2fKb' % kilobytes
33-
else:
34-
size = '%.2fb' % bytes
35-
return size
3619

3720
def get_file_usage(dir):
3821
typesize = {}
22+
typecount = {}
3923
try:
4024
for root, dirs, files in os.walk(dir):
4125
for file in files:
4226
prefix, extension = os.path.splitext(file)
4327
if extension not in typesize:
4428
typesize[extension] = 0
29+
typecount[extension] = 0
4530
typesize[extension] += os.stat(root + os.sep + file).st_size
31+
typecount[extension] += 1
4632
except KeyboardInterrupt:
4733
pass
4834

4935
# print(typesize)
5036
result =list(sorted(typesize.items(), key=lambda x:-x[1]))
51-
37+
5238
for i in range(len(result)):
53-
result[i] = [result[i][0], convert_bytes(result[i][1])]
39+
result[i] = [result[i][0], convert_bytes(result[i][1]), typecount[result[i][0]]]
5440

5541
return result

components/duplicates.py

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import os
22
import sys
3-
import hashlib
43

4+
from hashlib import md5, blake2b, sha1, sha256, sha512, sha3_256, sha3_512, shake_128, shake_256
5+
from xxhash import xxh64, xxh128, xxh3_64, xxh3_128
6+
7+
hashfunc="md5"
58

69
def duplicates(folders):
710
dup_size = {}
@@ -62,7 +65,38 @@ def join_dicts(dict1, dict2):
6265

6366
def hashfile(path, blocksize=65536):
6467
file = open(path, 'rb')
65-
hasher = hashlib.md5()
68+
hasher = ""
69+
match hashfunc:
70+
case "md5":
71+
hasher = md5()
72+
case "sha1":
73+
hasher = sha1()
74+
case "blake2b":
75+
hasher = blake2b()
76+
case "sha256":
77+
hasher = sha256()
78+
case "sha512":
79+
hasher = sha512()
80+
case "sha3_256":
81+
hasher = sha3_256()
82+
case "sha3_512":
83+
hasher = sha3_512()
84+
case "shake_128":
85+
hasher = shake_128()
86+
case "shake_256":
87+
hasher = shake_256()
88+
case "xxh64":
89+
hasher = xxh64()
90+
case "xxh128":
91+
hasher = xxh128()
92+
case "xxh3_64":
93+
hasher = xxh3_64()
94+
case "xxh3_128":
95+
hasher = xxh3_128()
96+
case _:
97+
print("Invalid hash function")
98+
sys.exit(1)
99+
66100
buf = file.read(blocksize)
67101
while len(buf) > 0:
68102
hasher.update(buf)
@@ -87,8 +121,11 @@ def print_results(dict1):
87121
print('No duplicate files found.')
88122

89123

90-
def find_duplicates(dir):
124+
def find_duplicates(dir, func):
91125
# dir=input("Enter the directory names to find for duplicates: ").split(" ")
126+
global hashfunc
127+
hashfunc = func
128+
print(hashfunc)
92129
a= duplicates([dir])
93130
return a
94131

components/gui.py

Lines changed: 112 additions & 53 deletions
Large diffs are not rendered by default.

components/infrequent.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,28 @@
11
import glob
22
import os
33
import time
4-
from convert import toGB
5-
6-
def infrequent_files(dir):
4+
from convert import convert_bytes
5+
def infrequent_files(dir, days):
6+
today = time.strftime('%m/%d/%Y', time.gmtime())
77
list_of_files = filter( os.path.isfile, glob.glob(dir + '/**/*') )
8-
list_of_files = sorted( list_of_files, key = os.path.getmtime)
9-
8+
list_of_files = sorted( list_of_files, key = os.path.getatime)
109
infrequent_files = []
1110
for file_path in list_of_files:
12-
timestamp_str = time.strftime( '%m/%d/%Y',
13-
time.gmtime(os.path.getmtime(file_path)))
14-
file_size = os.path.getsize(file_path)
15-
infrequent_files.append([file_path,file_size,timestamp_str])
11+
timestamp_str = time.strftime( '%m/%d/%Y', time.gmtime(os.path.getatime(file_path)))
12+
if compare_dates(timestamp_str, today, days):
13+
file_size = os.path.getsize(file_path)
14+
infrequent_files.append([file_path,convert_bytes(file_size),timestamp_str])
1615
return infrequent_files
1716

17+
def compare_dates(date1, date2, days):
18+
date1 = time.strptime(date1, '%m/%d/%Y')
19+
date2 = time.strptime(date2, '%m/%d/%Y')
20+
difference = time.mktime(date2) - time.mktime(date1)
21+
difference = difference / (24 * 60 * 60)
22+
if difference > days:
23+
return True
24+
return False
25+
1826
def delete_infrequent_files(files):
1927
if files:
2028
for file in files:

components/largefiles.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import os
2+
from convert import convert_bytes
23

3-
def find_large_files(path):
4-
# path=input("Enter the directory name to find large files: ")
4+
def find_large_files(path, file_size_limit):
5+
limit=file_size_limit*1024*1024
56
ans=[]
67
if os.path.exists(path):
78
for foldername, subfolders, filenames in os.walk(path):
89
for filename in filenames:
910
size=os.path.getsize(os.path.join(foldername, filename))
10-
if size>100000000: # 100 MB
11-
ans.append([foldername + '\\' + filename, size])
11+
if size>limit:
12+
ans.append([foldername + '\\' + filename, convert_bytes(size)])
1213

1314
return ans
1415
else:

components/scan.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,21 @@
99
# create a class file that imports functions from inported files and returns them in seperate functions
1010

1111
class Scanner:
12+
dataFormat = "bytes"
13+
hashalgo = "md5"
14+
file_size_limit = 100
15+
time_limit = 30
1216
def __init__(self):
1317
super().__init__()
1418

1519
def find_duplicates(self, folders):
16-
return duplicates.find_duplicates(folders)
20+
return duplicates.find_duplicates(folders, self.hashalgo)
1721

1822
def remove_duplicates(self, folders):
1923
return duplicates.remove_duplicates(folders)
2024

2125
def find_large_files(self, folder):
22-
return largefiles.find_large_files(folder)
26+
return largefiles.find_large_files(folder, self.file_size_limit)
2327

2428
def find_disk_utilization(self):
2529
return diskutil.get_disk_usage()
@@ -28,7 +32,7 @@ def find_file_usage(self, folder):
2832
return diskutil.get_file_usage(folder)
2933

3034
def show_infrequent_files(self, folder):
31-
return infrequent.infrequent_files(folder)
35+
return infrequent.infrequent_files(folder, self.time_limit)
3236

3337
def delete_infrequent_files(self, files):
3438
return infrequent.delete_infrequent_files(files)

dist/gui.exe

26.9 KB
Binary file not shown.

0 commit comments

Comments
 (0)