-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathkeeporsweep.py
executable file
Β·191 lines (144 loc) Β· 5.64 KB
/
keeporsweep.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/usr/bin/env python3
# π»πποΈ Keep or Sweep, v0.1.0 for desktop
# Show a random file so you can clean your stuff
# Simply make executable and click (or run as python3 keeporsweep.py)
# http://keeporsweep.net
import tkinter as tk
from tkinter import *
import PIL
from PIL import ImageTk,Image
import os
from random import shuffle
from send2trash import send2trash
import subprocess
try:
import sys
except:
pass
root = tk.Tk()
element_list = []
path = os.path.dirname(os.path.abspath(__file__))
# Ensure window fits screen
# Preview canvas to half screen height and margin/padding relatively
screen_height = root.winfo_screenheight()
canvas_width = int(screen_height/2)
canvas_height = int(screen_height/2)
margin = int(screen_height/40)
padding = margin
font_size = "-size 12"
font_size_weight = "-size 12 -weight bold"
class Application(tk.Frame):
def __init__(self, master=None):
super().__init__(master, bg="white")
self.pack()
# Set up basic layout
self.create_widgets()
# Get the randomized file list
self.random_files(path)
# Load first element
self.element_preview()
self.element_text()
# Return random list of all files
def random_files(self, path):
global element_list
# Get list of all files
for root, dirs, files in os.walk(path):
# Ignore hidden folders
dirs[:] = [d for d in dirs if not d.startswith('.')]
for file in files:
element_list.append(os.path.join(root, file))
# Return it in random order
shuffle(element_list)
# Initial set up of all interface elements
def create_widgets(self):
# Preview container
self.canvas = Canvas(self, width=canvas_width, height=canvas_height, bg="white")
self.canvas.pack(side="top", expand=1, padx=margin, pady=margin)
# Element title & detail text
self.title = Label(self, font=font_size_weight, bg="white", width="40")
self.title.pack()
self.details = Label(self, font=font_size, fg="#aaa", bg="white", width="40")
self.details.pack()
# Keep button
self.keep = tk.Button(self, text="Keep", foreground="white", activeforeground="white", background="#0082c9", activebackground="#0072b0", command=self.keep_element, cursor="heart", bitmap="warning", compound="top", relief="flat", font=font_size_weight, default="active")
self.keep.pack(side="right", ipadx=padding, ipady=padding, padx=margin, pady=margin)
# Sweep button
self.sweep = tk.Button(self, text="Sweep", foreground="white", activeforeground="white", background="#e9322d", activebackground="#e51d18", command=self.sweep_element, cursor="pirate", bitmap="error", compound="top", relief="flat", font=font_size_weight)
self.sweep.pack(side="left", ipadx=padding, ipady=padding, padx=margin, pady=margin)
# Display preview of current element
def element_preview(self):
self.canvas.delete("all")
element_current = element_list[0]
# Image handling
# if element_current.lower().endswith(('.png', '.jpg', '.jpeg', '.gif')):
try:
self.image_raw = Image.open(element_list[0])
# Make image fill canvas
if self.image_raw.size[0] > self.image_raw.size[1]:
# Wider than tall
self.image_raw.thumbnail((self.image_raw.size[0], canvas_height+1), PIL.Image.BICUBIC)
else:
# Taller than wide
self.image_raw.thumbnail((canvas_width+1, self.image_raw.size[1]), PIL.Image.BICUBIC)
self.image = ImageTk.PhotoImage(self.image_raw)
self.canvas.bind("<Button-1>",self.show_file)
self.canvas.create_image(canvas_width/2, canvas_height/2, anchor="center", image=self.image)
except:
return
# Display title and details of current element
def element_text(self):
element_current = element_list[0]
# Splitting up the file path, removing current directory
element_relativepath = element_current[len(path):]
element_details, element_title = os.path.split(element_relativepath)
# Element title
self.title.config(text=element_title)
self.title.bind("<Button-1>",self.show_file)
# Element details
self.details.config(text=element_details)
self.details.bind("<Button-1>", self.show_folder)
#Open folder on clicking path
def show_folder(self, null_arg):
pathToFolder = os.path.dirname(os.path.abspath(element_list[0]))
self.open_file(os.path.realpath(pathToFolder))
# Open file on click file path
def show_file(self, null_arg):
pathToFolder = os.path.abspath(element_list[0])
self.open_file(pathToFolder)
#Open the file according to the OS platform
def open_file(self, filename):
if sys.platform == "win32":
os.startfile(filename)
else:
opener ="open" if sys.platform == "darwin" else "xdg-open"
subprocess.call([opener, filename])
# Move to next element
def next_element(self):
# Remove element from list
element_list.pop(0)
# Display next element
self.element_preview()
self.element_text()
# Pressing "Keep" button
def keep_element(self):
# Simply go to next element
self.next_element()
# Pressing "Sweep" button
def sweep_element(self):
# Flash on deletion for extra excitement
self.sweep.flash()
element_current = element_list[0]
# Delete current element
if os.path.isfile(element_current):
send2trash(element_current)
# Then go to next element
self.next_element()
app = Application(master=root)
app.master.title("Keep or Sweep")
app.master.configure(background="white")
# Center window on the screen
# https://stackoverflow.com/a/28224382
if sys.platform != "win32":
root.eval('tk::PlaceWindow %s center' % root.winfo_pathname(root.winfo_id()))
root.iconbitmap('images/icon.ico')
app.mainloop()