Skip to content

Instantly share code, notes, and snippets.

@andreinechaev
Last active July 3, 2018 17:56
Show Gist options
  • Save andreinechaev/5006fea4f0c17c323942d351eb7b01ca to your computer and use it in GitHub Desktop.
Save andreinechaev/5006fea4f0c17c323942d351eb7b01ca to your computer and use it in GitHub Desktop.
Creates users and groups from a given CSV file.
from time import sleep
from nuxeo.client import Nuxeo
from nuxeo.exceptions import HTTPError
from nuxeo.users import User
from nuxeo.groups import Group
import csv
import argparse
def get_index(headers: iter):
arr = []
for h in range(len(headers)):
arr.append((headers[h], h))
return arr
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Import user and groups to your instance')
parser.add_argument('--host', type=str,
help='address of your instance followed by `nuxeo` path segment. for instance '
'http://localhost:8080/nuxeo',
default='http://localhost:8080/nuxeo')
parser.add_argument('--path', type=str, help='full path to csv file with user/group definition', required=True)
parser.add_argument('--user', type=str, help='Username of the acting user', default='Administrator')
parser.add_argument('--password', type=str, help='Password of the acting user', default='Administrator')
parser.add_argument('--type', type=str, help='Type of the document to create; user | group', default='user')
args = parser.parse_args()
nuxeo = Nuxeo(host=args.host, auth=(args.user, args.password))
with open(args.path, 'r') as f:
reader = csv.reader(f, quotechar='|')
header_index = get_index(next(reader))
for row in reader:
data = {}
for (field, index) in header_index:
val = row[index]
if ',' in val:
val = [x.strip() for x in val.split(',') if len(x.strip()) > 0]
data[field] = val
try:
if args.type is 'user':
user = User(properties=data)
user = nuxeo.users.create(user)
print(f"User: {user.as_dict()['properties']['username']} created")
elif args.type is 'group':
group = Group(properties=data)
group = nuxeo.groups.create(group)
print(f"Group: {group.as_dict()['properties']['groupname']} created")
except HTTPError as err:
print(f"Row #{reader.line_num} data {data} error {err}")
if reader.line_num % 32 == 0:
sleep(1)
@andreinechaev
Copy link
Author

An example of CSV:

username,password,firstName,lastName,email,company,groups
Paul,1234,Paul,McCartney,paul@nx.com,NX,|Librarian,administrators|

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment