-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore.py
More file actions
289 lines (237 loc) · 8.46 KB
/
core.py
File metadata and controls
289 lines (237 loc) · 8.46 KB
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# ----------- CORE ----------- #
# Author: Mudit Rustagi
# 06 April 2020
# 15:37
# --Hello_World --
# If you dont know what this is about, give the README a glance.
# If you would like to contribute, please do so freely.
from tkinter import filedialog
from tkinter import messagebox
from datetime import datetime
from langdetect import detect
from tkinter import *
import tkinter as tk
from os import walk
import webbrowser
import database
import os
import re
# Alternate the entry state according to button press
def edit():
global folder
if(folder["state"]=="normal"):
folder.config(state='disabled')
else:
folder.config(state='normal')
submit["state"]="normal"
# Reset the entry text
def reset():
global texti,folder
texti.set(" ")
folder.config(state='disabled')
submit["state"]="disabled"
# Browse ask directory functiality
def browse():
global texti,filename
filename = filedialog.askdirectory()
texti.set(" "+filename)
submit["state"]="normal"
# Deletes the file
def deleteIt(i):
global lang
for i in lang[i]:
os.remove(i)
# Updates the timer clock every one second
def update_clock():
global x
endTime=datetime.now()
elap=str(endTime-startTime)
statusbar.config(text=" "+elap[2:-7])
top.after(1000,update_clock)
# Ask closing confirmation
def on_closing():
if(messagebox.askquestion (' KIPPER','Are you sure you want to quit?',icon = 'error')=='yes'):
top.destroy()
# Get the list of files in a folder
def getListOfFiles(dirName):
listOfFiles=[]
for extension in database.EXTENSIONS:
listOfFiles.extend([os.path.join(dirName,file) for file in os.listdir(dirName) if file.endswith('.'+extension)])
return listOfFiles
# Submit button press functiality
def submit():
global filename,lang,outcome,texti
files=[]
filename=texti.get()
filename=filename.strip()
# Get all the list of subdirectory in the folder
subdirs = [x[0] for x in os.walk(filename)]
for i in subdirs:
x=getListOfFiles(i)
files.extend(x)
for filepath in files:
# opening the file
file = open(filepath, "r",encoding="utf8")
lines = file.readlines()
file.close()
# Generating plain text with no extra characters (digits, etc)
# Plain text easily detects the language
# Using Regex expression
text = ''
for line in lines:
if re.search('^[0-9]+$', line) is None and re.search('^[0-9]{2}:[0-9]{2}:[0-9]{2}', line) is None and re.search('^$', line) is None:
text += ' ' + line.rstrip('\n')
text = text.lstrip()
# shrinking the text to 200 characters only
text=text[max(0,len(text)-200):len(text)]
# Find the language using detect in langdetect library
res=detect(text)
# Append the result values to variables
try:
lang[res].append(filepath)
except:
lang[res]=[filepath]
outcome[res]=0
# Calling a external window
overlay()
# Hover Animation functions
def on_enter(e):
e.widget['background'] = '#BCBABB'
def enter(e):
e.widget['borderwidth']=1
def on_leave(e):
e.widget['background'] = '#a19d9f'
def leave(e):
e.widget['borderwidth']=0
# Generating a toplevel window for language selection
def overlay():
# Generating a toplevel with all the langauges found
global outcome
# Top level window customization
window = Toplevel(top)
window.grab_set()
window.geometry("380x350+600+300")
window.iconbitmap(r"logo.ico")
window.title(" KIPPER : Stay Relevant")
window.resizable(width="false", height="false")
# Adding Label
sec = Label(window, text="Select the needed Languages: ")
sec.place(x=20,y=20)
# Adding Checkbutton in a 2X1 order using custom ordering
j=0
for i,machine in enumerate(outcome):
outcome[machine] = IntVar()
l = Checkbutton(window,text=database.LANGUAGES[machine], variable=outcome[machine])
l.deselect()
l.place(x=(40 if i%2==0 else 200),y=50+j*30)
if i%2==1:
j=j+1
def delete():
# Confirmation Box for operation
if(messagebox.askquestion (' KIPPER','Are you sure you want to proceed?')=='yes'):
# Kill the toplevel
window.destroy()
global outcome
for i in outcome:
if not outcome[i].get():
deleteIt(i)
# Operation successful popup
messagebox.showinfo (' KIPPER','Operation Successful')
# Adding Save Button
proceed=Button(window,text="SAVE SELECTED",height=1,width=20, bg='#a19d9f',command=delete)
proceed.place(x=20,y=280)
proceed.bind("<Enter>", on_enter)
proceed.bind("<Leave>", on_leave)
# Adding status bar
white=Frame(window,height=50,width=1000,bg="#fff")
white.place(x=0,y=329)
seperator=Frame(window,height=1,width=1000,bg="#ababab")
seperator.place(x=0,y=328)
# Adding copyright tag static
static= Button(window, bg="#fff",text="%s Mudit Rustagi " % (u"\N{COPYRIGHT SIGN}"), command =website,borderwidth=0,anchor=E)
static.place(x=281,y=329)
static.bind("<Enter>", enter)
static.bind("<Leave>", leave)
window.mainloop()
# Quick Links Added
def website():
webbrowser.open_new_tab(database.WEBSITE)
def github():
webbrowser.open_new_tab(database.GITHUB)
def about():
webbrowser.open_new_tab(database.ABOUT)
def contribute():
webbrowser.open_new_tab(database.CONTRIBUTE)
#--Start Main Code:
# filename: browsed file name
filename=""
# lang: computed languages in folder
lang={}
# outcome: langauges selected
outcome={}
# startime recorded for timer
startTime=datetime.now()
#The main window intialization top
top = Tk()
top.geometry("550x216+600+300")
top.iconbitmap(r"logo.ico")
top.title(" KIPPER : Stay Relevant")
top.resizable(width="true", height="true")
top.after(1000,update_clock)
top.protocol("WM_DELETE_WINDOW", on_closing)
#Adding Menu Bar menubar
menubar = Menu(top, tearoff=True)
menubar.add_command(label=" Website ",command=website)
menubar.add_command(label=" Github ",command=github)
menubar.add_command(label=" Contribute ",command=contribute)
menubar.add_command(label=" About ",command=about)
top.config(menu=menubar)
#Adding Seperator seperator
seperator=Frame(top,height=1,width=1000,bg="#ababab")
seperator.place(x=0,y=0)
#Adding Browse Button browse
browse = Button(text="Step 1: BROWSE", width=20,command=browse,bg='#a19d9f')
browse.place(x=20,y=20)
browse.bind("<Enter>", on_enter)
browse.bind("<Leave>", on_leave)
#Adding Entry folder(variable text)
texti = StringVar()
folder = Entry(top,width=65,textvariable=texti)
folder.config(state='disabled')
folder.insert(END, '')
folder.place(x=20, y=70)
#Adding Edit Button editButton
editImage= PhotoImage(file = r"./icons/edit.gif").subsample(7)
editButton = Button(top, image = editImage,command=edit,borderwidth=0)
editButton.place(x=428,y=70)
editButton.bind("<Enter>", enter)
editButton.bind("<Leave>", leave)
#Adding Reset Button resetButton
resetImage = PhotoImage(file = r"./icons/reset.gif").subsample(2)
resetButton = Button(top, image = resetImage,command=reset,borderwidth=0)
resetButton.place(x=450,y=70)
resetButton.bind("<Enter>", enter)
resetButton.bind("<Leave>", leave)
#Adding Submit Button submit
submit=Button(top,text="Step 2: SUBMIT",height=1,width=20, bg='#a19d9f',command=submit)
submit.place(x=20,y=115)
submit.bind("<Enter>", on_enter)
submit.bind("<Leave>", on_leave)
submit["state"]="disabled"
#Adding white bar white
white=Frame(top,height=50,width=1000,bg="#fff")
white.place(x=0,y=175)
#Adding Seperator seperator
seperator=Frame(top,height=1,width=1000,bg="#ababab")
seperator.place(x=0,y=174)
#Adding timer statusbar
timer = PhotoImage(file = r"./icons/timer.gif").subsample(2,2)
statusbar = Label(top, text=" 00:00",bg="#fff", image = timer,compound = LEFT, anchor=W)
statusbar.place(x=5,y=175)
#Adding copyright tag static
static = Button(top, bg="#fff",text="%s Mudit Rustagi " % (u"\N{COPYRIGHT SIGN}"), command =website,borderwidth=0,anchor=E)
static.place(x=390,y=175)
static.bind("<Enter>", enter)
static.bind("<Leave>", leave)
#--End Main Code--
top.mainloop()