-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcsv_to_users.py
More file actions
executable file
·63 lines (42 loc) · 1.93 KB
/
csv_to_users.py
File metadata and controls
executable file
·63 lines (42 loc) · 1.93 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
#! /usr/bin/env python3
# Copyright 2017 Tom SF Haines
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import sys
import os.path
import json
import csv
# Takes a csv file of <name>, <internal name>, <project>, .. as many projects as you want and generates accounts in the user directory. Ignores any account that already exists and queries config.json to make sure it's doing the right thing. Note that it assumes images go in a path that starts 'images:'...
language = "english"
# Check we have a filename...
if len(sys.argv)<2:
print('Usage: ./csv_to_users.py <csv file>')
sys.exit(1)
# Load the configuration...
f = open('config.json', 'r')
config = json.loads(f.read())
f.close()
# Load the csv file, and loop each line in turn...
for row in csv.reader(open(sys.argv[1], newline='')):
name = row[0].strip()
username = row[1].strip()
projects = [n.strip() for n in row[2:] if len(n)!=0]
fn = os.path.join(config['users'], username + '.json')
if not os.path.exists(fn):
print('Adding:')
print(' name = %s' % name)
print(' username = %s' % username)
print(' projects = %s' % str(projects))
f = open(fn, 'w')
json.dump({'ident' : username, "name" : name, "image" : "images::%s.jpg"%username, "projects" : projects, "language" : language}, f, indent=1)
f.close()
else:
print('%s already exists - skipping' % username)