Было: `ruff check .` → 10 ошибок, CI-гейт на ruff был бы красным сразу. - 9× I001 (порядок импортов) — application/forecast.py:1, application/scenarios.py:1, domain/currency.py:1, infrastructure/ai/assistant.py:1, infrastructure/cli/app.py:11, infrastructure/cli/commands/init.py:2, infrastructure/cli/services.py:2, tests/test_model.py:1, tests/test_repositories.py:1 - 1× E501 — infrastructure/cli/services.py:30, сигнатура build_services перенесена на три строки Поведение не меняется: переупорядочены только импорты, убраны пустые строки, перенесена длинная сигнатура. Порядок импорта в app.py не влияет на момент вызова setup_i18n() — он по-прежнему после импорта команд. Проверка: 75/75 тестов зелёные, ruff check . — All checks passed.
66 lines
2.2 KiB
Python
66 lines
2.2 KiB
Python
"""Composition root + общие хелперы для CLI-команд."""
|
|
from __future__ import annotations
|
|
|
|
from decimal import Decimal
|
|
|
|
from rich.console import Console
|
|
|
|
from application.forecast import ForecastService
|
|
from application.scenarios import ScenarioService
|
|
from domain import CurrencyConverter, FinancialModel
|
|
from infrastructure.ai.assistant import AssistantService
|
|
from infrastructure.cli.paths import MODEL_PATH
|
|
from infrastructure.repositories import JsonFileRepository
|
|
|
|
console = Console()
|
|
|
|
_json_repo = JsonFileRepository()
|
|
|
|
|
|
def load_model() -> FinancialModel:
|
|
if MODEL_PATH.exists():
|
|
return _json_repo.load(MODEL_PATH)
|
|
return FinancialModel()
|
|
|
|
|
|
def save_model(model: FinancialModel) -> None:
|
|
_json_repo.save(model, MODEL_PATH)
|
|
|
|
|
|
def build_services(
|
|
model: FinancialModel,
|
|
) -> tuple[ForecastService, ScenarioService, AssistantService, CurrencyConverter]:
|
|
"""Composition root: собрать граф сервисов с зависимостями."""
|
|
converter = CurrencyConverter(model.exchange_rates)
|
|
forecast_service = ForecastService(model)
|
|
scenario_service = ScenarioService(model)
|
|
assistant_service = AssistantService(
|
|
model, forecast_service=forecast_service, converter=converter
|
|
)
|
|
return forecast_service, scenario_service, assistant_service, converter
|
|
|
|
|
|
def resolve_currency(model: FinancialModel, currency: str | None) -> tuple[str, str]:
|
|
target = currency or model.base_currency
|
|
converter = CurrencyConverter(model.exchange_rates)
|
|
symbol = converter.get_symbol(target)
|
|
return target, symbol
|
|
|
|
|
|
def convert_value(
|
|
converter: CurrencyConverter, amount: Decimal, from_curr: str, to_curr: str
|
|
) -> Decimal:
|
|
if to_curr == from_curr:
|
|
return amount
|
|
return converter.convert(amount, from_curr, to_curr)
|
|
|
|
|
|
def fmt_amount(amount: Decimal | float, currency: str, symbol: str = "") -> str:
|
|
"""Format money via the canonical display rule.
|
|
|
|
`symbol` is retained as a positional parameter for backward compatibility with
|
|
existing callers (compare.py / forecast.py / scenario.py / whatif.py); the
|
|
symbol/layout decision is centralised in `CurrencyConverter.format_money`.
|
|
"""
|
|
return CurrencyConverter.format_money(amount, currency)
|