Files
nifodea/infrastructure/cli/commands/import_xlsx.py
T
oqyude cdda703b37 T9: Excel as the model authoring surface
- ExcelRepository: canonical columns with ru/en aliases, date/number normalization
  (Excel dates, DD.MM.YYYY, '1 234,56', currency symbols, parentheses), account
  name->UUID resolution, per-row errors with sheet!row, warnings for formulas
  without cached values, duplicate names and default exchange rates
- ExcelTemplate: skeleton generator (number/date formats, dropdowns for currency,
  frequency en/ru and account, 'Инструкция' sheet); ship data/model_template.xlsx
- CLI: cf template-xlsx [--force]; cf import-xlsx --dry-run with rich error table,
  import writes nothing when the file has errors
- RecurringCashflow.account (empty = all accounts) + name/UUID matching in forecast;
  cf config recurring-add --account, account column in info/recurring-list
- Forecast: runs are idempotent, 'Итог: Баланс' sums all accounts of the last month
- README: 'Сборка модели в Excel' section
- ruff check . clean (9 pre-existing import/line-length findings fixed)
- MetaAgent: T9 archived, req-T9 approved, manifest/checkpoints/state synced

118 tests pass
2026-11-11 15:40:37 +03:00

76 lines
2.6 KiB
Python

"""cf import-xlsx — импорт данных из Excel."""
import typer
from rich.markup import escape
from rich.table import Table
from infrastructure.cli.i18n import t
from infrastructure.cli.services import console, save_model
from infrastructure.repositories import ExcelRepository, ExcelRepositoryError, ExcelRowError
app = typer.Typer()
_repo = ExcelRepository()
def _counts(model) -> dict[str, int]:
return {
"accounts": len(model.accounts),
"transactions": len(model.transactions),
"recurring": len(model.recurring),
"assets": len(model.assets),
"liabilities": len(model.liabilities),
}
def _print_warnings(warnings: list[str]) -> None:
for warning in warnings:
console.print(f"[yellow]{t('cmd.import.warn', warning=escape(warning))}[/yellow]")
def _print_errors(errors: list[ExcelRowError]) -> None:
table = Table(title=t("cmd.import.errors.title", count=len(errors)))
table.add_column(t("cmd.import.errors.col.sheet"))
table.add_column(t("cmd.import.errors.col.row"), justify="right")
table.add_column(t("cmd.import.errors.col.field"))
table.add_column(t("cmd.import.errors.col.message"))
for error in errors:
table.add_row(
escape(error.sheet),
str(error.row),
escape(error.field) or "—",
escape(error.message),
)
console.print(table)
console.print(f"[red]{t('cmd.import.errors.hint', first=escape(str(errors[0])))}[/red]")
@app.command(name="import-xlsx")
def import_xlsx(
path: str = typer.Argument(..., help=t("cmd.import.arg.path")),
dry_run: bool = typer.Option(False, "--dry-run", help=t("cmd.import.opt.dry_run")),
) -> None:
"""Импорт данных из Excel"""
try:
report = _repo.load_report(path)
except ExcelRepositoryError as e:
console.print(f"[red]{t('cmd.import.err', error=escape(str(e)))}[/red]")
raise typer.Exit(1)
_print_warnings(report.warnings)
if report.errors:
_print_errors(report.errors)
raise typer.Exit(1)
counts = _counts(report.model)
if dry_run:
console.print(t("cmd.import.dry_run.title"))
console.print(t("cmd.import.dry_run.ok", **counts))
console.print(t("cmd.import.dry_run.base_currency", currency=report.model.base_currency))
console.print(f"[yellow]{t('cmd.import.dry_run.not_saved')}[/yellow]")
return
save_model(report.model)
console.print(t("cmd.import.ok", **counts))
console.print(t("cmd.import.base_currency", currency=report.model.base_currency))