<commit_before># -*- coding: utf-8 -*-
#
# license: AGPL, see LICENSE for details. In addition we strongly encourage
#          you to buy us beer if we meet and you like the software.

"""A command line interface for AMIVApi."""
from os import listdir, remove
from os.path import join, isdir

from click import argument, echo, group, option, Path

from amivapi.bootstrap import create_app
from amivapi.cron import run_scheduled_tasks
from amivapi import ldap
from amivapi.groups.mailing_lists import update_group


@group()
def cli():
    """Manage amivapi."""


config_option = option("--config",
                       type=Path(exists=True, dir_okay=False, readable=True),
                       help="use specified config file")


@cli.command()
@config_option
def recreate_mailing_lists(config):
    """(Re-)create mailing lists for all groups.

    1. Delete all mailing list files.

    2. Create new mailing list files.

    For every group, we call the update_group function for this
    """
    app = create_app(config_file=config)
    directory = app.config.get('MAILING_LIST_DIR')
    prefix = app.config['MAILING_LIST_FILE_PREFIX']

    if not directory:
        echo('No directory for mailing lists specified in config.')
        return

    # Delete existing files
    if isdir(directory):
        for filename in listdir(directory):
            if filename.startswith(prefix):
                remove(join(directory, filename))

    # Create new files
    with app.app_context():
        groups = app.data.driver.db['groups'].find({})
        for g in groups:
            update_group(g, g)  # Use group as update and original


@cli.command()
@config_option
def cron(config):
    """Run scheduled tasks."""
    app = create_app(config_file=config)
    with app.app_context():
        run_scheduled_tasks()


@cli.command()
@config_option
@option('--all', 'sync_all', is_flag=True, help="Sync all users.")
@argument('nethz', nargs=-1)
def ldap_sync(config, sync_all, nethz):
    """Synchronize users with eth ldap.

    Examples:

        amivapi ldap_sync --all

        amivapi ldap_sync adietmue bconrad blumh
    """<commit_msg>Fix CLI typo (#248)<commit_after># -*- coding: utf-8 -*-
#
# license: AGPL, see LICENSE for details. In addition we strongly encourage
#          you to buy us beer if we meet and you like the software.

"""A command line interface for AMIVApi."""
from os import listdir, remove
from os.path import join, isdir

from click import argument, echo, group, option, Path

from amivapi.bootstrap import create_app
from amivapi.cron import run_scheduled_tasks
from amivapi import ldap
from amivapi.groups.mailing_lists import updated_group


@group()
def cli():
    """Manage amivapi."""


config_option = option("--config",
                       type=Path(exists=True, dir_okay=False, readable=True),
                       help="use specified config file")


@cli.command()
@config_option
def recreate_mailing_lists(config):
    """(Re-)create mailing lists for all groups.

    1. Delete all mailing list files.

    2. Create new mailing list files.

    For every group, we call the update_group function for this
    """
    app = create_app(config_file=config)
    directory = app.config.get('MAILING_LIST_DIR')
    prefix = app.config['MAILING_LIST_FILE_PREFIX']

    if not directory:
        echo('No directory for mailing lists specified in config.')
        return

    # Delete existing files
    if isdir(directory):
        for filename in listdir(directory):
            if filename.startswith(prefix):
                remove(join(directory, filename))

    # Create new files
    with app.app_context():
        groups = app.data.driver.db['groups'].find({})
        for g in groups:
            updated_group(g, g)  # Use group as update and original


@cli.command()
@config_option
def cron(config):
    """Run scheduled tasks."""
    app = create_app(config_file=config)
    with app.app_context():
        run_scheduled_tasks()


@cli.command()
@config_option
@option('--all', 'sync_all', is_flag=True, help="Sync all users.")
@argument('nethz', nargs=-1)
def ldap_sync(config, sync_all, nethz):
    """Synchronize users with eth ldap.

    Examples:

        amivapi ldap_sync --all

        amivapi ldap_sync adietmue bconrad blumh
    """
{'fraction_non_alphanumeric': 0.10525092936802974, 'fraction_numerical': 0.0025557620817843866, 'mean_word_length': 3.0922053231939164, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1013504', 'n_tokens_mistral': 1313, 'n_tokens_neox': 1261, 'n_words': 409}