<commit_before>#!/usr/bin/env python
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# Copyright (c) 2015 Digi International Inc. All Rights Reserved.
import json
import pprint

from core import WVA
import click
import os


CONFIG_DIR = os.path.abspath(os.environ.get('WVA_CONFIGDIR', os.path.expanduser("~/.wva")))


def load_config():
    try:
        with open(os.path.join(CONFIG_DIR, "config.json")) as f:
            return json.load(f)
    except (IOError, ValueError):
        return {}


def save_config(ctx):
    if not os.path.exists(CONFIG_DIR):
        os.makedirs(CONFIG_DIR)

    config = {
        "hostname": ctx.hostname,
        "username": ctx.username,
        "password": ctx.password,
    }

    # Write config to a file that is restricted to read/write access by current user
    fd = os.open(os.path.join(CONFIG_DIR, "config.json"), os.O_WRONLY | os.O_CREAT, 0o0600)
    with os.fdopen(fd, 'w') as f:
        f.write(json.dumps(config, indent=2))


def get_config_value(ctx, key, prompt, override_value, password=False):
    if override_value is not None:
        value = override_value
    elif ctx.config.get(key) is not None:
        value = ctx.config[key]
    else:
        value = click.prompt(prompt, hide_input=password)
        ctx.user_values_entered = True

    return value


def get_hostname(ctx, override_hostname=None):
    return get_config_value(ctx, 'hostname', "WVA Hostname", override_hostname)


def get_username(ctx, override_username=None):
    return get_config_value(ctx, 'username', "Username", override_username)


def get_password(ctx, override_password=None):
    return get_config_value(ctx, 'password', 'Password', override_password, password=True)


@click.group()
@click.option('--hostname', default=None, help='Force use of the specified hostname')
@click.option('--username', default=None, help='Force use of the specified username')
@click.option('--password', default=None, help='Force use of the specified password')
@click.pass_context
def cli(ctx, hostname, username, password):
    ctx.config = load_config()
    ctx.user_values_entered = False
    ctx.hostname = get_hostname(ctx, hostname)
    ctx.username = get_username(ctx, username)
    ctx.password = get_password(ctx, password)
    if ctx.user_values_entered:
        if click.confirm("Save new values to config file?"):
            save_config(ctx)

    ctx.wva = WVA(ctx.hostname, ctx.username, ctx.password)<commit_msg>cli: add --config-dir option to be used instead of environment variable<commit_after>#!/usr/bin/env python
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# Copyright (c) 2015 Digi International Inc. All Rights Reserved.
import json
import pprint

from core import WVA
import click
import os


def load_config(ctx):
    try:
        with open(os.path.join(ctx.config_dir, "config.json")) as f:
            return json.load(f)
    except (IOError, ValueError):
        return {}


def save_config(ctx):
    if not os.path.exists(ctx.config_dir):
        os.makedirs(ctx.config_dir)

    config = {
        "version": 1,  # may be used for migration purposes
        "hostname": ctx.hostname,
        "username": ctx.username,
        "password": ctx.password,
    }

    # Write config to a file that is restricted to read/write access by current user
    fd = os.open(os.path.join(ctx.config_dir, "config.json"), os.O_WRONLY | os.O_CREAT, 0o0600)
    with os.fdopen(fd, 'w') as f:
        f.write(json.dumps(config, indent=2))


def get_config_value(ctx, key, prompt, override_value, password=False):
    if override_value is not None:
        value = override_value
    elif ctx.config.get(key) is not None:
        value = ctx.config[key]
    else:
        value = click.prompt(prompt, hide_input=password)
        ctx.user_values_entered = True

    return value


def get_hostname(ctx, override_hostname=None):
    return get_config_value(ctx, 'hostname', "WVA Hostname", override_hostname)


def get_username(ctx, override_username=None):
    return get_config_value(ctx, 'username', "Username", override_username)


def get_password(ctx, override_password=None):
    return get_config_value(ctx, 'password', 'Password', override_password, password=True)


@click.group()
@click.option('--hostname', default=None, help='Force use of the specified hostname')
@click.option('--username', default=None, help='Force use of the specified username')
@click.option('--password', default=None, help='Force use of the specified password')
@click.option("--config-dir", default="~/.wva", help='Directory containing wva configuration files')
@click.pass_context
def cli(ctx, hostname, username, password, config_dir):
    ctx.user_values_entered = False
    ctx.config_dir = os.path.abspath(os.path.expanduser(config_dir))
    ctx.config = load_config(ctx)
    ctx.hostname = get_hostname(ctx, hostname)
    ctx.username = get_username(ctx, username)
    ctx.password = get_password(ctx, password)
    if ctx.user_values_entered:
        if click.confirm("Save new values to config file?"):
            save_config(ctx)

    ctx.wva = WVA(ctx.hostname, ctx.username, ctx.password)
{'fraction_non_alphanumeric': 0.11731123388581952, 'fraction_numerical': 0.005340699815837937, 'mean_word_length': 3.7390924956369984, 'pattern_counts': {'":': 7, '<': 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': '556226', 'n_tokens_mistral': 1725, 'n_tokens_neox': 1688, 'n_words': 476}