<commit_before>#!/usr/bin/env python
# -*- encoding: utf-8 -*-

"""Main CLI entry point, using Click as argument parser."""

__author__ = 'kotaimen'
__date__ = '2/3/15'

import sys
import os
import click

CONTEXT_SETTINGS = dict(
    auto_envvar_prefix='STONEMASON',
    help_option_names=['-h', '--help'],
)


class Context(object):
    """Custom context object, passing options collected from group command.
    """

    def __init__(self):
        self.themes = None
        self.verbosity = 0
        self.debug = False


pass_context = click.make_pass_decorator(Context)

#
# Main entry
#
import stonemason


@click.group(context_settings=CONTEXT_SETTINGS)
@click.option('--debug/--no-debug', default=False, help='debug mode.')
@click.option('-v', '--verbose', default=False, count=True,
              help='being verbose.')
@click.option('--themes', type=click.Path(exists=False), required=True,
              help='themes root directory, can be overridden using envvar '
                   'STONEMASON_THEMES.')
@click.version_option(stonemason.__version__, message='Stonemason %(version)s')
@click.pass_context
def cli(ctx, debug, verbose, themes):
    """Stonemason Tile Map Service Toolkit."""
    ctx.obj = Context()
    ctx.obj.themes = os.path.abspath(themes)
    ctx.obj.verbosity = verbose
    ctx.obj.debug = debug

#
# Tile Server Command
#
from stonemason.service.tileserver import TileServerApp


@cli.command('tileserver', short_help='frontend tile server.')
@click.option('-b', '--bind', default='127.0.0.1:7086', type=str,
              help='address and port to bind to')
@click.option('--read-only', is_flag=True,
              help='start the server in read only mode', )
@pass_context
def tile_server(ctx, bind, read_only):
    """Starts tile server using given themes root."""
    assert isinstance(ctx, Context)

    if read_only:
        click.secho('Starting tileserver in read only mode.', fg='red')

    host, port = bind.split(':')
    port = int(port)

    app = TileServerApp(themes=ctx.themes,
                        read_only=read_only,
                        debug=True)
    app.run(host=host, port=port,
            debug=ctx.debug, verbose=ctx.verbosity,
            read_only=read_only)


#
# Theme Root Init Command
#
@cli.command('init', short_help='init themes root.')
@pass_context
def tile_server(ctx, sample):
    click.secho('Initializing themes root at %s...' % ctx.themes)
    # TODO: Init theme root


if __name__ == '__main__':
    cli()
<commit_msg>FIX: Pass dict into TileServerApp instead of args.<commit_after>#!/usr/bin/env python
# -*- encoding: utf-8 -*-

"""Main CLI entry point, using Click as argument parser."""

__author__ = 'kotaimen'
__date__ = '2/3/15'

import sys
import os
import click
import six

# NOTE: click will add "_" between prefix and variable, but flask don't...
ENVVAR_PREFIX = 'STONEMASON'

CONTEXT_SETTINGS = dict(
    auto_envvar_prefix=ENVVAR_PREFIX,
    help_option_names=['-h', '--help'],
)


class Context(object):
    """Custom context object, passing options collected from group command.
    """

    def __init__(self):
        self.themes = None
        self.verbosity = 0
        self.debug = False


pass_context = click.make_pass_decorator(Context)

#
# Main entry
#
import stonemason


@click.group(context_settings=CONTEXT_SETTINGS)
@click.option('--debug/--no-debug', default=False, help='debug mode.')
@click.option('-v', '--verbose', default=False, count=True,
              help='being verbose.')
@click.option('--themes', type=click.Path(exists=False), required=True,
              help='themes root directory, can be overridden using envvar '
                   'STONEMASON_THEMES.')
@click.version_option(stonemason.__version__, message='Stonemason %(version)s')
@click.pass_context
def cli(ctx, debug, verbose, themes):
    """Stonemason Tile Map Service Toolkit."""
    ctx.obj = Context()
    ctx.obj.themes = os.path.abspath(themes)
    ctx.obj.verbosity = verbose
    ctx.obj.debug = debug

#
# Tile Server Command
#
from stonemason.service.tileserver import TileServerApp


@cli.command('tileserver', short_help='frontend tile server.')
@click.option('-b', '--bind', default='127.0.0.1:7086', type=str,
              help='address and port to bind to')
@click.option('--read-only', is_flag=True,
              help='start the server in read only mode', )
@pass_context
def tile_server(ctx, bind, read_only):
    """Starts tile server using given themes root."""
    assert isinstance(ctx, Context)

    if read_only and ctx.verbosity > 0:
        click.secho('Starting tileserver in read only mode.', fg='red')

    # parse binding port
    host, port = bind.split(':')
    port = int(port)

    config = {
        'THEMES': ctx.themes,
        'READ_ONLY': read_only,
        'DEBUG': ctx.debug,
        'VERBOSITY': ctx.verbosity
    }
    # add prefix to all config keys to confront with flask config
    config = dict((ENVVAR_PREFIX + '_' + k, v)
                  for (k, v) in six.iteritems(config))

    app = TileServerApp(**config)
    app.run(host=host, port=port,
            debug=ctx.debug)


#
# Theme Root Init Command
#
@cli.command('init', short_help='init themes root.')
@pass_context
def tile_server(ctx, sample):
    click.secho('Initializing themes root at %s...' % ctx.themes)
    # TODO: Init theme root


if __name__ == '__main__':
    cli()

{'fraction_non_alphanumeric': 0.1383413908516177, 'fraction_numerical': 0.0061361100780959466, 'mean_word_length': 3.269047619047619, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '477431', 'n_tokens_mistral': 1785, 'n_tokens_neox': 1713, 'n_words': 437}