Skip to main content

Integrating extra metadata into attr.ib()

Project description

PyPI version supported Python versions Travis build status source on GitHub

Integrating extra metadata into attr.ib()

Example

With a little luck a better example will be provided later but for now, here’s something.

A dev in #python was interested in having click build them attrs-defined configuration objects. Here’s a basic solution for that with the click options being defined on the attrs class attributes via a custom exttr keyword argument click=.

import collections
import sys

import attr
import click
import exttr


exttr.register_keywords(
    exttr.Keyword(name='click'),
)


@attr.s
class Configuration:
    foo = exttr.ib(click=click.option('--red'))


def main(configuration):
    print(configuration)

def clicked_fields(cls):
    fields = collections.OrderedDict()

    for field in attr.fields(cls):
        decorator = exttr.get(cls, field.name, 'click')

        if decorator is None:
            continue

        fields[field.name] = decorator

    return fields


def build_click(f, cls, command_or_group):
    fields = clicked_fields(cls)

    def cli(*args, **kwargs):
        configuration = cls(*args, **kwargs)

        return f(configuration)

    for name, decorator in reversed(fields.items()):
        before = getattr(cli, '__click_params__', [])

        cli = decorator(cli)

        after = getattr(cli, '__click_params__', [])
        new = after[len(before):]

        if len(new) == 1:
            new, = new
            new.name = name

    return command_or_group(cli)


click_main = build_click(
    f=main,
    cls=Configuration,
    command_or_group=click.command(),
)


sys.argv[1:] = ['--red', 'burgundy']
try:
    click_main()
except SystemExit:
    pass

Output:

Configuration(foo='burgundy')

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

exttr-2019.2.1.tar.gz (20.4 kB view details)

Uploaded Source

Built Distribution

exttr-2019.2.1-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

Details for the file exttr-2019.2.1.tar.gz.

File metadata

  • Download URL: exttr-2019.2.1.tar.gz
  • Upload date:
  • Size: 20.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

File hashes

Hashes for exttr-2019.2.1.tar.gz
Algorithm Hash digest
SHA256 8a367dbef2d26fd101f41a4b75553b8629e7b3724595608f5d4324e94e89ec4f
MD5 8752446fbce9f406936f73da9df61445
BLAKE2b-256 3ea2a44aca8405d57f21df687d1bfaa1ceb9a3e48a0e925fd662b56d5796bd28

See more details on using hashes here.

File details

Details for the file exttr-2019.2.1-py3-none-any.whl.

File metadata

  • Download URL: exttr-2019.2.1-py3-none-any.whl
  • Upload date:
  • Size: 6.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

File hashes

Hashes for exttr-2019.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 50deb5f04a9cb268c02179a35e54d0e85997ec8584b5578f8b7e20d12b6eca14
MD5 bea3750584766d1e1dbf81fca83a375b
BLAKE2b-256 aef798ad5ecc0638ca5d741ebcc05b50da73f2076cc6d6d11e152a17d449b3d9

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page