-
Notifications
You must be signed in to change notification settings - Fork 0
/
Password Grebber.pyw
164 lines (123 loc) · 4.55 KB
/
Password Grebber.pyw
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
"""
The GUI version of Password Grebber
"""
from functools import partial
import tkinter as tk
from tkinter import ttk
import os
import sys
import generator as core
import datetime as dt
from language import Language
def get_path(relative_path: str) -> str:
"""
Transform the given relative path to an absolute path
:param relative_path: the relative path to absolute
:return: the absolute path
"""
try:
base_path = sys._MEIPASS
except:
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)
lang = Language(get_path("assets/languages.json"))
def logger(log: tk.Label, text: str) -> None:
"""
logs informations in the footer and in the console
:param log: the label where the text should be put
:param text: the text to log
:return: None
"""
print(f"[{dt.datetime.now()}] {text}")
log.configure(text=text)
def paste(window: tk.Tk, string_var: tk.StringVar, log: tk.Label) -> None:
"""
Paste the text in the clipboard in the given StringVar
:param window: the window from where the clipboard is get
:param string_var: the string variable where the clipboard content is pasted
:param log: the label where the logging infos whould be
:return: None
"""
try:
clipboard_key = window.clipboard_get()
string_var.set(clipboard_key)
logger(log, lang.pasted_log.format(repr(clipboard_key)))
except:
logger(log, lang.empty_clipboard_log)
def generate(window: tk.Tk, string_var: tk.StringVar, mode: tk.IntVar, log: tk.Label) -> None:
"""
Generate and copy the password to the clipboard
:param window: the window from where the clipboard is get
:param string_var: the string variable where the key is get
:param mode: the mode of the password generation
:param log: the label where the logging infos whould be
:return: None
"""
gen_key = string_var.get()
if not gen_key:
paste(window, string_var, log)
gen_key = string_var.get()
if gen_key:
gen_key = core.generate(gen_key, mode.get())
window.clipboard_clear()
window.clipboard_append(gen_key)
logger(log, lang.generation_log.format(repr(string_var.get()), repr(core.modes[mode.get()])))
else:
logger(log, lang.empty_key_log)
def main() -> None:
"""
The main code of the program
:return: None
"""
window = tk.Tk()
window.title(lang.app_name)
window.resizable(False, False)
window.call('wm', 'iconphoto', window._w, tk.PhotoImage(file=get_path('assets/logo.png')))
body = tk.Frame(window)
body.pack()
footer = tk.Frame(window, background='white', height=17)
footer.pack_propagate(0)
footer.pack(fill=tk.X)
img = tk.PhotoImage(file=get_path('assets/info.png'))
tk.Label(footer, image=img, background='white').pack(side='left')
log = tk.Label(footer, text="...", bg='white', anchor="w")
log.pack(side='left')
string_var = tk.StringVar()
string_var.set("")
text_field = ttk.Entry(body, width=45, textvariable=string_var)
text_field.pack(pady=30, padx=80)
text_field.focus()
radio_frame = tk.Frame(body)
radio_frame.pack()
mode = tk.IntVar(value=0)
radio_buttons = []
for index, elem in enumerate(core.modes):
radio_buttons.append(ttk.Radiobutton(radio_frame, text=elem, variable=mode, value=index))
radio_buttons[index].pack(fill=tk.X)
button_frame = tk.Frame(body)
button_frame.pack(pady=30)
generation_button = ttk.Button(button_frame, text=lang.generation_button, command=partial(generate, window, string_var, mode, log), width=15)
generation_button.pack(side="left", padx=15)
paste_button = ttk.Button(button_frame, text=lang.paste_button, command=partial(paste, window, string_var, log), width=15)
paste_button.pack(side="right", padx=15)
def on_key_press(event: tk.Event) -> None:
"""
The triggered function when a keyboard key is pressed
:param event: the pressed key
:return: None
"""
if event.char != "\n" and event.char != "\r":
return
f = window.focus_get()
if f == text_field or f == generation_button:
generate(window, string_var, mode, log)
elif f == paste_button:
paste(window, string_var, log)
else:
for index, elem in enumerate(radio_buttons):
if f == elem:
mode.set(index)
window.bind('<KeyPress>', on_key_press)
window.mainloop()
if __name__ == "__main__":
main()