Skip to content

Commit 91e74d2

Browse files
committed
✨ Refactor: Replace json.loads with json_loads for consistency across director and registry_proxy modules
1 parent 33e1704 commit 91e74d2

File tree

3 files changed

+26
-27
lines changed

3 files changed

+26
-27
lines changed

services/catalog/src/simcore_service_catalog/clients/director.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import asyncio
22
import functools
3-
import json
43
import logging
54
import urllib.parse
65
from collections.abc import AsyncIterator, Awaitable, Callable
@@ -9,7 +8,7 @@
98
from typing import Any, Final
109

1110
import httpx
12-
from common_library.json_serialization import json_dumps
11+
from common_library.json_serialization import json_dumps, json_loads
1312
from fastapi import FastAPI, HTTPException
1413
from fastapi_lifespan_manager import State
1514
from models_library.api_schemas_directorv2.services import ServiceExtras
@@ -221,7 +220,7 @@ async def get_service_extras(
221220
_logger.debug("Compiling service extras from labels %s", pformat(labels))
222221

223222
if _SERVICE_RUNTIME_SETTINGS in labels:
224-
service_settings: list[dict[str, Any]] = json.loads(
223+
service_settings: list[dict[str, Any]] = json_loads(
225224
labels[_SERVICE_RUNTIME_SETTINGS]
226225
)
227226
for entry in service_settings:

services/director/src/simcore_service_director/producer.py

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import arrow
1414
import httpx
1515
import tenacity
16+
from common_library.json_serialization import json_loads
1617
from fastapi import FastAPI, status
1718
from packaging.version import Version
1819
from servicelib.async_utils import run_sequentially_in_context
@@ -135,7 +136,7 @@ async def _read_service_settings(
135136
) -> dict[str, Any] | list[Any] | None:
136137
image_labels, _ = await registry_proxy.get_image_labels(app, key, tag)
137138
settings: dict[str, Any] | list[Any] | None = (
138-
json.loads(image_labels[settings_name])
139+
json_loads(image_labels[settings_name])
139140
if settings_name in image_labels
140141
else None
141142
)
@@ -306,28 +307,28 @@ async def _create_docker_service_params(
306307
] += f", {service_name}_stripprefixregex"
307308

308309
placement_constraints_to_substitute: list[str] = []
309-
placement_substitutions: dict[
310-
str, str
311-
] = app_settings.DIRECTOR_GENERIC_RESOURCE_PLACEMENT_CONSTRAINTS_SUBSTITUTIONS
310+
placement_substitutions: dict[str, str] = (
311+
app_settings.DIRECTOR_GENERIC_RESOURCE_PLACEMENT_CONSTRAINTS_SUBSTITUTIONS
312+
)
312313
assert isinstance(service_parameters_labels, list) # nosec
313314
for param in service_parameters_labels:
314315
_check_setting_correctness(param)
315316
# replace %service_uuid% by the given uuid
316317
if str(param["value"]).find("%service_uuid%") != -1:
317318
dummy_string = json.dumps(param["value"])
318319
dummy_string = dummy_string.replace("%service_uuid%", node_uuid)
319-
param["value"] = json.loads(dummy_string)
320+
param["value"] = json_loads(dummy_string)
320321

321322
if param["type"] == "Resources":
322323
# python-API compatible for backward compatibility
323324
if "mem_limit" in param["value"]:
324-
docker_params["task_template"]["Resources"]["Limits"][
325-
"MemoryBytes"
326-
] = param["value"]["mem_limit"]
325+
docker_params["task_template"]["Resources"]["Limits"]["MemoryBytes"] = (
326+
param["value"]["mem_limit"]
327+
)
327328
if "cpu_limit" in param["value"]:
328-
docker_params["task_template"]["Resources"]["Limits"][
329-
"NanoCPUs"
330-
] = param["value"]["cpu_limit"]
329+
docker_params["task_template"]["Resources"]["Limits"]["NanoCPUs"] = (
330+
param["value"]["cpu_limit"]
331+
)
331332
if "mem_reservation" in param["value"]:
332333
docker_params["task_template"]["Resources"]["Reservations"][
333334
"MemoryBytes"
@@ -379,11 +380,11 @@ async def _create_docker_service_params(
379380

380381
# publishing port on the ingress network.
381382
elif param["name"] == "ports" and param["type"] == "int": # backward comp
382-
docker_params["labels"][
383-
_to_simcore_runtime_docker_label_key("port")
384-
] = docker_params["labels"][
385-
f"traefik.http.services.{service_name}.loadbalancer.server.port"
386-
] = str(
383+
docker_params["labels"][_to_simcore_runtime_docker_label_key("port")] = (
384+
docker_params["labels"][
385+
f"traefik.http.services.{service_name}.loadbalancer.server.port"
386+
]
387+
) = str(
387388
param["value"]
388389
)
389390
# REST-API compatible
@@ -445,11 +446,9 @@ async def _create_docker_service_params(
445446
] = container_spec["Labels"][
446447
_to_simcore_runtime_docker_label_key("cpu_limit")
447448
] = f"{float(nano_cpus_limit) / 1e9}"
448-
docker_params["labels"][
449-
_to_simcore_runtime_docker_label_key("memory_limit")
450-
] = container_spec["Labels"][
451-
_to_simcore_runtime_docker_label_key("memory_limit")
452-
] = mem_limit
449+
docker_params["labels"][_to_simcore_runtime_docker_label_key("memory_limit")] = (
450+
container_spec["Labels"][_to_simcore_runtime_docker_label_key("memory_limit")]
451+
) = mem_limit
453452

454453
# and make the container aware of them via env variables
455454
resource_limits = {

services/director/src/simcore_service_director/registry_proxy.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import httpx
1010
from aiocache import Cache, SimpleMemoryCache # type: ignore[import-untyped]
11+
from common_library.json_serialization import json_loads
1112
from fastapi import FastAPI, status
1213
from servicelib.async_utils import cancel_wait_task
1314
from servicelib.background_task import create_periodic_task
@@ -380,7 +381,7 @@ async def get_image_labels(
380381
request_result, headers = await registry_request(
381382
app, path=path, method="GET", use_cache=not update_cache
382383
)
383-
v1_compatibility_key = json.loads(request_result["history"][0]["v1Compatibility"])
384+
v1_compatibility_key = json_loads(request_result["history"][0]["v1Compatibility"])
384385
container_config: dict[str, Any] = v1_compatibility_key.get(
385386
"container_config", v1_compatibility_key["config"]
386387
)
@@ -413,7 +414,7 @@ async def get_image_details(
413414
if not key.startswith("io.simcore."):
414415
continue
415416
try:
416-
label_data = json.loads(labels[key])
417+
label_data = json_loads(labels[key])
417418
for label_key in label_data:
418419
image_details[label_key] = label_data[label_key]
419420
except json.decoder.JSONDecodeError:
@@ -483,7 +484,7 @@ async def list_interactive_service_dependencies(
483484
dependency_keys = []
484485
if DEPENDENCIES_LABEL_KEY in image_labels:
485486
try:
486-
dependencies = json.loads(image_labels[DEPENDENCIES_LABEL_KEY])
487+
dependencies = json_loads(image_labels[DEPENDENCIES_LABEL_KEY])
487488
dependency_keys = [
488489
{"key": dependency["key"], "tag": dependency["tag"]}
489490
for dependency in dependencies

0 commit comments

Comments
 (0)