pyxora.utils.platform

  1import sys
  2
  3__all__ = [
  4    "get_platform","get_web_platform",
  5    "is_web","is_local",
  6    "is_windows","is_linux","is_mac","is_android"
  7]
  8
  9def get_platform() -> str:
 10    """
 11    Get the platform string from the system.
 12
 13    Returns:
 14        str: The platform (e.g., 'win32', 'linux', 'darwin', 'emscripten').
 15    """
 16    return sys.platform
 17
 18
 19def is_windows() -> bool:
 20    """
 21    Check if the current platform is Windows.
 22
 23    Returns:
 24        bool: True if on Windows.
 25    """
 26    return get_platform().startswith("win")
 27
 28
 29def is_linux() -> bool:
 30    """
 31    Check if the current platform is Linux.
 32
 33    Returns:
 34        bool: True if on Linux.
 35    """
 36    return get_platform().startswith("linux")
 37
 38
 39def is_mac() -> bool:
 40    """
 41    Check if the current platform is macOS.
 42
 43    Returns:
 44        bool: True if on macOS.
 45    """
 46    return get_platform().startswith("darwin")
 47
 48
 49def is_android() -> bool:
 50    """
 51    Check if the current platform is Android.
 52
 53    Returns:
 54        bool: True if on Android.
 55    """
 56    return hasattr(sys, "getandroidapilevel")
 57
 58
 59def is_local() -> bool:
 60    """
 61    Check if the current platform is a local/native one (not web).
 62
 63    Returns:
 64        bool: True if running on Windows, Linux, macOS, or Android.
 65    """
 66    return is_windows() or is_linux() or is_mac() or is_android()
 67
 68
 69def is_web() -> bool:
 70    """
 71    Check if the game is running in a web environment (Emscripten).
 72
 73    Returns:
 74        bool: True if compiled with Emscripten and running in a browser.
 75    """
 76    return get_platform() == "emscripten"
 77
 78
 79def get_web_platform() -> str | None:
 80    """
 81    Attempt to determine the user's OS/platform from the browser user agent.
 82
 83    Only works if running in a web context (via Emscripten). Parses
 84    the JavaScript `navigator.userAgent`.
 85
 86    Returns:
 87        str | None: One of ['ios', 'android', 'win', 'mac', 'linux', 'unknown'], or None if not in web.
 88    """
 89    if not is_web():
 90        return None
 91
 92    # Import JavaScript interop module available in Pyodide/Emscripten
 93    user_agent = __import__("js").navigator.userAgent.lower()
 94    web_platforms = {
 95        "iphone": "ios",
 96        "ipad": "ios",
 97        "android": "android",
 98        "windows": "win",
 99        "macintosh": "mac",
100        "linux": "linux"
101    }
102    return next((v for k, v in web_platforms.items() if k in user_agent), "unknown")
def get_platform() -> str:
10def get_platform() -> str:
11    """
12    Get the platform string from the system.
13
14    Returns:
15        str: The platform (e.g., 'win32', 'linux', 'darwin', 'emscripten').
16    """
17    return sys.platform

Get the platform string from the system.

Returns:

str: The platform (e.g., 'win32', 'linux', 'darwin', 'emscripten').

def get_web_platform() -> str | None:
 80def get_web_platform() -> str | None:
 81    """
 82    Attempt to determine the user's OS/platform from the browser user agent.
 83
 84    Only works if running in a web context (via Emscripten). Parses
 85    the JavaScript `navigator.userAgent`.
 86
 87    Returns:
 88        str | None: One of ['ios', 'android', 'win', 'mac', 'linux', 'unknown'], or None if not in web.
 89    """
 90    if not is_web():
 91        return None
 92
 93    # Import JavaScript interop module available in Pyodide/Emscripten
 94    user_agent = __import__("js").navigator.userAgent.lower()
 95    web_platforms = {
 96        "iphone": "ios",
 97        "ipad": "ios",
 98        "android": "android",
 99        "windows": "win",
100        "macintosh": "mac",
101        "linux": "linux"
102    }
103    return next((v for k, v in web_platforms.items() if k in user_agent), "unknown")

Attempt to determine the user's OS/platform from the browser user agent.

Only works if running in a web context (via Emscripten). Parses the JavaScript navigator.userAgent.

Returns:

str | None: One of ['ios', 'android', 'win', 'mac', 'linux', 'unknown'], or None if not in web.

def is_web() -> bool:
70def is_web() -> bool:
71    """
72    Check if the game is running in a web environment (Emscripten).
73
74    Returns:
75        bool: True if compiled with Emscripten and running in a browser.
76    """
77    return get_platform() == "emscripten"

Check if the game is running in a web environment (Emscripten).

Returns:

bool: True if compiled with Emscripten and running in a browser.

def is_local() -> bool:
60def is_local() -> bool:
61    """
62    Check if the current platform is a local/native one (not web).
63
64    Returns:
65        bool: True if running on Windows, Linux, macOS, or Android.
66    """
67    return is_windows() or is_linux() or is_mac() or is_android()

Check if the current platform is a local/native one (not web).

Returns:

bool: True if running on Windows, Linux, macOS, or Android.

def is_windows() -> bool:
20def is_windows() -> bool:
21    """
22    Check if the current platform is Windows.
23
24    Returns:
25        bool: True if on Windows.
26    """
27    return get_platform().startswith("win")

Check if the current platform is Windows.

Returns:

bool: True if on Windows.

def is_linux() -> bool:
30def is_linux() -> bool:
31    """
32    Check if the current platform is Linux.
33
34    Returns:
35        bool: True if on Linux.
36    """
37    return get_platform().startswith("linux")

Check if the current platform is Linux.

Returns:

bool: True if on Linux.

def is_mac() -> bool:
40def is_mac() -> bool:
41    """
42    Check if the current platform is macOS.
43
44    Returns:
45        bool: True if on macOS.
46    """
47    return get_platform().startswith("darwin")

Check if the current platform is macOS.

Returns:

bool: True if on macOS.

def is_android() -> bool:
50def is_android() -> bool:
51    """
52    Check if the current platform is Android.
53
54    Returns:
55        bool: True if on Android.
56    """
57    return hasattr(sys, "getandroidapilevel")

Check if the current platform is Android.

Returns:

bool: True if on Android.