| 1 |
============ |
| 2 |
AutoRegistry |
| 3 |
============ |
| 4 |
|
| 5 |
AutoRegistry_ is a python library that automatically creates string-to-functionality mappings, making it trivial to instantiate classes or invoke functions from CLI parameters. |
| 6 |
|
| 7 |
Lets consider the following program that can download a file from either a GCP, AWS, or Azure bucket (without worrying about the implementation): |
| 8 |
|
| 9 |
.. code-block:: python |
| 10 |
|
| 11 |
import cyclopts |
| 12 |
from pathlib import Path |
| 13 |
from typing import Literal |
| 14 |
|
| 15 |
def _download_gcp(bucket: str, key: str, dst: Path): |
| 16 |
print("Downloading data from Google.") |
| 17 |
|
| 18 |
def _download_s3(bucket: str, key: str, dst: Path): |
| 19 |
print("Downloading data from Amazon.") |
| 20 |
|
| 21 |
def _download_azure(bucket: str, key: str, dst: Path): |
| 22 |
print("Downloading data from Azure.") |
| 23 |
|
| 24 |
_downloaders = { |
| 25 |
"gcp": _download_gcp, |
| 26 |
"s3": _download_s3, |
| 27 |
"azure": _download_azure, |
| 28 |
} |
| 29 |
|
| 30 |
app = cyclopts.App() |
| 31 |
|
| 32 |
@app.command |
| 33 |
def download(bucket: str, key: str, dst: Path, provider: Literal[tuple(_downloaders)] = "gcp"): |
| 34 |
downloader = _downloaders[provider] |
| 35 |
downloader(bucket, key, dst) |
| 36 |
|
| 37 |
app() |
| 38 |
|
| 39 |
.. code-block:: console |
| 40 |
|
| 41 |
$ my-script download --help |
| 42 |
╭─ Parameters ────────────────────────────────────────────────────────────╮ |
| 43 |
│ * BUCKET,--bucket [required] │ |
| 44 |
│ * KEY,--key [required] │ |
| 45 |
│ * DST,--dst [required] │ |
| 46 |
│ PROVIDER,--provider [choices: gcp,s3,azure] [default: gcp] │ |
| 47 |
╰─────────────────────────────────────────────────────────────────────────╯ |
| 48 |
|
| 49 |
$ my-script my-bucket my-key local.bin --provider=s3 |
| 50 |
Downloading data from Amazon. |
| 51 |
|
| 52 |
|
| 53 |
Not bad, but let's see how this would look with AutoRegistry. |
| 54 |
|
| 55 |
.. code-block:: python |
| 56 |
|
| 57 |
import cyclopts |
| 58 |
from autoregistry import Registry |
| 59 |
from pathlib import Path |
| 60 |
from typing import Literal |
| 61 |
|
| 62 |
_downloaders = Registry(prefix="_download_") |
| 63 |
|
| 64 |
@_downloaders |
| 65 |
def _download_gcp(bucket: str, key: str, dst: Path): |
| 66 |
print("Downloading data from Google.") |
| 67 |
|
| 68 |
@_downloaders |
| 69 |
def _download_s3(bucket: str, key: str, dst: Path): |
| 70 |
print("Downloading data from Amazon.") |
| 71 |
|
| 72 |
@_downloaders |
| 73 |
def _download_azure(bucket: str, key: str, dst: Path): |
| 74 |
print("Downloading data from Azure.") |
| 75 |
|
| 76 |
app = cyclopts.App() |
| 77 |
|
| 78 |
@app.command |
| 79 |
def download(bucket: str, key: str, dst: Path, provider: Literal[tuple(_downloaders)] = "gcp"): |
| 80 |
downloader = _downloaders[provider] |
| 81 |
downloader(bucket, key, dst) |
| 82 |
|
| 83 |
app() |
| 84 |
|
| 85 |
.. code-block:: console |
| 86 |
|
| 87 |
$ my-script download --help |
| 88 |
╭─ Parameters ────────────────────────────────────────────────────────────╮ |
| 89 |
│ * BUCKET,--bucket [required] │ |
| 90 |
│ * KEY,--key [required] │ |
| 91 |
│ * DST,--dst [required] │ |
| 92 |
│ PROVIDER,--provider [choices: gcp,s3,azure] [default: gcp] │ |
| 93 |
╰─────────────────────────────────────────────────────────────────────────╯ |
| 94 |
|
| 95 |
$ my-script my-bucket my-key local.bin --provider=s3 |
| 96 |
Downloading data from Amazon. |
| 97 |
|
| 98 |
Exactly the same functionality, but more terse and organized. |
| 99 |
With Autoregistry, the download providers are much more self-contained, do not require changes in other code locations, and reduce duplication. |
| 100 |
|
| 101 |
.. _AutoRegistry: https://github.com/BrianPugh/autoregistry |