server.web.utils.json_compat

Utility functions for JSON compatibility between old and new client versions.

Attributes

JSON_COMPAT_CUTOFF_VERSION

Functions

deserialize_requested_resources(→ Any)

Parse a task_resources.requested_resources column value.

normalize_node_ids(→ Optional[List[int]])

Normalize node_ids to a list of integers, supporting both old and new formats.

ensure_json_compatible_format(→ Any)

Ensure the node_ids are in a format that can be stored in the database.

get_client_compatibility_mode(→ str)

Determine the compatibility mode based on client version.

normalize_node_ids_for_client(→ Any)

Normalize node_ids based on client version compatibility.

Module Contents

server.web.utils.json_compat.deserialize_requested_resources(raw: str | None) Any

Parse a task_resources.requested_resources column value.

New rows are json.dumps’d by /task/bind_resources (lowercase true/false/null). Older rows are Python repr-style (True/False/None) from pre-FastAPI versions. Try JSON first, fall back to ast.literal_eval.

server.web.utils.json_compat.JSON_COMPAT_CUTOFF_VERSION = '3.4.23'
server.web.utils.json_compat.normalize_node_ids(node_ids: Any) List[int] | None

Normalize node_ids to a list of integers, supporting both old and new formats.

Parameters:

node_ids – Can be: - None - A list of integers (new format) - A JSON string like “[1, 2, 3]” (old format) - A string representation of a list like “[1, 2, 3]”

Returns:

List of integers or None if input is None/empty

Raises:

ValueError – If the input cannot be parsed into a list of integers

server.web.utils.json_compat.ensure_json_compatible_format(node_ids: Any) Any

Ensure the node_ids are in a format that can be stored in the database.

This function is used when storing data to ensure consistency.

Parameters:

node_ids – The node_ids to format

Returns:

The node_ids in a format suitable for database storage

server.web.utils.json_compat.get_client_compatibility_mode(client_version: str | None) str

Determine the compatibility mode based on client version.

Parameters:

client_version – The client version string (e.g., “3.4.10”, “3.4.24.dev1”, “3.4.24.stage1”)

Returns:

“old”, “new”, or “unknown” - “new” for versions with “dev” or “stage” in them - “old” for versions <= JSON_COMPAT_CUTOFF_VERSION - “new” for versions > JSON_COMPAT_CUTOFF_VERSION

Return type:

Compatibility mode

server.web.utils.json_compat.normalize_node_ids_for_client(node_ids: Any, client_version: str | None = None) Any

Normalize node_ids based on client version compatibility.

Parameters:
  • node_ids – The node_ids to normalize

  • client_version – The client version string

Returns:

  • For clients <= JSON_COMPAT_CUTOFF_VERSION: Returns quoted JSON string like “[1, 2]” (can be parsed with json.loads)

  • For clients > JSON_COMPAT_CUTOFF_VERSION: Returns unquoted JSON array like [1, 2]

Return type:

The node_ids in the format expected by the client