File size: 1,198 Bytes
981aa5d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
from __future__ import annotations

import sys
from collections.abc import Callable


GUI_SYSTEM_PACKAGES = (
    "libgirepository-2.0-dev",
    "gobject-introspection",
    "libgtk-3-dev",
    "libcairo2-dev",
    "pkg-config",
)


def format_missing_gi_message() -> str:
    packages = " ".join(GUI_SYSTEM_PACKAGES)
    return (
        "CodexDashBoard needs PyGObject / GTK to start.\n"
        "Install Ubuntu/Pop!_OS system packages:\n"
        f"  sudo apt-get install -y {packages}\n"
        "Then install the Python GUI extra:\n"
        "  python3 -m pip install -e '.[gui]'"
    )


def run(app_main_loader: Callable[[], Callable[[], int]] | None = None, *, err=None) -> int:
    err = sys.stderr.write if err is None else err
    app_main_loader = load_app_main if app_main_loader is None else app_main_loader
    try:
        app_main = app_main_loader()
    except ImportError as exc:
        text = str(exc)
        if "gi" in text or "Gtk" in text or "Gdk" in text or "Ayatana" in text:
            err(format_missing_gi_message() + "\n")
            return 1
        raise
    return app_main()


def load_app_main() -> Callable[[], int]:
    from .app import main

    return main