This commit is contained in:
2026-01-23 15:15:49 -06:00
parent b04a997561
commit 0c5686b3a7
9 changed files with 120 additions and 106 deletions

View File

@@ -14,12 +14,14 @@ from docker_compose.domain.compose.volume_files import VolumeFile
if TYPE_CHECKING: if TYPE_CHECKING:
from docker_compose.domain.paths.src import SrcPaths from docker_compose.domain.paths.src import SrcPaths
class ComposeDict(TypedDict): class ComposeDict(TypedDict):
name: str name: str
services: dict[str, ServiceWriteDict] services: dict[str, ServiceWriteDict]
networks: dict[str, NetworkDictSub] networks: dict[str, NetworkDictSub]
volumes: dict[str, Any] volumes: dict[str, Any]
@final @final
class Compose(Slots): class Compose(Slots):
def __init__(self, src_paths: SrcPaths) -> None: def __init__(self, src_paths: SrcPaths) -> None:
@@ -44,15 +46,12 @@ class Compose(Slots):
for network in service.networks: for network in service.networks:
yield network.as_dict yield network.as_dict
@property @property
def as_dict(self) -> ComposeDict: def as_dict(self) -> ComposeDict:
return { return {
'name':self.name, "name": self.name,
"services": dict(ChainMap(*(s.as_dict for s in self.services))), "services": dict(ChainMap(*(s.as_dict for s in self.services))),
"networks": dict(ChainMap( "networks": dict(ChainMap(*(self.networks))),
*(self.networks)
)),
"volumes": dict(ChainMap(*(vol.as_dict for vol in self.volumes))), "volumes": dict(ChainMap(*(vol.as_dict for vol in self.volumes))),
} }

View File

@@ -21,8 +21,12 @@ from docker_compose.util import ReplaceStr
if TYPE_CHECKING: if TYPE_CHECKING:
from docker_compose.domain.compose.compose import Compose from docker_compose.domain.compose.compose import Compose
class DependsOnDict(TypedDict): class DependsOnDict(TypedDict):
condition: Literal['service_started', 'service_healthy', 'service_completed_successfully'] condition: Literal[
"service_started", "service_healthy", "service_completed_successfully"
]
class HealthCheck(TypedDict): class HealthCheck(TypedDict):
test: tuple[str, ...] test: tuple[str, ...]
@@ -31,9 +35,12 @@ class HealthCheck(TypedDict):
retries: int | None retries: int | None
start_period: str | None start_period: str | None
class ServiceReadKeys(StrEnum): class ServiceReadKeys(StrEnum):
image='image' image = "image"
restart='restart' restart = "restart"
class ServiceReadDict(TypedDict): class ServiceReadDict(TypedDict):
image: str image: str
restart: NotRequired[str] restart: NotRequired[str]
@@ -70,6 +77,7 @@ class ServiceWriteDict(TypedDict):
ports: tuple[str, ...] ports: tuple[str, ...]
healthcheck: HealthCheck | None healthcheck: HealthCheck | None
@final @final
class Service(Slots): class Service(Slots):
_traefik_labels: tuple[str, ...] = ( _traefik_labels: tuple[str, ...] = (
@@ -89,21 +97,33 @@ class Service(Slots):
self.container_name: Final[str] = f"{DN.repl}_{self.service_name}" self.container_name: Final[str] = f"{DN.repl}_{self.service_name}"
self.image:Final[str] = data['image'] self.image: Final[str] = data["image"]
self.user:Final[str | None] = data.get('user') self.user: Final[str | None] = data.get("user")
self.shm_size:Final[str | None] = data.get('shm_size') self.shm_size: Final[str | None] = data.get("shm_size")
self.restart:Final[str] = data.get('restart',"unless-stopped") self.restart: Final[str] = data.get("restart", "unless-stopped")
self.depends_on:Final[str | dict[str, DependsOnDict] | None] = data.get('depends_on') self.depends_on: Final[str | dict[str, DependsOnDict] | None] = data.get(
self.command:Final[tuple[str, ...]] = self.string_lists(data, 'command') "depends_on"
self.entrypoint:Final[tuple[str, ...]] = self.string_lists(data,'entrypoint') )
self.environment:Final[dict[str, str]] = self.string_dict(data.get('environment',{}) ) self.command: Final[tuple[str, ...]] = self.string_lists(data, "command")
self.labels_raw:Final[tuple[str, ...]] = self.string_lists(data,'labels') self.entrypoint: Final[tuple[str, ...]] = self.string_lists(data, "entrypoint")
self.logging:Final[tuple[str, ...]] =self.string_lists( data,'logging') self.environment: Final[dict[str, str]] = self.string_dict(
self.networks:Final[tuple[Network, ...]] = tuple(Network(self, s) for s in data.get('networks', ()) ) data.get("environment", {})
self.security_opt:Final[tuple[str, ...]] = self.string_lists(data,'security_opts') )
self.volumes:Final[tuple[Volumes, ...]] = tuple(Volumes(self, s) for s in data.get('volumes', ()) ) self.labels_raw: Final[tuple[str, ...]] = self.string_lists(data, "labels")
self.ports:Final[tuple[Port, ...]] = tuple(Port(s) for s in data.get('ports', ())) self.logging: Final[tuple[str, ...]] = self.string_lists(data, "logging")
self.healthcheck:Final[HealthCheck | None] = data.get('healthcheck') self.networks: Final[tuple[Network, ...]] = tuple(
Network(self, s) for s in data.get("networks", ())
)
self.security_opt: Final[tuple[str, ...]] = self.string_lists(
data, "security_opts"
)
self.volumes: Final[tuple[Volumes, ...]] = tuple(
Volumes(self, s) for s in data.get("volumes", ())
)
self.ports: Final[tuple[Port, ...]] = tuple(
Port(s) for s in data.get("ports", ())
)
self.healthcheck: Final[HealthCheck | None] = data.get("healthcheck")
@property @property
def as_dict(self) -> dict[str, ServiceWriteDict]: def as_dict(self) -> dict[str, ServiceWriteDict]:
@@ -128,7 +148,6 @@ class Service(Slots):
) )
} }
def __iter__(self) -> Generator[ReplaceStr]: def __iter__(self) -> Generator[ReplaceStr]:
yield FQDN yield FQDN
yield DN yield DN

