diff options
| author | Murilo Ijanc <[email protected]> | 2026-07-26 11:44:58 -0300 |
|---|---|---|
| committer | Murilo Ijanc <[email protected]> | 2026-07-26 11:44:58 -0300 |
| commit | a82459630e957a8d94c375394dae66734f3c90c1 (patch) | |
| tree | d9d1b9e1249ccd8cd1d989f532c6368a70dc9cc2 /src/cepis/__main__.py | |
| download | python-a82459630e957a8d94c375394dae66734f3c90c1.tar.gz | |
Initial importv0.1.0
Diffstat (limited to 'src/cepis/__main__.py')
| -rw-r--r-- | src/cepis/__main__.py | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/src/cepis/__main__.py b/src/cepis/__main__.py new file mode 100644 index 0000000..9478a75 --- /dev/null +++ b/src/cepis/__main__.py @@ -0,0 +1,61 @@ +"""Minimal CLI: `python -m cepis 01001000`. + +It exists for one concrete case — quickly confirming a local instance answers +before pointing code at it:: + + python -m cepis --base-url http://localhost:8080 01001000 + +A single CEP goes through the unit lookup, which carries `geo`; several go +through the batch, which is one request instead of N and has no `geo`. + +It is not the library's main interface; for that, import :mod:`cepis`. +""" + +from __future__ import annotations + +import argparse +import dataclasses +import json +import sys + +from . import DEFAULT_URL, CepisError, Client + + +def main(argv: list[str] | None = None) -> int: + """Run the CLI. Returns the process exit code.""" + parser = argparse.ArgumentParser( + prog="python -m cepis", + description="Look CEPs up against the cep.is API.", + ) + parser.add_argument("cep", nargs="+", help="one or more CEPs, masked or not") + parser.add_argument( + "--base-url", + default=DEFAULT_URL, + help=f"API URL (default: {DEFAULT_URL}) — use it to point at a local instance", + ) + parser.add_argument( + "--timeout", type=float, default=10.0, help="per-socket timeout, in seconds" + ) + args = parser.parse_args(argv) + + client = Client(base_url=args.base_url, timeout=args.timeout) + + try: + if len(args.cep) == 1: + addresses = [client.lookup(args.cep[0])] + else: + addresses = client.lookup_many(args.cep) + except CepisError as exc: + print(f"error: {exc}", file=sys.stderr) + return 1 + + output = [None if a is None else dataclasses.asdict(a) for a in addresses] + for item in output: + if item is not None: + item.pop("raw", None) + print(json.dumps(output if len(output) > 1 else output[0], ensure_ascii=False, indent=2)) + return 0 + + +if __name__ == "__main__": + raise SystemExit(main()) |