36 lines
1.0 KiB
Python
36 lines
1.0 KiB
Python
from __future__ import annotations
|
|
|
|
from collections.abc import Generator
|
|
from typing import TYPE_CHECKING, Any, cast, final
|
|
|
|
from pydantic import Field, model_serializer
|
|
from pydantic.dataclasses import dataclass
|
|
from pydantic_core.core_schema import SerializerFunctionWrapHandler
|
|
|
|
from docker_compose.domain.env.env_row import EnvRow
|
|
|
|
if TYPE_CHECKING:
|
|
from docker_compose.domain.paths.src import SrcPaths
|
|
|
|
|
|
@final
|
|
@dataclass(slots=True)
|
|
class EnvData:
|
|
src_paths: SrcPaths
|
|
data: tuple[EnvRow, ...] = Field(init=False)
|
|
|
|
def __post_init__(self):
|
|
self.data = tuple(self.lines)
|
|
|
|
@property
|
|
def lines(self) -> Generator[EnvRow]:
|
|
with self.src_paths.env_file.open(mode="rt") as f:
|
|
for line in f:
|
|
if line.startswith("#"):
|
|
continue
|
|
yield EnvRow.from_str(self, line)
|
|
|
|
@model_serializer(mode="wrap")
|
|
def serialize_model(self, handler: SerializerFunctionWrapHandler) -> list[str]:
|
|
return cast(dict[str, Any], handler(self))["data"] # pyright: ignore[reportAny]
|