class AssetType(_LenientChoice):
    name = 'asset-type'

    def __init__(self):
        _LenientChoice.__init__(self, DEFAULT_ASSET_TYPES)

    def get_remote_choices(self):
        return get_asset_types() class AssetTypePerm(AssetType):

    @staticmethod
    def to_permissions(asset_types):
        return filters.permission_filter(*[
            'assets.%s:download' % a for a in asset_types
        ])

    def convert(self, val, param, ctx):
        return AssetTypePerm.to_permissions(
            AssetType.convert(self, val, param, ctx)) class DateRange(Range):
    @property
    def _builder(self):
        return filters.date_range

    def _parse(self, val, param, ctx):
        parsed = strp_lenient(val)
        if parsed is None:
            self.fail('invalid date: %s.' % val, param, ctx)
        return parsed class GeomFilter(click.ParamType):
    name = 'geom'

    def convert(self, val, param, ctx):
        val = read(val)
        if not val:
            return []
        try:
            geoj = json.loads(val)
        except ValueError:
            raise click.BadParameter('invalid GeoJSON')
        geom = geometry_from_json(geoj)
        if geom is None:
            raise click.BadParameter('unable to find geometry in input')
        return [filters.geom_filter(geom)] class FilterJSON(click.ParamType):
    name = 'filter'

    def convert(self, val, param, ctx):
        val = read(val)
        if not val:
            return []
        try:
            filt = json.loads(val)
        except ValueError:
            raise click.BadParameter('invalid JSON')
        if not filters.is_filter_like(filt):
            self.fail('Does not appear to be valid filter', param, ctx)
        return filt class ItemType(_LenientChoice):
    name = 'item-type'
    allow_all = True
    allow_prefix = False

    def __init__(self):
        _LenientChoice.__init__(self, DEFAULT_ITEM_TYPES)

    def get_remote_choices(self):
        return get_item_types() class NumberIn(_FilterFieldValues):
    val_type = float

    @property
    def _builder(self):
        return filters.num_filter class Range(CompositeParamType):
    arity = 3
    name = 'field comp value'
    comp_ops = ['lt', 'lte', 'gt', 'gte']

    @property
    def _builder(self):
        return filters.range_filter

    def _parse(self, val, param, ctx):
        try:
            return float(val)
        except ValueError:
            self.fail('invalid value for range: "%s", must be number' % val)

    def convert(self, vals, param, ctx):
        field, comp_op, val = vals
        if comp_op not in self.comp_ops:
            self.fail('invalid operator: %s. allowed: %s' % (
                comp_op, ','.join(self.comp_ops)), param, ctx)
        args = dict([(comp_op, self._parse(val, param, ctx))])
        return self._builder(field, **args) class SortSpec(CompositeParamType):
    name = 'field order'
    arity = 2

    def convert(self, val, param, ctx):
        if not val:
            return ''
        field, order = val
        fields = ('published', 'acquired')
        if field not in fields:
            raise click.BadParameter(
                'sort only supports: %s' % ' '.join(fields))
        orders = ('asc', 'desc')
        if order not in orders:
            raise click.BadParameter(
                'order only supports: %s' % ' '.join(orders))
        return ' '.join(val) class StringIn(_FilterFieldValues):
    val_type = str

    @property
    def _builder(self):
        return filters.string_filter class Bundle(_LenientChoice):
    name = 'bundle'
    allow_all = True

    def __init__(self):
        _LenientChoice.__init__(self, DEFAULT_BUNDLES)

    def get_remote_choices(self):
        return get_bundles()
import click
from .types import (
    AssetType,
    AssetTypePerm,
    DateRange,
    GeomFilter,
    FilterJSON,
    ItemType,
    NumberIn,
    Range,
    SortSpec,
    StringIn,
    Bundle
)
# Copyright 2017 Planet Labs, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.




pretty = click.option('-pp/-r', '--pretty/--no-pretty', default=None,
                      is_flag=True, help='Format JSON output')

dest_dir = click.option('-d', '--dest', help='Destination directory',
                        type=click.Path(file_okay=False, resolve_path=True,
                                        exists=True))

num_type = type('number', (click.types.IntParamType,), {'name': 'number'})()


def limit_option(default):
    limit_msg = "Limit the number of items. Default: {}".format(default)
    return click.option('--limit', default=default, required=False,
                        type=num_type, help=limit_msg)


geom_filter = click.option('--geom', type=GeomFilter(), help=(
    'Specify a geometry filter as geojson.'
))

date_range_filter = click.option(
    '--date', nargs=3, multiple=True, type=DateRange(),
    help=(
        'Filter field by date.'
    )
)

range_filter = click.option(
    '--range', multiple=True, type=Range(),
    help=(
        'Filter field by numeric range.'
    )
)

number_in_filter = click.option(
    '--number-in', multiple=True, type=NumberIn(),
    help=(
        'Filter field by numeric in.'
    )
)

string_in_filter = click.option(
    '--string-in', multiple=True, type=StringIn(),
    help=(
        'Filter field by string in.'
    )
)

item_type_option = click.option(
    '--item-type', multiple=True, required=True, type=ItemType(), help=(
       'Specify item type(s)'
    )
)

bundle_option = click.option(
    '--bundle', multiple=False, required=True, type=Bundle(), help=(
       'Specify bundle'
    )
)

asset_type_option = click.option(

    '--asset-type', multiple=True, required=True, type=AssetType(), help=(