View File

@@ -13,6 +13,7 @@ if TYPE_CHECKING:
@final @final
class Volumes(Slots): class Volumes(Slots):
sep = ":" sep = ":"
def __init__(self, service: Service, raw: str) -> None: def __init__(self, service: Service, raw: str) -> None:
src, dest = (s.strip() for s in raw.split(self.sep)) src, dest = (s.strip() for s in raw.split(self.sep))
self.service: Final[Service] = service self.service: Final[Service] = service

View File

@@ -10,14 +10,13 @@ from docker_compose.domain.env.env_row import EnvRow
if TYPE_CHECKING: if TYPE_CHECKING:
from docker_compose.domain.paths.src import SrcPaths from docker_compose.domain.paths.src import SrcPaths
@final @final
class EnvData(Slots): class EnvData(Slots):
def __init__(self, src_paths: SrcPaths) -> None: def __init__(self, src_paths: SrcPaths) -> None:
self.src_paths: Final = src_paths self.src_paths: Final = src_paths
self.data: Final = tuple(self.lines) self.data: Final = tuple(self.lines)
@property @property
def lines(self) -> Generator[EnvRow]: def lines(self) -> Generator[EnvRow]:
with self.src_paths.env_file.open(mode="rt") as f: with self.src_paths.env_file.open(mode="rt") as f:
@@ -29,5 +28,3 @@ class EnvData(Slots):
@property @property
def as_list(self): def as_list(self):
return tuple(str(row) for row in self.data) return tuple(str(row) for row in self.data)

View File

@@ -7,7 +7,6 @@ from autoslot import Slots
from docker_compose import APP_ROOT from docker_compose import APP_ROOT
from docker_compose.domain.paths import FILES from docker_compose.domain.paths import FILES
from docker_compose.domain.paths.org import OrgData
if TYPE_CHECKING: if TYPE_CHECKING:
from docker_compose.domain.paths.org import OrgData from docker_compose.domain.paths.org import OrgData
@@ -20,4 +19,3 @@ class DestPath(Slots):
self.base_path: Final[Path] = APP_ROOT.joinpath(*self.org_data) self.base_path: Final[Path] = APP_ROOT.joinpath(*self.org_data)
self.compose_path: Final[Path] = self.base_path.joinpath(FILES.COMPOSE) self.compose_path: Final[Path] = self.base_path.joinpath(FILES.COMPOSE)
self.env_path: Final[Path] = self.base_path.joinpath(FILES.ENV) self.env_path: Final[Path] = self.base_path.joinpath(FILES.ENV)

View File

@@ -17,7 +17,9 @@ class SrcPaths(Slots):
self.path: Final = path self.path: Final = path
self.compose: Final = Compose(self) self.compose: Final = Compose(self)
self.cfg:Final[dict[Orgs,OrgData]]= {cast(Orgs,obj.org): obj for obj in OrgData.from_src_path(self)} self.cfg: Final[dict[Orgs, OrgData]] = {
cast(Orgs, obj.org): obj for obj in OrgData.from_src_path(self)
}
self.env: Final = EnvData(self) self.env: Final = EnvData(self)
self.compose_file: Final = self.path.joinpath(FILES.COMPOSE) self.compose_file: Final = self.path.joinpath(FILES.COMPOSE)
self.bind_vol_path: Final = self.path.joinpath(FILES.BIND_VOLS) self.bind_vol_path: Final = self.path.joinpath(FILES.BIND_VOLS)

View File

@@ -5,7 +5,6 @@ from typing import TYPE_CHECKING, Final, final, override
from autoslot import Slots from autoslot import Slots
from docker_compose.domain.compose.compose import Compose from docker_compose.domain.compose.compose import Compose
from docker_compose.domain.paths.org import OrgData
from docker_compose.domain.render.bind_vols import BindVols from docker_compose.domain.render.bind_vols import BindVols
if TYPE_CHECKING: if TYPE_CHECKING:
@@ -29,4 +28,3 @@ class Render(Slots):
def __call__(self): def __call__(self):
with self.org_data.dest.compose_path.open("wt") as f: with self.org_data.dest.compose_path.open("wt") as f:
_ = f.write(str(self)) _ = f.write(str(self))