-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsv_user_example.py
44 lines (34 loc) · 1.24 KB
/
csv_user_example.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
import csv
import det_user_sync
def parse_userlist_csv(args: list) -> det_user_sync.SourceGroups:
filepath = args[0]
required_fields = [
"groupname",
"username",
"uid",
"gid",
"unix_username",
"unix_groupname",
"display_name",
]
groups = det_user_sync.SourceGroups()
with open(filepath, "r") as csvfile:
for ii, row in enumerate(csv.DictReader(csvfile)):
for required_field in required_fields:
if row.get(required_field) is None:
raise ValueError(
f"Could not find required field '{required_field}' in row {ii}: {row}"
)
if row["groupname"] not in groups:
groups[row["groupname"]] = det_user_sync.SourceUsers()
user = det_user_sync.SourceUser(
username=row["username"],
uid=int(row["uid"]),
gid=int(row["gid"]),
unix_username=row["unix_username"],
unix_groupname=row["unix_groupname"],
display_name=row["display_name"],
password=row["password"],
)
groups[row["groupname"]].append(user)
return groups