<commit_before>import click

from .loader import load_yaml
from .blueprint import Blueprint
from .exceptions import ValidationError, YAMLError, BackendError


def get_blueprint(*files):
    try:
        return Blueprint.from_description(load_yaml(*files))
    except (YAMLError, ValidationError) as e:
        raise click.ClickException(e.message)
    except Exception as e:
        raise click.ClickException("Unexpected error during the blueprint "
                                   "loading: {}".format(e.message))


@click.group()
@click.version_option()
def cli():
    pass


@cli.group()
def run():
    pass


@run.command()
@click.option('--host', default='localhost', help="Database host address")
@click.option('--port', default=5432, type=int, help="Database host port")
@click.option('--db', help="Database name")
@click.option('--user', help="Postgresql user name used to authenticate")
@click.option('--password', help="Postgresql password used to authenticate")
@click.argument('files', nargs=-1, required=True)
def postgresql(host, port, db, user, password, files):
    blueprint = get_blueprint(*files)

    try:
        from populous.backends.postgres import Postgres

        backend = Postgres(database=db, user=user, password=password,
                           host=host, port=port)

        try:
            with backend.transaction() as trans:
                for item in blueprint:
                    if not item.total:
                        continue

                    label = 'Creating {} {}'.format(item.total, item.name)
                    with click.progressbar(label=label,
                                           length=item.total) as bar:
                        for progress in backend.generate(item, trans):
                            bar.update(progress)
        finally:
            backend.close()

    except BackendError as e:
        raise click.ClickException(e.message)


@cli.command()
@click.argument('files', nargs=-1, required=True)<commit_msg>Make run command more generic<commit_after>import importlib

import click

from .loader import load_yaml
from .blueprint import Blueprint
from .exceptions import ValidationError, YAMLError, BackendError


def get_blueprint(*files):
    try:
        return Blueprint.from_description(load_yaml(*files))
    except (YAMLError, ValidationError) as e:
        raise click.ClickException(e.message)
    except Exception as e:
        raise click.ClickException("Unexpected error during the blueprint "
                                   "loading: {}".format(e.message))


@click.group()
@click.version_option()
def cli():
    pass


@cli.group()
def run():
    pass


def _generic_run(modulename, classname, files, **kwargs):
    blueprint = get_blueprint(*files)

    try:
        try:
            module = importlib.import_module('.' + modulename,
                                             package='populous.backends')
            backend_cls = getattr(module, classname)
        except (ImportError, AttributeError):
            click.ClickException("Backend not found.")

        backend = backend_cls(**kwargs)

        try:
            with backend.transaction() as trans:
                for item in blueprint:
                    if not item.total:
                        continue

                    label = 'Creating {} {}'.format(item.total, item.name)
                    with click.progressbar(label=label,
                                           length=item.total) as bar:
                        for progress in backend.generate(item, trans):
                            bar.update(progress)
        finally:
            backend.close()

    except BackendError as e:
        raise click.ClickException(e.message)


@run.command()
@click.option('--host', default='localhost', help="Database host address")
@click.option('--port', default=5432, type=int, help="Database host port")
@click.option('--db', help="Database name")
@click.option('--user', help="Postgresql user name used to authenticate")
@click.option('--password', help="Postgresql password used to authenticate")
@click.argument('files', nargs=-1, required=True)
def postgresql(host, port, db, user, password, files):
    return _generic_run('postgres', 'Postgres', files, host=host, port=port,
                        database=db, user=user, password=password)


@cli.command()
@click.argument('files', nargs=-1, required=True)
{'fraction_non_alphanumeric': 0.11493992291997279, 'fraction_numerical': 0.002720471548401723, 'mean_word_length': 2.409582689335394, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4177304', 'n_tokens_mistral': 1205, 'n_tokens_neox': 1150, 'n_words': 305}