gitscope
https://dev.kronoz.win/r/marzneshin.git
cli/utils.py · v0.3.0-alpha2
Source
1 a47eb4c Dawsh 2y ago import pydoc
2 a47eb4c Dawsh 2y ago from datetime import datetime
3 a47eb4c Dawsh 2y ago from typing import Any, Dict, Iterable, Optional, TypeVar, Union
4 a47eb4c Dawsh 2y ago
5 a47eb4c Dawsh 2y ago import typer
6 a47eb4c Dawsh 2y ago from rich.console import Console
7 a47eb4c Dawsh 2y ago from rich.table import Table
8 a47eb4c Dawsh 2y ago
9 a47eb4c Dawsh 2y ago from app.db import crud
10 a47eb4c Dawsh 2y ago from app.db.models import User
11 a47eb4c Dawsh 2y ago
12 a47eb4c Dawsh 2y ago T = TypeVar("T")
13 a47eb4c Dawsh 2y ago
14 a47eb4c Dawsh 2y ago rich_console = Console()
15 a47eb4c Dawsh 2y ago PASSWORD_ENVIRON_NAME = "MARZBAN_ADMIN_PASSWORD"
16 a47eb4c Dawsh 2y ago
17 a47eb4c Dawsh 2y ago FLAGS: Dict[str, tuple] = {
18 a47eb4c Dawsh 2y ago "username": ("--username", "-u"),
19 a47eb4c Dawsh 2y ago "limit": ("--limit", "-l"),
20 a47eb4c Dawsh 2y ago "offset": ("--offset", "-o"),
21 a47eb4c Dawsh 2y ago "yes_to_all": ("--yes", "-y"),
22 a47eb4c Dawsh 2y ago "is_sudo": ("--sudo/--no-sudo",),
23 a47eb4c Dawsh 2y ago "format": ("--format", "-f"),
24 a47eb4c Dawsh 2y ago "output_file": ("--output", "-o"),
25 a47eb4c Dawsh 2y ago "status": ("--status",),
26 a47eb4c Dawsh 2y ago }
27 a47eb4c Dawsh 2y ago
28 a47eb4c Dawsh 2y ago
29 a47eb4c Dawsh 2y ago def success(text: str, auto_exit: bool = True):
30 a47eb4c Dawsh 2y ago typer.echo(typer.style(text, fg=typer.colors.GREEN))
31 a47eb4c Dawsh 2y ago if auto_exit:
32 a47eb4c Dawsh 2y ago raise typer.Exit(0)
33 a47eb4c Dawsh 2y ago
34 a47eb4c Dawsh 2y ago
35 a47eb4c Dawsh 2y ago def error(text: str, auto_exit: bool = True):
36 a47eb4c Dawsh 2y ago typer.echo(typer.style(text, fg=typer.colors.RED), err=True)
37 a47eb4c Dawsh 2y ago if auto_exit:
38 a47eb4c Dawsh 2y ago raise typer.Exit(-1)
39 a47eb4c Dawsh 2y ago
40 a47eb4c Dawsh 2y ago
41 a47eb4c Dawsh 2y ago def paginate(text: str):
42 a47eb4c Dawsh 2y ago pydoc.pager(text)
43 a47eb4c Dawsh 2y ago
44 a47eb4c Dawsh 2y ago
45 a47eb4c Dawsh 2y ago def get_user(db, username: str) -> User:
46 a47eb4c Dawsh 2y ago user: Union[User, None] = crud.get_user(db=db, username=username)
47 a47eb4c Dawsh 2y ago if not user:
48 a47eb4c Dawsh 2y ago error(f'User "{username}" not found!')
49 a47eb4c Dawsh 2y ago
50 a47eb4c Dawsh 2y ago return user
51 a47eb4c Dawsh 2y ago
52 a47eb4c Dawsh 2y ago
53 a47eb4c Dawsh 2y ago def print_table(
54 01cdf2e Dawsh 2y ago table: Table, rows: Iterable[Iterable[Any]], console: Optional[Console] = None
55 a47eb4c Dawsh 2y ago ):
56 a47eb4c Dawsh 2y ago for row in rows:
57 a47eb4c Dawsh 2y ago table.add_row(*row)
58 a47eb4c Dawsh 2y ago
59 a47eb4c Dawsh 2y ago (console or rich_console).print(table)
60 a47eb4c Dawsh 2y ago
61 a47eb4c Dawsh 2y ago
62 a47eb4c Dawsh 2y ago def readable_datetime(
63 a47eb4c Dawsh 2y ago date_time: Union[datetime, int, None],
64 a47eb4c Dawsh 2y ago include_date: bool = True,
65 01cdf2e Dawsh 2y ago include_time: bool = True,
66 a47eb4c Dawsh 2y ago ):
67 a47eb4c Dawsh 2y ago def get_datetime_format():
68 a47eb4c Dawsh 2y ago dt_format = ""
69 a47eb4c Dawsh 2y ago if include_date:
70 a47eb4c Dawsh 2y ago dt_format += "%d %B %Y"
71 a47eb4c Dawsh 2y ago if include_time:
72 a47eb4c Dawsh 2y ago if dt_format:
73 a47eb4c Dawsh 2y ago dt_format += ", "
74 a47eb4c Dawsh 2y ago dt_format += "%H:%M:%S"
75 a47eb4c Dawsh 2y ago
76 a47eb4c Dawsh 2y ago return dt_format
77 a47eb4c Dawsh 2y ago
78 a47eb4c Dawsh 2y ago if isinstance(date_time, int):
79 a47eb4c Dawsh 2y ago date_time = datetime.fromtimestamp(date_time)
80 a47eb4c Dawsh 2y ago
81 a47eb4c Dawsh 2y ago return date_time.strftime(get_datetime_format()) if date_time else "-"
82 a47eb4c Dawsh 2y ago
83 a47eb4c Dawsh 2y ago
84 a47eb4c Dawsh 2y ago def raise_if_falsy(obj: T, message: str) -> T:
85 a47eb4c Dawsh 2y ago if not obj:
86 a47eb4c Dawsh 2y ago error(text=message, auto_exit=True)
87 a47eb4c Dawsh 2y ago
88 a47eb4c Dawsh 2y ago return obj