-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.py
210 lines (181 loc) · 5.91 KB
/
example.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
from serializer import Serializer
from data_base import DataBase
from user import UserData
from password_generator import generate_random_password
import pyperclip
import sys
import getpass
import tests
AUTHOR = "Yablonskyi Nazarii (Nazarwadim)"
help_string = f"\n **Help menu** \n \
h - help\n \
e - exit\n \
a - add new password\n \
r - remove password from index\n \
s - show all(passwords will be hidden)\n \
cl - copy login\n \
cp - copy password\n \
ch - change existing\n \
au - author\n \
--------------------------------------------"
data_base = DataBase(Serializer())
def get_input_password() -> str:
action = input("Generate random password? Y/N:").lower()
if action == 'n':
while True:
password = getpass.getpass("Password: ")
verify_password = getpass.getpass("Password second time: ")
if(password == verify_password):
break
print("Invalid password verify. Both passwords must be same!")
return password
try:
length = int(input("Print password length(can`t be larger than 1000):"))
if length <= 0 or length > 1000:
raise Exception()
return generate_random_password(length)
except Exception:
print("Invalid length. Generating 12 length password.")
return generate_random_password(12)
def get_input_position() -> int:
position = input("Print index:")
return int(position)
def prog_end():
global data_base
data_base.close()
print('Data saved. Exit.')
sys.exit(0)
def show_all():
global data_base
user_data_list = data_base.get_data()
counter = 0
for user_data in user_data_list:
counter += 1
print(str(counter) + ')',user_data, '\n')
def on_add_password():
global data_base
user_data = UserData()
user_data.title = input("Input title:")
user_data.description = input("Input description(can be skipped by pressing 'enter'):")
user_data.email = input("Input email(can be skipped):")
user_data.login = input("Input login(skip?):")
user_data.password = get_input_password()
data_base.add_user_data(user_data)
def on_remove_password():
global data_base
position = get_input_position()
try:
sure = input("Sure: Y/N").lower()
if sure == "n":
return
data_base.remove_user_data(position - 1)
except Exception:
print("Invalid position.")
def on_copy_login():
global data_base
position = get_input_position()
try:
user_data = data_base.get_user_data(position - 1)
pyperclip.copy(user_data.login)
input("Enter anything to remove login from clipboard.")
pyperclip.copy(' ')
except Exception:
print("Invalid position.")
def on_copy_password():
global data_base
position = get_input_position()
try:
user_data = data_base.get_user_data(position - 1)
pyperclip.copy(user_data.password)
input("Enter anything to remove login from clipboard.")
pyperclip.copy(' ')
except Exception:
print("Invalid position.")
def on_change():
global data_base
position = get_input_position()
try:
user_data = data_base.get_user_data(position - 1)
except Exception:
print("Invalid position.")
return
action = input("Input t to change title, d to description, e to email, p to password, l to login\n")
if action == 't':
user_data.title = input("Input new title:")
elif action == 'p':
user_data.password = get_input_password()
elif action == 'd':
user_data.description = input("Input new description:")
elif action == 'e':
user_data.email = input("Input new email:")
elif action == 'l':
user_data.login = input("Input new login:")
def on_start_use():
global data_base
global help_string
global AUTHOR
c : str = " "
while c != 'e':
c = input("'h' for commands list:")
if c == 'h':
print(help_string)
elif c == 'a':
on_add_password()
elif c == 'r':
on_remove_password()
elif c == 's':
show_all()
elif c == 'cl':
on_copy_login()
elif c == 'cp':
on_copy_password()
elif c == 'ch':
on_change()
elif c == 'au':
print(AUTHOR)
def on_search_db():
global data_base
dbs = data_base.get_available_db_names()
print("Available password bases:", dbs)
if(len(dbs) == 0):
print("You have not existing data bases.")
db = input("Data base(--new to create new one):")
if(db == '--new'):
while True:
password = getpass.getpass("Password: ")
verify_password = getpass.getpass("Password second time: ")
if(password == verify_password):
break
print("Invalid password verify. Both passwords must be same!")
filename = input("Input filename: ")
data_base.new(password, filename)
return
if not db in dbs:
print("Incorrect data base. Exit.")
exit()
counter = 3
while(counter != 0):
password = getpass.getpass("Password: ")
try:
data_base.open(password, db)
break
except Exception:
print("Incorrect password! Try again.")
counter -= 1
else:
print("Too many attempts. Exit.")
exit()
def main():
try:
print('Enter Ctrl+C or Ctrl+D to exit.')
global data_base
on_search_db()
on_start_use()
prog_end()
except:
prog_end()
if __name__ == "__main__":
if len(sys.argv) == 2 and sys.argv[1] == "--test":
tests.test()
else:
main()