-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
executable file
·151 lines (113 loc) · 4.71 KB
/
app.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
import tkinter as tk
from tkinter.constants import *
from tkinter import filedialog
from tkinter.messagebox import showinfo
from os import path
from shutil import copyfile
main = tk.Tk()
main.geometry('500x200')
main.title('copyist')
main.resizable(False, False)
PADDING = 7
def choose_file(txt):
file_path = filedialog.askopenfilename()
if not file_path:
return
txt.delete(0, END)
txt.insert(0, file_path)
def choose_dir(txt):
dir_path = filedialog.askdirectory()
if not dir_path:
return
txt.delete(0, END)
txt.insert(0, dir_path)
def update_check_state(state, txt):
if state.get() == 1:
txt.config(state=NORMAL)
elif state.get() == 0:
txt.config(state=DISABLED)
def copy():
original_path = original_txt.get().strip()
filenames_path = filenames_txt.get().strip()
dest_path = dest_txt.get().strip()
extension = path.splitext(original_path)[1]
prefix = prefix_txt.get().lstrip()
suffix = suffix_txt.get().rstrip()
# If prefix or suffix is unchecked, prefix or suffix stays blank
if prefix_txt.cget('state') != NORMAL:
prefix = ''
if suffix_txt.cget('state') != NORMAL:
suffix = ''
# If any of the paths are invalid
is_invalid = False
if not path.isfile(original_path) or original_path == '':
original_txt.config(bg='tomato')
is_invalid = True
else:
original_txt.config(bg=ORIGINAL_TXT_COLOR)
if not path.isfile(filenames_path) or filenames_path == '':
filenames_txt.config(bg='tomato')
is_invalid = True
else:
filenames_txt.config(bg=ORIGINAL_TXT_COLOR)
if not path.isdir(dest_path) or dest_path == '':
dest_txt.config(bg='tomato')
is_invalid = True
else:
dest_txt.config(bg=ORIGINAL_TXT_COLOR)
if is_invalid:
return
names = []
with open(filenames_path, 'r') as fp:
for line in fp:
names.append(prefix + line.strip() + suffix)
for name in names:
copyfile(original_path, path.join(dest_path, name + extension))
showinfo('Done', 'Copying finished!')
# Widgets
original_lbl = tk.Label(main, text='Original: ', anchor=W, width=3)
original_txt = tk.Entry(main)
original_btn = tk.Button(main, text='Choose',
command=lambda: choose_file(original_txt))
filenames_lbl = tk.Label(main, text='Filenames: ', anchor=W, width=3)
filenames_txt = tk.Entry(main)
filenames_btn = tk.Button(main, text='Choose',
command=lambda: choose_file(filenames_txt))
dest_lbl = tk.Label(main, text='Destination: ', anchor=W, width=3)
dest_txt = tk.Entry(main)
dest_btn = tk.Button(main, text='Choose', command=lambda: choose_dir(dest_txt))
prefix_state = tk.IntVar()
prefix_txt = tk.Entry(main, state=DISABLED)
prefix_checkbox = tk.Checkbutton(main, text='Prefix', onvalue=1, offvalue=0,
variable=prefix_state,
command=lambda:
update_check_state(prefix_state,
prefix_txt))
suffix_state = tk.IntVar()
suffix_txt = tk.Entry(main, state=DISABLED)
suffix_checkbox = tk.Checkbutton(main, text='Suffix', onvalue=1, offvalue=0,
variable=suffix_state,
command=lambda:
update_check_state(suffix_state,
suffix_txt))
ORIGINAL_TXT_COLOR = original_txt.cget('bg')
copy_btn = tk.Button(main, text='Copy', command=copy)
# Apply widgets
main.rowconfigure((0, 1, 2, 3, 4), weight=1)
main.columnconfigure((0, 1, 2), weight=1)
original_lbl.grid(row=0, column=0, sticky=EW, padx=PADDING, pady=PADDING)
original_txt.grid(row=0, column=1, sticky=EW, padx=PADDING, pady=PADDING)
original_btn.grid(row=0, column=2, sticky=EW, padx=PADDING, pady=PADDING)
filenames_lbl.grid(row=1, column=0, sticky=EW, padx=PADDING, pady=PADDING)
filenames_txt.grid(row=1, column=1, sticky=EW, padx=PADDING, pady=PADDING)
filenames_btn.grid(row=1, column=2, sticky=EW, padx=PADDING, pady=PADDING)
dest_lbl.grid(row=2, column=0, sticky=EW, padx=PADDING, pady=PADDING)
dest_txt.grid(row=2, column=1, sticky=EW, padx=PADDING, pady=PADDING)
dest_btn.grid(row=2, column=2, sticky=EW, padx=PADDING, pady=PADDING)
prefix_checkbox.grid(row=3, column=0, sticky=EW, padx=PADDING, pady=PADDING)
prefix_txt.grid(row=3, column=1, sticky=EW, padx=PADDING, pady=PADDING)
suffix_checkbox.grid(row=4, column=0, sticky=EW, padx=PADDING, pady=PADDING)
suffix_txt.grid(row=4, column=1, sticky=EW, padx=PADDING, pady=PADDING)
copy_btn.grid(row=3, column=2, rowspan=2, sticky=NSEW,
padx=PADDING, pady=PADDING)
main.mainloop()