-
Notifications
You must be signed in to change notification settings - Fork 1
/
console.py
50 lines (45 loc) · 1.38 KB
/
console.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
import utils.db as db
import getpass
def commander(query):
"""
Command parser for the admin console
Params:
query - A string containing the command or query from the console
Returns:
True if successful execution, False otherwise
"""
args = query.strip().split(" ")
if len(args) == 0:
return True
# >> list
# Lists out the admins inside the database
if args[0] == "list":
for admin in db.list_admin():
print admin
return True
# >> add username password
# Adds a new user with credentials username:password
if args[0] == "add" and len(args) == 3:
db.add_admin(args[1], args[2])
return True
# >> remove username
# Exits immediately if 'username' doesn't exist. Otherwise, it will prompt
# for the password of 'username', and if validated, will delete the user.
if args[0] == "remove" and len(args) == 2:
if not db.username_exists(args[1]):
return False
pword = str(getpass.getpass("Password: "))
if db.admin_exists(args[1], pword):
db.remove_admin(args[1], pword)
return True
else:
return False
# >> exit
# Exits the console
if args[0].upper() == "EXIT":
exit(0)
return False
while True:
q = str(raw_input(">> "))
if not commander(q):
print("FAILED")