Files
thePlug/app/utils/docker/ports.py
T
reloia 4fa25355e7 chore: refactor all docker logic files structure
- Implement compose.py with functions to find compose files, list services, and aggregate service info
- Add editor.py for changing service ports with backup support
- Define DockerService model in models.py
- Refactor ports.py to support new port mapping logic and helpers
- Expose Docker Compose utilities in __init__.py
2026-04-12 19:46:48 +00:00

66 lines
1.8 KiB
Python

from collections.abc import Mapping
def _get_service_port(mapping: str | int | Mapping[str, object]) -> int | None:
if isinstance(mapping, int):
return mapping
if isinstance(mapping, Mapping):
published = mapping.get("published")
if published is None:
return None
try:
return int(published)
except (TypeError, ValueError):
return None
parts = str(mapping).split(":")
match parts:
case [""]:
return None
case [port] | [port, _]:
try:
return int(port)
except ValueError:
return None
case ["localhost" | "127.0.0.1", _, _]:
return None
case [_, port, _]:
try:
return int(port)
except ValueError:
return None
case _:
return None
def _replace_port_in_mapping(
mapping: str | int | Mapping[str, object],
port_from: int,
port_to: int,
) -> str | dict[str, object]:
if isinstance(mapping, Mapping):
updated = dict(mapping)
if _get_service_port(updated) == port_from:
updated["published"] = str(port_to)
return updated
parts = str(mapping).split(":")
match parts:
case [_]:
return str(port_to)
case [_, container_port]:
return f"{port_to}:{container_port}"
case [ip, _, container_port]:
return f"{ip}:{port_to}:{container_port}"
case _:
return str(port_to)
def _get_port_index(mappings: list[str | int | Mapping[str, object]], port: int) -> int | None:
for i, mapping in enumerate(mappings):
if _get_service_port(mapping) == port:
return i
return None