initdb.py
6.45 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
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
#!/usr/bin/env python3
'''
Commandline utilizty to initialize and update student database
'''
# base
import csv
import argparse
import re
from string import capwords
from concurrent.futures import ThreadPoolExecutor
# installed packages
import bcrypt
import sqlalchemy as sa
# this project
from perguntations.models import Base, Student
# ===========================================================================
def parse_commandline_arguments():
'''Parse command line options'''
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
description='Insert new users into a database. Users can be imported '
'from CSV files in the SIIUE format or defined in the '
'command line. If the database does not exist, a new one '
'is created.')
parser.add_argument('csvfile',
nargs='*',
type=str,
default='',
help='CSV file to import (SIIUE)')
parser.add_argument('--db',
default='students.db',
type=str,
help='database file')
parser.add_argument('-A', '--admin',
action='store_true',
help='insert the admin user')
parser.add_argument('-a', '--add',
nargs=2,
action='append',
metavar=('uid', 'name'),
help='add new user')
parser.add_argument('-u', '--update',
nargs='+',
metavar='uid',
default=[],
help='users to update')
parser.add_argument('--pw',
default=None,
type=str,
help='set password for new and updated users')
parser.add_argument('-V', '--verbose',
action='store_true',
help='show all students in database')
return parser.parse_args()
# ===========================================================================
def get_students_from_csv(filename):
'''
SIIUE names have alien strings like "(TE)" and are sometimes capitalized
We remove them so that students dont keep asking what it means
'''
csv_settings = {
'delimiter': ';',
'quotechar': '"',
'skipinitialspace': True,
}
try:
with open(filename, encoding='iso-8859-1') as file:
csvreader = csv.DictReader(file, **csv_settings)
students = [{
'uid': s['N.º'],
'name': capwords(re.sub(r'\(.*\)', '', s['Nome']).strip())
} for s in csvreader]
except OSError:
print(f'!!! Error reading file "{filename}" !!!')
students = []
except csv.Error:
print(f'!!! Error parsing CSV from "{filename}" !!!')
students = []
return students
# ===========================================================================
def hashpw(student, password=None):
'''replace password by hash for a single student'''
print('.', end='', flush=True)
if password is None:
student['pw'] = ''
else:
student['pw'] = bcrypt.hashpw(password.encode('utf-8'),
bcrypt.gensalt())
# ===========================================================================
def insert_students_into_db(session, students):
'''insert list of students into the database'''
try:
session.add_all([Student(id=s['uid'], name=s['name'], password=s['pw'])
for s in students])
session.commit()
except sa.exc.IntegrityError:
print('!!! Integrity error. Users already in database. Aborted !!!\n')
session.rollback()
# ============================================================================
def show_students_in_database(session, verbose=False):
'''get students from database'''
users = session.query(Student).all()
total_users = len(users)
print('Registered users:')
if total_users == 0:
print(' -- none --')
else:
users.sort(key=lambda u: f'{u.id:>12}') # sort by number
if verbose:
for user in users:
print(f'{user.id:>12} {user.name}')
else:
print(f'{users[0].id:>12} {users[0].name}')
if total_users > 1:
print(f'{users[1].id:>12} {users[1].name}')
if total_users > 3:
print(' | |')
if total_users > 2:
print(f'{users[-1].id:>12} {users[-1].name}')
print(f'Total: {total_users}.')
# ============================================================================
def main():
'''insert, update, show students from database'''
args = parse_commandline_arguments()
# --- make list of students to insert/update
students = []
for csvfile in args.csvfile:
print('Adding users from:', csvfile)
students.extend(get_students_from_csv(csvfile))
if args.admin:
print('Adding user: 0, Admin.')
students.append({'uid': '0', 'name': 'Admin'})
if args.add:
for uid, name in args.add:
print(f'Adding user: {uid}, {name}.')
students.append({'uid': uid, 'name': name})
# --- password hashing
if students:
print('Generating password hashes', end='')
with ThreadPoolExecutor() as executor: # hashing in parallel
executor.map(lambda s: hashpw(s, args.pw), students)
print()
# --- database stuff
print(f'Using database: {args.db}')
engine = sa.create_engine(f'sqlite:///{args.db}', echo=False)
Base.metadata.create_all(engine) # Criates schema if needed
SessionMaker = sa.orm.sessionmaker(bind=engine)
session = SessionMaker()
if students:
print(f'Inserting {len(students)}')
insert_students_into_db(session, students)
for student_id in args.update:
print(f'Updating password of: {student_id}')
student = session.query(Student).get(student_id)
password = (args.pw or student_id).encode('utf-8')
student.password = bcrypt.hashpw(password, bcrypt.gensalt())
session.commit()
show_students_in_database(session, args.verbose)
session.close()
# ============================================================================
if __name__ == '__main__':
main()