File: validation.py

package info (click to toggle)
python-click 6.6-1~bpo8%2B1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 1,068 kB
  • sloc: python: 6,964; makefile: 124
file content (44 lines) | stat: -rw-r--r-- 1,428 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import click
try:
    from urllib import parser as urlparse
except ImportError:
    import urlparse


def validate_count(ctx, param, value):
    if value < 0 or value % 2 != 0:
        raise click.BadParameter('Should be a positive, even integer.')
    return value


class URL(click.ParamType):
    name = 'url'

    def convert(self, value, param, ctx):
        if not isinstance(value, tuple):
            value = urlparse.urlparse(value)
            if value.scheme not in ('http', 'https'):
                self.fail('invalid URL scheme (%s).  Only HTTP URLs are '
                          'allowed' % value.scheme, param, ctx)
        return value


@click.command()
@click.option('--count', default=2, callback=validate_count,
              help='A positive even number.')
@click.option('--foo', help='A mysterious parameter.')
@click.option('--url', help='A URL', type=URL())
@click.version_option()
def cli(count, foo, url):
    """Validation.

    This example validates parameters in different ways.  It does it
    through callbacks, through a custom type as well as by validating
    manually in the function.
    """
    if foo is not None and foo != 'wat':
        raise click.BadParameter('If a value is provided it needs to be the '
                                 'value "wat".', param_hint=['--foo'])
    click.echo('count: %s' % count)
    click.echo('foo: %s' % foo)
    click.echo('url: %s' % repr(url))