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

"""
tarbell.cli
~~~~~~~~~

This module provides the CLI interface to tarbell.
"""

import os
import sys
from subprocess import call
import subprocess
from time import sleep

import clint.resources
from clint import args
from clint.eng import join as eng_join
from clint.textui import colored, puts, columns

from legit.cli import cmd_switch, cmd_branches

import tempfile
import shutil

from .app import TarbellSite

from .oauth import get_drive_api

__version__ = '0.9'

def list_get(l, idx, default=None):
    try:
        if l[idx]:
            return l[idx]
        else:
            return default
    except IndexError:
        return default

def black(s):
    #if settings.allow_black_foreground:
        #return colored.black(s)
    #else:
    return s.encode('utf-8')

class EnsureSite():
    """Context manager to ensure the user is in a Tarbell site environment."""
    def __init__(self, reset=False):
        self.reset = reset

    def __enter__(self):
        return self.ensure_site()

    def __exit__(self, type, value, traceback):
        pass

    def ensure_site(self, path=None):
        if not path:
            path = os.getcwd()

        if path is "/":
            show_error("The current directory is not part of a Tarbell project")
            sys.exit(1)

        if not os.path.exists(os.path.join(path, '.tarbell')):
            path = os.path.realpath(os.path.join(path, '..'))
            return self.ensure_site(path)
        else:
            os.chdir(path)
            self.ensure_secrets(path)
            return path

    def ensure_secrets(self, path):
        # Check for client secrets? Might be unnecessary, really.
        client = get_drive_api(path, self.reset)
        # Trap errors and show docs

# Alias to lowercase
ensure_site = EnsureSite

# --------
# Dispatch
# --------

def main():
    """Primary Tarbell command dispatch."""

    command = Command.lookup(args.get(0))

    if len(args) == 0 or args.contains(('-h', '--help', 'help')):
        display_info()
        sys.exit(1)

    elif args.contains(('-v', '--version')):
        display_version()
        sys.exit(1)

    elif command:
        arg = args.get(0)
        args.remove(arg)

        reset = False
        if args.contains('--reset-creds'):
            reset = True
            args.remove('--reset-creds')

        with ensure_site(reset) as path:
            command.__call__(args, path)

        sys.exit()

    else:
        show_error(colored.red('Error! Unknown command `{0}`.\n'.format(args.get(0))))
        display_info()
        sys.exit(1)


def split_sentences(s):
    sentences = []
    for index, sentence in enumerate(s.split('. ')):
        pad = ''
        if index > 0:
            pad = ' ' * 39
        if sentence.endswith('.'):
            sentence = sentence[:-1]
        sentences.append('%s %s.' % (pad, sentence.strip()))
    return "\n".join(sentences)


def display_info():
    """Displays Tarbell info."""

    puts('{0}\n'.format(
        black('Tarbell: Simple web publishing'),
    ))

    puts('Usage: {0}\n'.format(colored.cyan('tarbell <command>')))
    puts('Commands:\n')
    for command in Command.all_commands():
        usage = command.usage or command.name
        help = command.help or ''
        puts('{0:48} {1}\n'.format(
                colored.green(usage),
                split_sentences(help)))

    puts('\nOptions\n')
    puts('{0:48} {1}'.format(
        colored.green("--reset-creds"),
        'Reset Google Drive OAuth2 credentials'
    ))

    puts('\n{0}'.format(
        black(u'A Chicago Tribune News Applications project')
    ))

def display_version():
    """Displays Legit version/release."""

    puts('{0} v{1}'.format(
        colored.yellow('Tarbell'),
        __version__
    ))


def show_error(msg):
    sys.stdout.flush()
    sys.stderr.write("{0}: {1}".format(colored.red("Error"), msg + '\n'))


def tarbell_generate(args, path):
    """Generate static files."""
    site = TarbellSite(path)
    output_root = list_get(args, 0, False)
    if not output_root:
        tempdir = tempfile.mkdtemp(prefix="{0}-".format(site.project.__name__))
        os.makedirs(os.path.join(tempdir, site.project.__name__))
        output_root = os.path.join(tempdir, site.project.__name__)
    site.generate_static_site(output_root)
    puts("\nCreated site in {0}".format(output_root))
    return tempdir


def tarbell_list(args, path):
    """List tarbell projects."""

    # Use legit branches command for now
    cmd_branches(path)


def tarbell_publish(args, path):
    """Publish a site by calling s3cmd"""
    site = TarbellSite(path)
    bucket_name = list_get(args, 0, "staging")
    bucket_uri = site.project.S3_BUCKETS.get(bucket_name, False)

    try:
        if bucket_uri:
            puts("Deploying to {0} ({1})\n".format(
                colored.green(bucket_name), colored.green(bucket_uri)))
            tempdir = tarbell_generate([], path)
            projectdir = os.path.join(tempdir, site.project.__name__)
            s3cmd = call(['s3cmd', 'sync', '--acl-public', '--delete-removed',
                projectdir, bucket_uri])<commit_msg>better handle output dir argument<commit_after># -*- coding: utf-8 -*-

"""
tarbell.cli
~~~~~~~~~

This module provides the CLI interface to tarbell.
"""

import os
import sys
from subprocess import call
import subprocess
from time import sleep

