forked from dblinkhorn/file_renamer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
renamer_ui.py
181 lines (153 loc) · 6.26 KB
/
renamer_ui.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
from tkinter import *
from tkinter import ttk
from tkinter import filedialog
import file_renamer as fr
def select_folder():
global target_path
selected_path = filedialog.askdirectory()
target_path.set(selected_path)
target_path_text.delete(1.0, END)
target_path_text.insert(1.0, selected_path)
def add_replacement_key():
replacement_keys.append(StringVar())
# entry value will be bound to the StringVar appended above
ttk.Entry(
replacement_frame, textvariable=replacement_keys[-1], width=15).grid(
column=0, row=len(replacement_keys)+1, pady=4)
def add_replacement_value():
replacement_values.append(StringVar())
ttk.Entry(
replacement_frame,
textvariable=replacement_values[-1], width=15).grid(
column=1, row=len(replacement_values)+1, pady=4, padx=12)
def set_case():
if case_value.get() == 'lowercase':
fr.lowercase = True
if case_value.get() == 'uppercase':
fr.uppercase = True
def set_replacements():
# build replacements dictionary from user input
for key, value in zip(replacement_keys, replacement_values):
key = key.get()
value = value.get()
fr.replacements[key] = value
def apply_rename():
set_case()
set_replacements()
fr.run_renamer(target_path)
def invalid_replacements():
invalid = False
for key, value in zip(replacement_keys, replacement_values):
if not key.get() or not value.get():
invalid = True
return invalid
def confirm_rename():
modal_root = Tk()
modal_root.attributes('-topmost', True)
modal_root.title("Confirm Rename")
modal_frame = ttk.Frame(
modal_root, padding="12 12 12 12")
modal_frame.grid(column=0, row=0)
if not target_path.get():
empty_path_msg = (
"You must specify a valid target directory. Please try again.")
ttk.Label(modal_frame, text=empty_path_msg).grid(column=0, row=1)
ttk.Button(
modal_frame, text="Okay", command=modal_root.destroy).grid(
sticky=E, column=0, row=2, pady=(12, 0))
return
if invalid_replacements():
invalid_replacements_msg = (
"Every 'Target' and 'Replacement' must have a value.")
ttk.Label(modal_frame, text=invalid_replacements_msg).grid(
column=0, row=1)
ttk.Button(
modal_frame, text="Okay", command=modal_root.destroy).grid(
sticky=E, column=0, row=2, pady=(12, 0))
return
# get total files & dirs that will be inspected
file_count, dir_count = fr.get_counts(target_path.get())
no_files_msg = '\nNo files found. Please select another directory.'
if file_count > 0:
singular_subdir_string = 'sub-directory'
plural_subdir_string = 'sub-directories'
# determine singular or plural
subdir_string = (singular_subdir_string if dir_count == 1
else plural_subdir_string)
subdirs_string = f', including {dir_count} {subdir_string},'
count_string = (f'\n{file_count} files'
f'{subdirs_string if dir_count else ""} '
'will be affected.\n')
files_msg = count_string if file_count else no_files_msg
# this will be rendered on the confirm modal
confirm_message = (f'\nSelected target directory: {target_path.get()}'
f'\n{files_msg}'
'\nAre you sure you wish to proceed '
'with this rename operation?')
ttk.Label(modal_frame, text=confirm_message).grid(
column=0, row=1, pady=(0, 12))
ttk.Button(
modal_frame, text="Confirm",
command=lambda: [apply_rename(), modal_root.destroy()]).grid(
sticky=E, column=0, row=2)
ttk.Button(
modal_frame, text="Cancel", command=modal_root.destroy).grid(
sticky=E, column=1, row=2, padx=(12, 0))
else:
ttk.Label(modal_frame, text=no_files_msg).grid(column=0, row=1)
ttk.Button(
modal_frame, text="Okay", command=modal_root.destroy).grid(
sticky=E, column=0, row=2, pady=(12, 0))
# these hold the StringVars that will comprise the key/value pairs for
# 'replacements' dictionary in 'file_renamer.py'
replacement_keys = []
replacement_values = []
root = Tk()
root.title("File Renamer")
mainframe = ttk.Frame(root, padding="12 12 12 12")
mainframe.grid(column=0, row=0)
# target folder
target_path = StringVar()
target_path_text = Text(mainframe, height=1, width=50)
target_path_text.grid(column=0, row=1, padx=(12, 0))
target_path_label = ttk.Label(
mainframe, text='1. Select target folder:').grid(
sticky=W, column=0, row=0)
select_folder_btn = ttk.Button(
mainframe, text="Browse", command=select_folder).grid(
column=1, row=1, padx=(12, 0))
# force case
case_value = StringVar()
case_value.set(None)
lowercase_check = Radiobutton(
mainframe, text=" Lowercase", variable=case_value,
value="lowercase").grid(column=0, row=3, sticky=W)
lowercase_label = ttk.Label(mainframe, text="2. Force case (optional):").grid(
sticky=W, column=0, row=2, pady=(18, 0))
uppercase_check = Radiobutton(
mainframe, text=" Uppercase", variable=case_value,
value="uppercase").grid(column=0, row=4, sticky=W)
# replacement frame
replacement_frame = ttk.Frame(mainframe, padding=("10 10 0 0"))
replacement_frame.grid(sticky=W, column=0, row=6, pady=(12, 0))
# replacement rules
replacement_key_text = Text(mainframe, height=1, width=30)
replacement_label = ttk.Label(
mainframe, text="3. Add replacement rules:").grid(
sticky=W, column=0, row=5, pady=(18, 0))
replacement_key_label = ttk.Label(replacement_frame, text="Target:").grid(
sticky=W, column=0, row=0)
replacement_value_label = ttk.Label(
replacement_frame, text="Replacement:").grid(
sticky=W, column=1, row=0, padx=12)
replacement_add_btn = ttk.Button(
mainframe, text="Add",
command=lambda: [add_replacement_key(), add_replacement_value()]).grid(
column=0, row=5, padx=(24, 0), pady=(12, 0))
# apply rename
apply_rename_btn = ttk.Button(
mainframe, text="Apply", command=confirm_rename).grid(
sticky=SE, column=1, row=6)
add_replacement_key()
add_replacement_value()
root.mainloop()