<commit_before>import click
import json
from planet import api

client = api.Client()

pretty = click.option('-pp', '--pretty', default=False, is_flag=True)
scene_type = click.option('-s', '--scene-type', default='ortho')


def call_and_wrap(func, *args, **kw):
    '''call the provided function and wrap any API exception with a click
    exception. this means no stack trace is visible to the user but instead
    a (hopefully) nice message is provided.
    note: could be a decorator but didn't play well with click
    '''
    try:
        return func(*args, **kw)
    except api.APIException, ex:
        if type(ex) is api.APIException:
            raise click.ClickException('Unexpected response: %s' % ex.message)
        msg = "%s: %s" % (type(ex).__name__, ex.message)
        raise click.ClickException(msg)


@click.group()
@click.option('-k', '--api-key',
              help='Valid API key - or via env variable %s' % api.ENV_KEY)
@click.option('-u', '--base-url', help='Optional for testing')
def cli(api_key, base_url):
    '''Planet API Client'''
    if api_key:
        client.api_key = api_key
    if base_url:
        client.base_url = base_url


@cli.command()
def list_all_scene_types():
    '''List all scene types.'''
    click.echo(call_and_wrap(client.list_all_scene_types))


@scene_type
@click.argument('scene_ids', nargs=-1)
@click.option('--product', type=click.Choice(['visual', 'analytic']), default='visual')
@cli.command('download')
@click.pass_context
def fetch_scene_geotiff(ctx, scene_ids, scene_type, product):
    '''Fetch full scene image(s)'''
    
    if len(scene_ids) == 0:
        src = click.open_file('-')
        if not src.isatty():
            scene_ids = map(lambda s: s.strip(), src.readlines())
        else:
            click.echo(ctx.get_usage())
    
    call_and_wrap(client.fetch_scene_geotiffs, scene_ids, scene_type, product)


@scene_type
@click.argument("scene-ids", nargs=-1)
@click.option('--size', type=click.Choice(['sm', 'md', 'lg']), default='md')
@click.option('--format', 'fmt', type=click.Choice(['png', 'jpg', 'jpeg']), default='png')
@cli.command('thumbnails')
def fetch_scene_thumbnails(scene_ids, scene_type, size, fmt):
    '''Fetch scene thumbnail(s)'''
    
    if len(scene_ids) == 0:
        src = click.open_file('-')
        if not src.isatty():
            scene_ids = map(lambda s: s.strip(), src.readlines())
    
    call_and_wrap(client.fetch_scene_thumbnails, scene_ids, scene_type, size, fmt)


@pretty
@scene_type
@click.argument('id', nargs=1)
@cli.command()
def fetch_scene_info(id, scene_type, pretty):<commit_msg>allow landsat product types for cli<commit_after>import click
import json
from planet import api

client = api.Client()

pretty = click.option('-pp', '--pretty', default=False, is_flag=True)
scene_type = click.option('-s', '--scene-type', default='ortho')


def call_and_wrap(func, *args, **kw):
    '''call the provided function and wrap any API exception with a click
    exception. this means no stack trace is visible to the user but instead
    a (hopefully) nice message is provided.
    note: could be a decorator but didn't play well with click
    '''
    try:
        return func(*args, **kw)
    except api.APIException, ex:
        if type(ex) is api.APIException:
            raise click.ClickException('Unexpected response: %s' % ex.message)
        msg = "%s: %s" % (type(ex).__name__, ex.message)
        raise click.ClickException(msg)


@click.group()
@click.option('-k', '--api-key',
              help='Valid API key - or via env variable %s' % api.ENV_KEY)
@click.option('-u', '--base-url', help='Optional for testing')
def cli(api_key, base_url):
    '''Planet API Client'''
    if api_key:
        client.api_key = api_key
    if base_url:
        client.base_url = base_url


@cli.command()
def list_all_scene_types():
    '''List all scene types.'''
    click.echo(call_and_wrap(client.list_all_scene_types))


@scene_type
@click.argument('scene_ids', nargs=-1)
@click.option('--product', type=click.Choice([ "band_%d" % i for i in range(1, 12) ] + ['visual', 'analytic', 'qa']), default='visual')
@cli.command('download')
@click.pass_context
def fetch_scene_geotiff(ctx, scene_ids, scene_type, product):
    '''Fetch full scene image(s)'''
    
    if len(scene_ids) == 0:
        src = click.open_file('-')
        if not src.isatty():
            scene_ids = map(lambda s: s.strip(), src.readlines())
        else:
            click.echo(ctx.get_usage())
    
    call_and_wrap(client.fetch_scene_geotiffs, scene_ids, scene_type, product)


@scene_type
@click.argument("scene-ids", nargs=-1)
@click.option('--size', type=click.Choice(['sm', 'md', 'lg']), default='md')
@click.option('--format', 'fmt', type=click.Choice(['png', 'jpg', 'jpeg']), default='png')
@cli.command('thumbnails')
def fetch_scene_thumbnails(scene_ids, scene_type, size, fmt):
    '''Fetch scene thumbnail(s)'''
    
    if len(scene_ids) == 0:
        src = click.open_file('-')
        if not src.isatty():
            scene_ids = map(lambda s: s.strip(), src.readlines())
    
    call_and_wrap(client.fetch_scene_thumbnails, scene_ids, scene_type, size, fmt)


@pretty
@scene_type
@click.argument('id', nargs=1)
@cli.command('metadata')
def fetch_scene_info(id, scene_type, pretty):
{'fraction_non_alphanumeric': 0.15967498110355252, 'fraction_numerical': 0.0024565381708238853, 'mean_word_length': 3.6511423550087874, '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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1176554', 'n_tokens_mistral': 1797, 'n_tokens_neox': 1758, 'n_words': 441}