gitscope
https://dev.kronoz.win/r/marzneshin.git
cli/utils.py · v0.6.1
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 f4fecee Dawsh 2y ago table: Table,
55 f4fecee Dawsh 2y ago rows: Iterable[Iterable[Any]],
56 f4fecee Dawsh 2y ago console: Optional[Console] = None,
57 a47eb4c Dawsh 2y ago ):
58 a47eb4c Dawsh 2y ago for row in rows:
59 a47eb4c Dawsh 2y ago table.add_row(*row)
60 a47eb4c Dawsh 2y ago
61 a47eb4c Dawsh 2y ago (console or rich_console).print(table)
62 a47eb4c Dawsh 2y ago
63 a47eb4c Dawsh 2y ago
64 a47eb4c Dawsh 2y ago def readable_datetime(
65 a47eb4c Dawsh 2y ago date_time: Union[datetime, int, None],
66 a47eb4c Dawsh 2y ago include_date: bool = True,
67 01cdf2e Dawsh 2y ago include_time: bool = True,
68 a47eb4c Dawsh 2y ago ):
69 a47eb4c Dawsh 2y ago def get_datetime_format():
70 a47eb4c Dawsh 2y ago dt_format = ""
71 a47eb4c Dawsh 2y ago if include_date:
72 a47eb4c Dawsh 2y ago dt_format += "%d %B %Y"
73 a47eb4c Dawsh 2y ago if include_time:
74 a47eb4c Dawsh 2y ago if dt_format:
75 a47eb4c Dawsh 2y ago dt_format += ", "
76 a47eb4c Dawsh 2y ago dt_format += "%H:%M:%S"
77 a47eb4c Dawsh 2y ago
78 a47eb4c Dawsh 2y ago return dt_format
79 a47eb4c Dawsh 2y ago
80 a47eb4c Dawsh 2y ago if isinstance(date_time, int):
81 a47eb4c Dawsh 2y ago date_time = datetime.fromtimestamp(date_time)
82 a47eb4c Dawsh 2y ago
83 a47eb4c Dawsh 2y ago return date_time.strftime(get_datetime_format()) if date_time else "-"
84 a47eb4c Dawsh 2y ago
85 a47eb4c Dawsh 2y ago
86 a47eb4c Dawsh 2y ago def raise_if_falsy(obj: T, message: str) -> T:
87 a47eb4c Dawsh 2y ago if not obj:
88 a47eb4c Dawsh 2y ago error(text=message, auto_exit=True)
89 a47eb4c Dawsh 2y ago
90 a47eb4c Dawsh 2y ago return obj