Enhance configuration classes with detailed docstrings and improve YAML saving functionality in DockerComposeManager
This commit is contained in:
@ -35,22 +35,26 @@ class ServiceConfig(BaseModel):
|
|||||||
deploy: dict[str, Any] | None = None
|
deploy: dict[str, Any] | None = None
|
||||||
resources: dict[str, Any] | None = None
|
resources: dict[str, Any] | None = None
|
||||||
|
|
||||||
|
# Allow extra fields for flexibility and to support arbitrary Docker Compose extensions.
|
||||||
model_config = {"extra": "allow"}
|
model_config = {"extra": "allow"}
|
||||||
|
|
||||||
|
|
||||||
class VolumeConfig(BaseModel):
|
class VolumeConfig(BaseModel):
|
||||||
# Add more fields as needed for Docker Compose volumes
|
"""Configuration for a Docker Compose volume.
|
||||||
|
|
||||||
|
Represents the configuration options for defining a volume in a Docker Compose file.
|
||||||
|
"""
|
||||||
|
|
||||||
driver: str | None = None
|
driver: str | None = None
|
||||||
driver_opts: dict[str, Any] | None = None
|
driver_opts: dict[str, Any] | None = None
|
||||||
external: bool | dict[str, Any] | None = None
|
external: bool | dict[str, Any] | None = None
|
||||||
labels: dict[str, str] | list[str] | None = None
|
labels: dict[str, str] | list[str] | None = None
|
||||||
name: str | None = None
|
name: str | None = None
|
||||||
|
|
||||||
model_config = {"extra": "allow"}
|
|
||||||
|
|
||||||
|
|
||||||
class NetworkConfig(BaseModel):
|
class NetworkConfig(BaseModel):
|
||||||
# Add more fields as needed for Docker Compose networks
|
"""Represents a network configuration for a Docker Compose file."""
|
||||||
|
|
||||||
driver: str | None = None
|
driver: str | None = None
|
||||||
driver_opts: dict[str, Any] | None = None
|
driver_opts: dict[str, Any] | None = None
|
||||||
attachable: bool | None = None
|
attachable: bool | None = None
|
||||||
@ -58,6 +62,7 @@ class NetworkConfig(BaseModel):
|
|||||||
labels: dict[str, str] | list[str] | None = None
|
labels: dict[str, str] | list[str] | None = None
|
||||||
name: str | None = None
|
name: str | None = None
|
||||||
|
|
||||||
|
# Allow extra fields for flexibility and to support arbitrary Docker Compose extensions.
|
||||||
model_config = {"extra": "allow"}
|
model_config = {"extra": "allow"}
|
||||||
|
|
||||||
|
|
||||||
@ -66,6 +71,11 @@ if TYPE_CHECKING:
|
|||||||
|
|
||||||
|
|
||||||
class DockerComposeManager:
|
class DockerComposeManager:
|
||||||
|
"""A class to create and modify Docker Compose YAML files programmatically.
|
||||||
|
|
||||||
|
Supports context manager usage for auto-saving.
|
||||||
|
"""
|
||||||
|
|
||||||
def add_volume(self, name: str, config: VolumeConfig | dict[str, Any] | None = None) -> DockerComposeManager:
|
def add_volume(self, name: str, config: VolumeConfig | dict[str, Any] | None = None) -> DockerComposeManager:
|
||||||
"""Add a top-level volume definition.
|
"""Add a top-level volume definition.
|
||||||
|
|
||||||
@ -122,11 +132,6 @@ class DockerComposeManager:
|
|||||||
self._dirty = True
|
self._dirty = True
|
||||||
return self
|
return self
|
||||||
|
|
||||||
"""A class to create and modify Docker Compose YAML files programmatically.
|
|
||||||
|
|
||||||
Supports context manager usage for auto-saving.
|
|
||||||
"""
|
|
||||||
|
|
||||||
def __init__(self, path: str, version: str = "3.8") -> None:
|
def __init__(self, path: str, version: str = "3.8") -> None:
|
||||||
"""Initialize the manager with a YAML file path. Loads existing file or creates a new one."""
|
"""Initialize the manager with a YAML file path. Loads existing file or creates a new one."""
|
||||||
self.path: str = path
|
self.path: str = path
|
||||||
@ -249,7 +254,8 @@ class DockerComposeManager:
|
|||||||
def save(self) -> None:
|
def save(self) -> None:
|
||||||
"""Save the current state to the YAML file."""
|
"""Save the current state to the YAML file."""
|
||||||
with Path(self.path).open("w", encoding="utf-8") as f:
|
with Path(self.path).open("w", encoding="utf-8") as f:
|
||||||
yaml.dump(self._data, f, sort_keys=False, indent=2)
|
yaml.dump(self._data, f, sort_keys=False, indent=2, default_flow_style=False)
|
||||||
|
self._dirty = False
|
||||||
self._dirty = False
|
self._dirty = False
|
||||||
|
|
||||||
def __enter__(self) -> Self:
|
def __enter__(self) -> Self:
|
||||||
|
Reference in New Issue
Block a user