import clint.resources
from clint import args
from clint.eng import join as eng_join
from clint.textui import colored, puts, columns

from legit.cli import cmd_switch, cmd_branches

import tempfile
import shutil

from .app import TarbellSite

from .oauth import get_drive_api

__version__ = '0.9'

def list_get(l, idx, default=None):
    try:
        if l[idx]:
            return l[idx]
        else:
            return default
    except IndexError:
        return default

def black(s):
    #if settings.allow_black_foreground:
        #return colored.black(s)
    #else:
    return s.encode('utf-8')

class EnsureSite():
    """Context manager to ensure the user is in a Tarbell site environment."""
    def __init__(self, reset=False):
        self.reset = reset

    def __enter__(self):
        return self.ensure_site()

    def __exit__(self, type, value, traceback):
        pass

    def ensure_site(self, path=None):
        if not path:
            path = os.getcwd()

        if path is "/":
            show_error("The current directory is not part of a Tarbell project")
            sys.exit(1)

        if not os.path.exists(os.path.join(path, '.tarbell')):
            path = os.path.realpath(os.path.join(path, '..'))
            return self.ensure_site(path)
        else:
            os.chdir(path)
            self.ensure_secrets(path)
            return path

    def ensure_secrets(self, path):
        # Check for client secrets? Might be unnecessary, really.
        client = get_drive_api(path, self.reset)
        # Trap errors and show docs

# Alias to lowercase
ensure_site = EnsureSite

# --------
# Dispatch
# --------

def main():
    """Primary Tarbell command dispatch."""

    command = Command.lookup(args.get(0))

    if len(args) == 0 or args.contains(('-h', '--help', 'help')):
        display_info()
        sys.exit(1)

    elif args.contains(('-v', '--version')):
        display_version()
        sys.exit(1)

    elif command:
        arg = args.get(0)
        args.remove(arg)

        reset = False
        if args.contains('--reset-creds'):
            reset = True
            args.remove('--reset-creds')

        with ensure_site(reset) as path:
            command.__call__(args, path)

        sys.exit()

    else:
        show_error(colored.red('Error! Unknown command `{0}`.\n'.format(args.get(0))))
        display_info()
        sys.exit(1)


def split_sentences(s):
    sentences = []
    for index, sentence in enumerate(s.split('. ')):
        pad = ''
        if index > 0:
            pad = ' ' * 39
        if sentence.endswith('.'):
            sentence = sentence[:-1]
        sentences.append('%s %s.' % (pad, sentence.strip()))
    return "\n".join(sentences)


def display_info():
    """Displays Tarbell info."""

    puts('{0}\n'.format(
        black('Tarbell: Simple web publishing'),
    ))

    puts('Usage: {0}\n'.format(colored.cyan('tarbell <command>')))
    puts('Commands:\n')
    for command in Command.all_commands():
        usage = command.usage or command.name
        help = command.help or ''
        puts('{0:48} {1}\n'.format(
                colored.green(usage),
                split_sentences(help)))

    puts('\nOptions\n')
    puts('{0:48} {1}'.format(
        colored.green("--reset-creds"),
        'Reset Google Drive OAuth2 credentials'
    ))

    puts('\n{0}'.format(
        black(u'A Chicago Tribune News Applications project')
    ))

def display_version():
    """Displays Legit version/release."""

    puts('{0} v{1}'.format(
        colored.yellow('Tarbell'),
        __version__
    ))


def show_error(msg):
    sys.stdout.flush()
    sys.stderr.write("{0}: {1}".format(colored.red("Error"), msg + '\n'))


def tarbell_generate(args, path):
    """Generate static files."""
    site = TarbellSite(path)
    output_root = list_get(args, 0, False)
    if not output_root:
        tempdir = tempfile.mkdtemp(prefix="{0}-".format(site.project.__name__))
        os.makedirs(os.path.join(tempdir, site.project.__name__))
        output_root = os.path.join(tempdir, site.project.__name__)
    else:
        tempdir = output_root
        output_root = os.path.join(tempdir, site.project.__name__)
    site.generate_static_site(output_root)
    puts("\nCreated site in {0}".format(output_root))
    return tempdir


def tarbell_list(args, path):
    """List tarbell projects."""

    # Use legit branches command for now
    cmd_branches(path)


def tarbell_publish(args, path):
    """Publish a site by calling s3cmd"""
    site = TarbellSite(path)
    bucket_name = list_get(args, 0, "staging")
    bucket_uri = site.project.S3_BUCKETS.get(bucket_name, False)

    try:
        if bucket_uri:
            puts("Deploying to {0} ({1})\n".format(
                colored.green(bucket_name), colored.green(bucket_uri)))
            tempdir = tarbell_generate([], path)
            projectdir = os.path.join(tempdir, site.project.__name__)
            s3cmd = call(['s3cmd', 'sync', '--acl-public', '--delete-removed',
                projectdir, bucket_uri])
{'fraction_non_alphanumeric': 0.14000953743443015, 'fraction_numerical': 0.008202193609918931, 'mean_word_length': 2.4256778830447567, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 7, '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': '56297', 'n_tokens_mistral': 3321, 'n_tokens_neox': 3251, 'n_words': 793}