-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathankisyncctl.py
executable file
·83 lines (68 loc) · 2.21 KB
/
ankisyncctl.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
#!/usr/bin/env python
import os
import sys
import getpass
import ankisyncd.config
from ankisyncd.users import SqliteUserManager
config = ankisyncd.config.load()
AUTHDBPATH = config['auth_db_path']
COLLECTIONPATH = config['data_root']
def usage():
print("usage: {} <command> [<args>]".format(sys.argv[0]))
print()
print("Commands:")
print(" adduser <username> - add a new user")
print(" deluser <username> - delete a user")
print(" lsuser - list users")
print(" passwd <username> - change password of a user")
def adduser(username):
password = getpass.getpass("Enter password for {}: ".format(username))
user_manager = SqliteUserManager(AUTHDBPATH, COLLECTIONPATH)
user_manager.add_user(username, password)
def deluser(username):
user_manager = SqliteUserManager(AUTHDBPATH, COLLECTIONPATH)
try:
user_manager.del_user(username)
except ValueError as error:
print("Could not delete user {}: {}".format(username, error), file=sys.stderr)
def lsuser():
user_manager = SqliteUserManager(AUTHDBPATH, COLLECTIONPATH)
try:
users = user_manager.user_list()
for username in users:
print(username)
except ValueError as error:
print("Could not list users: {}".format(error), file=sys.stderr)
def passwd(username):
user_manager = SqliteUserManager(AUTHDBPATH, COLLECTIONPATH)
if username not in user_manager.user_list():
print("User {} doesn't exist".format(username))
return
password = getpass.getpass("Enter password for {}: ".format(username))
try:
user_manager.set_password_for_user(username, password)
except ValueError as error:
print("Could not set password for user {}: {}".format(username, error), file=sys.stderr)
def main():
argc = len(sys.argv)
cmds = {
"adduser": adduser,
"deluser": deluser,
"lsuser": lsuser,
"passwd": passwd,
}
if argc < 2:
usage()
exit(1)
c = sys.argv[1]
try:
if argc > 2:
for arg in sys.argv[2:]:
cmds[c](arg)
else:
cmds[c]()
except KeyError:
usage()
exit(1)
if __name__ == "__main__":
main()