Files
nifodea/infrastructure/cli/commands/config/assets.py
T

59 lines
2.2 KiB
Python

"""`cf config asset-*` — активы: добавление, список, удаление."""
import typer
from rich.table import Table
from domain import Asset, CurrencyConverter
from infrastructure.cli.commands.config._app import app
from infrastructure.cli.commands.config._helpers import print_ok, print_warning, remove_items
from infrastructure.cli.services import console, load_model, save_model
from infrastructure.i18n import t
@app.command()
def asset_add(
name: str = typer.Option(..., "--name", "-n", help=t("cmd.config.asset_add.opt.name")),
value: float = typer.Option(0.0, "--value", "-v", help=t("cmd.config.asset_add.opt.value")),
growth_rate: float = typer.Option(0.0, "--growth", "-g", help=t("cmd.config.asset_add.opt.growth")),
) -> None:
"""Добавить актив"""
model = load_model()
asset = Asset(name=name, value=value, growth_rate=growth_rate)
model.assets.append(asset)
save_model(model)
print_ok(t("cmd.config.asset_add.ok", name=name, value=value))
@app.command(name="asset-list")
def asset_list() -> None:
"""Список активов"""
model = load_model()
if not model.assets:
print_warning(t("cmd.config.asset_list.empty"))
return
table = Table(title=t("table.assets.title"))
table.add_column(t("table.assets.col.id"), style="dim")
table.add_column(t("table.assets.col.name"), style="cyan")
table.add_column(t("table.assets.col.value"), justify="right")
table.add_column(t("table.assets.col.growth"), justify="right")
for a in model.assets:
value_str = CurrencyConverter.format_money(a.value, model.base_currency)
table.add_row(str(a.id)[:8], a.name, value_str, f"{a.growth_rate:.1f}%")
console.print(table)
@app.command(name="asset-remove")
def asset_remove(
identifier: str = typer.Argument(..., help=t("cmd.config.asset_remove.arg")),
) -> None:
"""Удалить актив по ID или названию"""
model = load_model()
remove_items(
model,
"assets",
lambda a: str(a.id) == identifier or a.name == identifier,
identifier=identifier,
ok_key="cmd.config.asset_remove.ok",
err_key="cmd.config.asset_remove.err",
)