-
Notifications
You must be signed in to change notification settings - Fork 0
/
userManage.py
133 lines (114 loc) · 3.27 KB
/
userManage.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
# coding:utf8
import getopt
import hashlib
import json
import os
import sys
from libs.dbAPI import checkName, addUser, updateUser, deleteUser, readUsers, makeDir
__author__ = 'cyh'
def add(username, password):
(flag, msg) = addUser(username, password)
if not flag:
print "Error: ", msg
else:
print msg
def update(username, password):
(flag, msg) = updateUser(username, password)
if not flag:
print "Error: ", msg
else:
print msg
def delete(username):
(flag, msg) = deleteUser(username)
if not flag:
print "Error: ", msg
else:
print msg
def listUsers():
users = readUsers()
for i, user in enumerate(users.keys()):
print i + 1, ":", user
def control():
while True:
try:
select = int(raw_input("""
0. list All User
1. add User
2. update User
3. delete User
4. exit
select: """))
if select == 0:
listUsers()
elif select == 1:
username = str(raw_input("UserName:"))
password = str(raw_input("Password(At least 6):"))
add(username, password)
elif select == 2:
username = str(raw_input("UserName:"))
password = str(raw_input("Password(At least 6):"))
update(username, password)
elif select == 3:
username = str(raw_input("UserName:"))
delete(username)
else:
break
raw_input("Press Enter to continue ... ")
except Exception:
pass
if __name__ == '__main__':
makeDir()
help = """
python userManage.py [option][value]...
-u or --user [value] (user's name)
-p or --password [value] (user's password)
(-u xxx -p ****** to add a new user)
-d or --delete (user's name) delete a user
-s or --show (show all of user)
-c or (None) (show menu to manage users)
-h or --help (show this)
"""
try:
opts, args = getopt.getopt(sys.argv[1:], "hu:p:d:sc", ["help", "user", "password", "delete", "show"])
except getopt.GetoptError, err:
print str(err)
sys.exit(2)
isControl = True
isDelete = False
user = password = None
for o, a in opts:
if o in ("-h", "--help"):
print help
sys.exit()
elif o in ("-u", "--user"):
user = str(a)
isControl = False
elif o in ("-p", "--password"):
password = str(a)
isControl = False
elif o in ("-d", "--delete"):
user = str(a)
isDelete = True
isControl = False
elif o in ("-s", "--show"):
listUsers()
sys.exit()
elif o in ("-c",):
control()
sys.exit()
else:
assert False, "unhandled option"
if isControl:
control()
sys.exit()
if isDelete:
(flag, msg) = deleteUser(user)
if not flag:
print "Error: ", msg
else:
print msg
else:
if user is None or password is None:
print "Error: ", "please input -u xxx -p ******"
sys.exit()
add(user, password)