From 6a70beb15671a4031665bb85a15232cfcd120af7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20Hells=C3=A9n?= Date: Wed, 18 Jun 2025 04:44:14 +0200 Subject: [PATCH] Enhance configuration classes with detailed docstrings and improve YAML saving functionality in DockerComposeManager --- src/compose/__init__.py | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/src/compose/__init__.py b/src/compose/__init__.py index b434e4d..867f7c0 100644 --- a/src/compose/__init__.py +++ b/src/compose/__init__.py @@ -35,22 +35,26 @@ class ServiceConfig(BaseModel): deploy: 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"} 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_opts: dict[str, Any] | None = None external: bool | dict[str, Any] | None = None labels: dict[str, str] | list[str] | None = None name: str | None = None - model_config = {"extra": "allow"} - 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_opts: dict[str, Any] | None = None attachable: bool | None = None @@ -58,6 +62,7 @@ class NetworkConfig(BaseModel): labels: dict[str, str] | list[str] | None = None name: str | None = None + # Allow extra fields for flexibility and to support arbitrary Docker Compose extensions. model_config = {"extra": "allow"} @@ -66,6 +71,11 @@ if TYPE_CHECKING: 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: """Add a top-level volume definition. @@ -122,11 +132,6 @@ class DockerComposeManager: self._dirty = True 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: """Initialize the manager with a YAML file path. Loads existing file or creates a new one.""" self.path: str = path @@ -249,7 +254,8 @@ class DockerComposeManager: def save(self) -> None: """Save the current state to the YAML file.""" 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 def __enter__(self) -> Self: