<commit_before># -*- coding: utf-8 -*-

"""
tarbell.configure
~~~~~~~~~~~~~~~~~

This module provides the Tarbell configure command.
"""

import os
import sys
import yaml
import shutil

from subprocess import call
from datetime import datetime
from clint.textui import colored, puts

from .settings import Settings
from .oauth import get_drive_api
from .utils import list_get, get_config_from_args

try:
    import readline
except ImportError:
    pass

def tarbell_configure(command, args):
    """Tarbell configuration routine"""
    puts("Configuring Tarbell. Press ctrl-c to bail out!")

    # Check if there's settings configured
    path = get_config_from_args(args)
    prompt = True
    if len(args):
        prompt = False

    settings = _get_or_create_config(path)

    if prompt or "drive" in args:
        settings.update(_setup_google_spreadsheets(settings, path, prompt))
    if prompt or "s3" in args:
        settings.update(_setup_s3(settings, path, prompt))
    if prompt or "path" in args:
        settings.update(_setup_tarbell_project_path(settings, path, prompt))
    if prompt or "templates" in args:
        settings.update(_setup_default_templates(settings, path, prompt))

    with open(path, 'w') as f:
        puts("\nWriting {0}".format(colored.green(path)))
        yaml.dump(settings, f, default_flow_style=False)

    if all:
        puts("\n- Done configuring Tarbell. Type `{0}` for help.\n"
             .format(colored.green("tarbell")))

    return Settings(path)


def _get_or_create_config(path, prompt=True):
    """Get or create a Tarbell configuration directory."""
    dirname = os.path.dirname(path)
    filename = os.path.basename(path)

    try:
        os.makedirs(dirname)
    except OSError:
        pass

    if os.path.isfile(path):
        puts("{0} already exists, backing up".format(colored.green(path)))
        _backup(dirname, filename)

    with open(path, 'w+r') as f:
        settings = yaml.load(f)
        if settings and settings.get('s3_buckets') and not settings.get('default_s3_buckets'):
            puts("- Automatically updating default bucket configuration from `s3_buckets` to `default_s3_buckets`")
            settings['default_s3_buckets'] = settings['s3_buckets']
            del settings['s3_buckets']

    return settings or {}


def _setup_google_spreadsheets(settings, path, prompt=True):
    """Set up a Google spreadsheet"""
    if prompt:
        use = raw_input("\nWould you like to use Google spreadsheets [Y/n]? ")
        if use.lower() != "y" and use != "":
            return settings

    dirname = os.path.dirname(path)

    secrets = os.path.join(dirname, 'client_secrets.json')

    write_secrets = True
    if os.path.isfile(secrets):
        write_secrets_input = raw_input("client_secrets.json already exists. Would you like to overwrite it? [y/N] ")
        if not write_secrets_input.lower().startswith('y'):
            write_secrets = False

    if write_secrets:
        puts(("\nLogin in to Google and go to {0} to create an app and generate the "
              "\n{1} authentication file. You should create credentials for an `installed app`. See "
              "\n{2} for more information."
              .format(colored.red("https://code.google.com/apis/console/"),
                      colored.yellow("client_secrets.json"),
                      colored.red("http://tarbell.readthedocs.com/#correctlink")
                     )
            ))

        secrets_path = raw_input(("\nWhere is your client secrets file? "
                                  "[~/Downloads/client_secrets.json] "
                                ))

        if secrets_path == "":
            secrets_path = os.path.join("~", "Downloads/client_secrets.json")

        secrets_path = os.path.expanduser(secrets_path)

        puts("\nCopying {0} to {1}\n"
             .format(colored.green(secrets_path),
                     colored.green(dirname))
        )

        _backup(dirname, "client_secrets.json")
        shutil.copy(secrets_path, os.path.join(dirname, 'client_secrets.json'))

    # Now, try and obtain the API for the first time
    get_api = raw_input("Would you like to authenticate your client_secrets.json? [Y/n] ")
    if get_api == '' or get_api.lower().startswith('y'):
        get_drive_api(dirname, reset_creds=True)

    ret = {}
    default_account = settings.get("google_account", "")
    account = raw_input(("What Google account should have access to new spreadsheets? "
                         "(e.g. <EMAIL>, leave blank to specify for each new "
                         "project) [{0}] ".format(default_account)
                        ))
    if default_account != "" and account == "":
        account = default_account
    if account != "":
        ret = { "google_account" : account }

    return ret
    puts("\n- Done configuring Google spreadsheets.")


def _setup_s3(settings, path, prompt=True):
    """Prompt user to set up Amazon S3"""
    ret = {'default_s3_buckets': {}, 's3_credentials': settings.get('s3_credentials', {})}

    if prompt:<commit_msg>catch shutil errors when copying client_secrets.json, fixes #55<commit_after># -*- coding: utf-8 -*-

"""
tarbell.configure
~~~~~~~~~~~~~~~~~

This module provides the Tarbell configure command.
"""

import os
import sys
import yaml
import shutil

from subprocess import call
from datetime import datetime
from clint.textui import colored, puts

from .settings import Settings
from .oauth import get_drive_api
from .utils import list_get, get_config_from_args, show_error

try:
    import readline
except ImportError:
    pass

def tarbell_configure(command, args):
    """Tarbell configuration routine"""
    puts("Configuring Tarbell. Press ctrl-c to bail out!")

    # Check if there's settings configured
    path = get_config_from_args(args)
    prompt = True
    if len(args):
        prompt = False

    settings = _get_or_create_config(path)

    if prompt or "drive" in args:
        settings.update(_setup_google_spreadsheets(settings, path, prompt))
    if prompt or "s3" in args:
        settings.update(_setup_s3(settings, path, prompt))
    if prompt or "path" in args:
        settings.update(_setup_tarbell_project_path(settings, path, prompt))
    if prompt or "templates" in args:
        settings.update(_setup_default_templates(settings, path, prompt))

    with open(path, 'w') as f:
        puts("\nWriting {0}".format(colored.green(path)))
        yaml.dump(settings, f, default_flow_style=False)

    if all:
        puts("\n- Done configuring Tarbell. Type `{0}` for help.\n"
             .format(colored.green("tarbell")))

    return Settings(path)


def _get_or_create_config(path, prompt=True):
    """Get or create a Tarbell configuration directory."""
    dirname = os.path.dirname(path)
    filename = os.path.basename(path)

    try:
        os.makedirs(dirname)
    except OSError:
        pass

    if os.path.isfile(path):
        puts("{0} already exists, backing up".format(colored.green(path)))
        _backup(dirname, filename)

    with open(path, 'w+r') as f:
        settings = yaml.load(f)
        if settings and settings.get('s3_buckets') and not settings.get('default_s3_buckets'):
            puts("- Automatically updating default bucket configuration from `s3_buckets` to `default_s3_buckets`")
            settings['default_s3_buckets'] = settings['s3_buckets']
            del settings['s3_buckets']

    return settings or {}


def _setup_google_spreadsheets(settings, path, prompt=True):
    """Set up a Google spreadsheet"""
    if prompt:
        use = raw_input("\nWould you like to use Google spreadsheets [Y/n]? ")
        if use.lower() != "y" and use != "":
            return settings

    dirname = os.path.dirname(path)

    secrets = os.path.join(dirname, 'client_secrets.json')

    write_secrets = True
    if os.path.isfile(secrets):
        write_secrets_input = raw_input("client_secrets.json already exists. Would you like to overwrite it? [y/N] ")
        if not write_secrets_input.lower().startswith('y'):
            write_secrets = False

    if write_secrets:
        puts(("\nLogin in to Google and go to {0} to create an app and generate the "
              "\n{1} authentication file. You should create credentials for an `installed app`. See "
              "\n{2} for more information."
              .format(colored.red("https://code.google.com/apis/console/"),
                      colored.yellow("client_secrets.json"),
                      colored.red("http://tarbell.readthedocs.com/#correctlink")
                     )
            ))

        secrets_path = raw_input(("\nWhere is your client secrets file? "
                                  "[~/Downloads/client_secrets.json] "
                                ))

        if secrets_path == "":
            secrets_path = os.path.join("~", "Downloads/client_secrets.json")

        secrets_path = os.path.expanduser(secrets_path)

        puts("\nCopying {0} to {1}\n"
             .format(colored.green(secrets_path),
                     colored.green(dirname))
        )

        _backup(dirname, "client_secrets.json")
        try:
            shutil.copy(secrets_path, os.path.join(dirname, 'client_secrets.json'))
        except shutil.Error, e:
            show_error(str(e))

    # Now, try and obtain the API for the first time
    get_api = raw_input("Would you like to authenticate your client_secrets.json? [Y/n] ")
    if get_api == '' or get_api.lower().startswith('y'):
        get_drive_api(dirname, reset_creds=True)

    ret = {}
    default_account = settings.get("google_account", "")
    account = raw_input(("What Google account should have access to new spreadsheets? "
                         "(e.g. <EMAIL>, leave blank to specify for each new "
                         "project) [{0}] ".format(default_account)
                        ))
    if default_account != "" and account == "":
        account = default_account
    if account != "":
        ret = { "google_account" : account }

    return ret
    puts("\n- Done configuring Google spreadsheets.")


def _setup_s3(settings, path, prompt=True):
    """Prompt user to set up Amazon S3"""
    ret = {'default_s3_buckets': {}, 's3_credentials': settings.get('s3_credentials', {})}

    if prompt:
{'fraction_non_alphanumeric': 0.11781832298136646, 'fraction_numerical': 0.004852484472049689, 'mean_word_length': 2.63107822410148, 'pattern_counts': {'":': 8, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1418056', 'n_tokens_mistral': 3138, 'n_tokens_neox': 3005, 'n_words': 804}