D37: обновление курсов валют из сети (ЦБ РФ)
- infrastructure/rates/cbr.py — загрузка ежедневных курсов ЦБ РФ (XML, windows-1251) и разбор их в пары X → RUB; разбор вынесен в чистую функцию, поэтому тестируется без сети; - cf config rate-refresh [--dry-run] — обновляет курсы в модели: пары X → RUB обновляются, остальные (например, EUR → USD) не трогаются, неизменившиеся курсы перечисляются отдельно; - нет сети или битый ответ — понятное сообщение, модель не меняется. Тесты: 333 → 342 (tests/test_rates.py: разбор XML, применение курсов).
This commit is contained in:
1 parent
6b13a15408
commit
ebcc78be3a
6 files changed
+296
No files matched your search
@@ -11,6 +11,7 @@ from infrastructure.cli.commands.config._app import app
|
||||
from infrastructure.cli.commands.config._helpers import print_error, print_ok, print_warning
|
||||
from infrastructure.cli.services import console, fail_validation, load_model, save_model
|
||||
from infrastructure.i18n import t
|
||||
from infrastructure.rates import RatesError, apply_rates, fetch_cbr_rates
|
||||
|
||||
|
||||
@app.command()
|
||||
@@ -77,3 +78,32 @@ def rate_remove(
|
||||
else:
|
||||
print_error(t("cmd.config.rate_remove.err", fc=fc, tc=tc))
|
||||
raise typer.Exit(1)
|
||||
|
||||
|
||||
@app.command(name="rate-refresh")
|
||||
def rate_refresh(
|
||||
dry_run: bool = typer.Option(False, "--dry-run", help=t("cmd.config.rate_refresh.opt.dry_run")),
|
||||
) -> None:
|
||||
"""Обновить курсы валют из сети (ЦБ РФ)"""
|
||||
model = load_model()
|
||||
try:
|
||||
snapshot = fetch_cbr_rates()
|
||||
except RatesError as e:
|
||||
print_error(t("cmd.config.rate_refresh.err", error=str(e)))
|
||||
raise typer.Exit(1)
|
||||
|
||||
report = apply_rates(model, snapshot)
|
||||
console.print(t("cmd.config.rate_refresh.date", date=snapshot.date or "—"))
|
||||
for line in report["added"]:
|
||||
console.print(t("cmd.config.rate_refresh.added", pair=line))
|
||||
for line in report["updated"]:
|
||||
console.print(t("cmd.config.rate_refresh.updated", pair=line))
|
||||
if report["unchanged"]:
|
||||
console.print(t("cmd.config.rate_refresh.unchanged", count=len(report["unchanged"])))
|
||||
|
||||
if dry_run:
|
||||
console.print(f"[yellow]{t('cmd.config.rate_refresh.dry_run')}[/yellow]")
|
||||
return
|
||||
|
||||
save_model(model)
|
||||
print_ok(t("cmd.config.rate_refresh.ok", count=len(snapshot.rates)))
|
||||
Reference in new issue
Block a user