File size: 2,668 Bytes
ae66b87
 
d809564
ae66b87
056eba8
ae66b87
056eba8
ae66b87
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
056eba8
 
 
 
 
b98f07f
ae66b87
 
 
3f84332
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import warnings
from functools import lru_cache
from pathlib import Path
from typing import Literal

PROJECT_ROOT = Path(__file__).parents[2]

CSS_FOLDER = PROJECT_ROOT / "src/assets/css"
if not CSS_FOLDER.exists():
    warnings.warn(f"CSS folder not found: {CSS_FOLDER.as_posix()!r}. Please check the folder exists.", stacklevel=2)
JS_FOLDER = PROJECT_ROOT / "src/assets/js"
if not JS_FOLDER.exists():
    warnings.warn(
        f"JavaScript folder not found: {JS_FOLDER.as_posix()!r}. Please check the folder exists.", stacklevel=2
    )
HTML_FOLDER = PROJECT_ROOT / "src/assets/html"
if not HTML_FOLDER.exists():
    warnings.warn(f"HTML folder not found: {HTML_FOLDER.as_posix()!r}. Please check the folder exists.", stacklevel=2)


custom_css = CSS_FOLDER / "custom.css"


# ------------------------------------------------------------
# Backend status indicator
# ------------------------------------------------------------
@lru_cache(maxsize=1)
def backend_status_js() -> str:
    file_path = JS_FOLDER / "backend-status.js"
    if not file_path.exists():
        raise FileNotFoundError(f"JavaScript file not found: {file_path.as_posix()!r}. Please check the file exists.")
    return file_path.read_text(encoding="utf-8")


@lru_cache(maxsize=3)
def backend_status_indicator_html(status: Literal["healthy", "unhealthy", "undefined"]) -> str:
    file_path = HTML_FOLDER / f"backend-status-indicator.{status}.html"
    if not file_path.exists():
        raise FileNotFoundError(f"HTML file not found: {file_path.as_posix()!r}. Please check the file exists.")
    return file_path.read_text(encoding="utf-8")


@lru_cache(maxsize=1)
def get_window_url_params() -> str:
    return """
    function(url_params) {
        const params = new URLSearchParams(window.location.search);
        url_params = Object.fromEntries(params);
        return url_params;
    }
    """


backend_status_indicator_css = CSS_FOLDER / "backend-status-indicator.css"


# ------------------------------------------------------------
# Submit form
# ------------------------------------------------------------
@lru_cache(maxsize=1)
def submit_html() -> str:
    file_path = HTML_FOLDER / "submit.html"
    if not file_path.exists():
        raise FileNotFoundError(f"HTML file not found: {file_path.as_posix()!r}. Please check the file exists.")
    return file_path.read_text(encoding="utf-8")


@lru_cache(maxsize=1)
def submit_js() -> str:
    file_path = JS_FOLDER / "submit.js"
    if not file_path.exists():
        raise FileNotFoundError(f"JavaScript file not found: {file_path.as_posix()!r}. Please check the file exists.")
    return file_path.read_text(encoding="utf-8")