<commit_before>from __future__ import print_function

from functools import wraps

from flask_script import Manager, prompt, prompt_bool

from indico.core.db import DBMgr
from indico.util.console import cformat, prompt_email, prompt_pass, success, error, warning
from MaKaC.common.info import HelperMaKaCInfo
from MaKaC.errors import UserError
from MaKaC.user import AvatarHolder, Avatar, LoginInfo


IndicoAdminManager = Manager(usage="Manages administration actions")


def print_user_info(avatar):
    print()
    print('User info:')
    print("  First name: {}".format(avatar.getFirstName()))
    print("  Family name: {}".format(avatar.getFamilyName()))
    print("  Email: {}".format(avatar.getEmail()))
    print("  Affiliation: {}".format(avatar.getAffiliation()))
    print()


def with_zodb(fn):
    @wraps(fn)
    def wrapper(*args, **kwargs):
        with DBMgr.getInstance().global_connection(commit=True):
            return fn(*args, **kwargs)
    return wrapper


@IndicoAdminManager.option('-a', '--admin', action='store_true', dest="grant_admin",
                           help="Grants administration rights to new user")
@with_zodb
def user_create(grant_admin):
    """Creates new user"""
    avatar = Avatar()
    user_type = 'user' if not grant_admin else 'admin'

    print()
    name = prompt("New {} name".format(user_type))
    surname = prompt("New {} surname".format(user_type))
    organization = prompt("New {} organization".format(user_type))
    print()
    login = prompt("Enter username")
    email = prompt_email().encode('utf-8')
    password = prompt_pass().encode('utf-8')
<commit_msg>Make with_zodb rollback on Exceptions<commit_after>from __future__ import print_function

from functools import wraps

import transaction
from flask_script import Manager, prompt, prompt_bool

from indico.core.db import DBMgr
from indico.util.console import cformat, prompt_email, prompt_pass, success, error, warning
from MaKaC.common.info import HelperMaKaCInfo
from MaKaC.errors import UserError
from MaKaC.user import AvatarHolder, Avatar, LoginInfo


IndicoAdminManager = Manager(usage="Manages administration actions")


def print_user_info(avatar):
    print()
    print('User info:')
    print("  First name: {}".format(avatar.getFirstName()))
    print("  Family name: {}".format(avatar.getFamilyName()))
    print("  Email: {}".format(avatar.getEmail()))
    print("  Affiliation: {}".format(avatar.getAffiliation()))
    print()


def with_zodb(fn):
    @wraps(fn)
    def wrapper(*args, **kwargs):
        with DBMgr.getInstance().global_connection():
            try:
                result = fn(*args, **kwargs)
            except:
                transaction.abort()
                raise
            else:
                transaction.commit()
            return result
    return wrapper


@IndicoAdminManager.option('-a', '--admin', action='store_true', dest="grant_admin",
                           help="Grants administration rights to new user")
@with_zodb
def user_create(grant_admin):
    """Creates new user"""
    avatar = Avatar()
    user_type = 'user' if not grant_admin else 'admin'

    print()
    name = prompt("New {} name".format(user_type))
    surname = prompt("New {} surname".format(user_type))
    organization = prompt("New {} organization".format(user_type))
    print()
    login = prompt("Enter username")
    email = prompt_email().encode('utf-8')
    password = prompt_pass().encode('<PASSWORD>')

{'fraction_non_alphanumeric': 0.12756132756132757, 'fraction_numerical': 0.0008658008658008658, 'mean_word_length': 3.7156462585034014, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7015403', 'n_tokens_mistral': 1040, 'n_tokens_neox': 1009, 'n_words': 256}