jmri_server.context

ServerContext — the shared state every service can see.

Owns the connection, service registry, WebSocket connection manager, and metadata (railroad name, node identity, active profile). Constructed once at app startup and passed into each JsonService instance.

 1"""``ServerContext`` — the shared state every service can see.
 2
 3Owns the connection, service registry, WebSocket connection manager, and
 4metadata (railroad name, node identity, active profile). Constructed once
 5at app startup and passed into each :class:`JsonService` instance.
 6"""
 7from __future__ import annotations
 8
 9import platform
10import uuid
11from dataclasses import dataclass, field
12
13from jmri_core.hal.connection import SystemConnection
14
15
16@dataclass
17class ServerMetadata:
18    """Server-identity metadata returned by the hello/version/railroad services."""
19
20    jmri_version: str = "5.14.0-py"
21    json_protocol_version: str = "5.7.0"
22    railroad: str = "Python JMRI"
23    node: str = field(default_factory=lambda: f"node-{platform.node()}-{uuid.uuid4().hex[:8]}")
24    active_profile: str | None = "default"
25    heartbeat_ms: int = 15000
26
27
28@dataclass
29class ServerContext:
30    """Container for everything a service needs at runtime."""
31
32    connection: SystemConnection
33    metadata: ServerMetadata = field(default_factory=ServerMetadata)
34    # Set during app startup by ``create_app``. Bean services use this to
35    # persist created/edited beans to PanelPro.xml. Optional so tests
36    # that build the app via ``build_app`` directly leave it ``None`` for
37    # hermeticity (no writes to the user's real settings dir).
38    config_store: object | None = None
39    # JMRI's ``profiles.xml``-driven multi-profile model. Populated by
40    # ``create_app`` when the settings directory has a ``profiles.xml``;
41    # tests can leave it None.
42    profile_registry: object | None = None
43    # Profile-scoped registry of LogixNG programs. Constructed lazily in
44    # :func:`build_app` so every ServerContext (including the ones
45    # tests spin up via ``build_app``) has a real manager without
46    # needing a profile on disk. ``create_app`` additionally
47    # populates it from ``<profile>/logixng/*.yaml`` at startup.
48    logix_ng: object | None = None
49    # Auth subsystem — populated by ``attach_auth`` when
50    # ``JMRI_AUTH_ENABLED=1``, else stays ``None`` (auth off, every
51    # request is admin-equivalent). Typed as ``object`` here so the
52    # ``jmri_server.auth`` module isn't imported when auth is off.
53    # See :class:`jmri_server.auth.manager.AuthManager` for the
54    # runtime shape.
55    auth: object | None = None
@dataclass
class ServerMetadata:
17@dataclass
18class ServerMetadata:
19    """Server-identity metadata returned by the hello/version/railroad services."""
20
21    jmri_version: str = "5.14.0-py"
22    json_protocol_version: str = "5.7.0"
23    railroad: str = "Python JMRI"
24    node: str = field(default_factory=lambda: f"node-{platform.node()}-{uuid.uuid4().hex[:8]}")
25    active_profile: str | None = "default"
26    heartbeat_ms: int = 15000

Server-identity metadata returned by the hello/version/railroad services.

ServerMetadata( jmri_version: str = '5.14.0-py', json_protocol_version: str = '5.7.0', railroad: str = 'Python JMRI', node: str = <factory>, active_profile: str | None = 'default', heartbeat_ms: int = 15000)
jmri_version: str = '5.14.0-py'
json_protocol_version: str = '5.7.0'
railroad: str = 'Python JMRI'
node: str
active_profile: str | None = 'default'
heartbeat_ms: int = 15000
@dataclass
class ServerContext:
29@dataclass
30class ServerContext:
31    """Container for everything a service needs at runtime."""
32
33    connection: SystemConnection
34    metadata: ServerMetadata = field(default_factory=ServerMetadata)
35    # Set during app startup by ``create_app``. Bean services use this to
36    # persist created/edited beans to PanelPro.xml. Optional so tests
37    # that build the app via ``build_app`` directly leave it ``None`` for
38    # hermeticity (no writes to the user's real settings dir).
39    config_store: object | None = None
40    # JMRI's ``profiles.xml``-driven multi-profile model. Populated by
41    # ``create_app`` when the settings directory has a ``profiles.xml``;
42    # tests can leave it None.
43    profile_registry: object | None = None
44    # Profile-scoped registry of LogixNG programs. Constructed lazily in
45    # :func:`build_app` so every ServerContext (including the ones
46    # tests spin up via ``build_app``) has a real manager without
47    # needing a profile on disk. ``create_app`` additionally
48    # populates it from ``<profile>/logixng/*.yaml`` at startup.
49    logix_ng: object | None = None
50    # Auth subsystem — populated by ``attach_auth`` when
51    # ``JMRI_AUTH_ENABLED=1``, else stays ``None`` (auth off, every
52    # request is admin-equivalent). Typed as ``object`` here so the
53    # ``jmri_server.auth`` module isn't imported when auth is off.
54    # See :class:`jmri_server.auth.manager.AuthManager` for the
55    # runtime shape.
56    auth: object | None = None

Container for everything a service needs at runtime.

ServerContext( connection: jmri_core.hal.SystemConnection, metadata: ServerMetadata = <factory>, config_store: object | None = None, profile_registry: object | None = None, logix_ng: object | None = None, auth: object | None = None)
metadata: ServerMetadata
config_store: object | None = None
profile_registry: object | None = None
logix_ng: object | None = None
auth: object | None = None