-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanager.py
More file actions
138 lines (111 loc) · 3.89 KB
/
manager.py
File metadata and controls
138 lines (111 loc) · 3.89 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
import random
import string
import pyfiglet
from getpass import getpass
import argparse
from functools import wraps
import sys
from support import DB
from support.dbManage import store_in_db, fetch_from_db
from support.encrypt_decrypt import endec, is_encrypted
def lock() -> None:
if not is_encrypted(DB):
endec()
lock()
def interaction_wrapper(func: callable, *args, **kwargs) -> None:
try:
endec()
func(*args, **kwargs)
endec()
except:
lock()
def looper(func: callable) -> callable:
@wraps(func)
def mod_func(*args, **kwargs) -> None:
func(*args, **kwargs)
while not input('Wanna quit?(y/n) ').strip().lower().startswith('y'):
func(*args, **kwargs)
print()
return mod_func
def get_int(prompt: str) -> int:
while True:
try:
return int(input(prompt))
except:
continue
@looper
def interface_generator() -> None:
included = ''
sample_space = {
'uppercase alphabets': string.ascii_uppercase,
'lowercase alphabets': string.ascii_lowercase,
'decimal digits': string.digits,
'special characters': string.punctuation, 'whitespace': ' '
}
for name, sample in sample_space.items():
if not input(f"Want to include {name} (y/n) [defaults to y]: ").strip().lower().startswith('n'):
included += sample
print("Included")
else:
print("Left")
length = get_int("What length of password is desired? ")
pswd = ''.join(random.choices(included, k = length))
print(f"Password Generated: {pswd}")
if input('Wanna store this password in the database?(y/n) ').strip().lower().startswith('y'):
interface_registrar.__wrapped__(pswd)
@looper
def interface_registrar(pswd: str | None = None) -> None:
place, uname = None, None
while not place:
place = input('For which place registration is to be made? ')
while not uname:
uname = input('Your username for this place? ')
if not pswd:
while (pswd := getpass('Enter the password to store: ')) != getpass('Confirm Password: '):
print('Confirm Password and Password does\'t match. Retry!\n')
interaction_wrapper(store_in_db, place, uname, pswd, DB)
@looper
def interface_fetcher() -> None:
place = input('Search with place : ')
uname = input('Search with username : ')
show = input('Wanna show passwords?(y/n) : ').strip().lower().startswith('y')
interaction_wrapper(fetch_from_db, place, uname, show, DB)
def cli() -> None:
print(pyfiglet.figlet_format('Command line Interface'))
print('Usage:-')
print('\tstore - Pulls out storing in database interface')
print('\tfetch - Pulls out fetch from database interface')
print('\tgenerate - Pulls out password generation interface')
print('\tquit/exit - Bails out')
cmd_to_func = {
'store' : interface_registrar.__wrapped__,
'fetch' : interface_fetcher.__wrapped__,
'generate': interface_generator.__wrapped__,
'quit' : sys.exit,
'exit' : sys.exit,
}
while True:
cmd = input('> ').strip()
if cmd in cmd_to_func:
cmd_to_func[cmd]()
else:
print(f'Command not registered ({cmd}) : can be one of {list(cmd_to_func.keys())}')
if __name__ == '__main__':
parser = argparse.ArgumentParser(prog = 'UI', description = 'Command-Line Interface generator for password management')
parser.add_argument('-s', '--store', action = 'store_true', dest = 'store',
help = 'Pull out password storing interface')
parser.add_argument('-g', '--generator', action = 'store_true', dest = 'generate',
help = 'Pull out password generator interface')
parser.add_argument('-f', '--fetch', action = 'store_true', dest = 'fetch',
help = 'Pull out password fetching interface')
parser.add_argument('-i', '--interact', action = 'store_true', dest = 'interact',
help = 'Interact in command line interface with database')
args = parser.parse_args()
if args.store:
interface_registrar()
if args.generate:
interface_generator()
if args.fetch:
interface_fetcher()
if args.interact or not any(vars(args).values()):
cli()