82 lines
2.3 KiB
Python
82 lines
2.3 KiB
Python
from collections.abc import Callable, Iterator
|
|
from dataclasses import dataclass, field
|
|
from os import sep
|
|
from pathlib import Path
|
|
from typing import Self, final
|
|
|
|
from docker_compose.cfg import DATA_ROOT
|
|
from docker_compose.cfg.org import AppVal, OrgData, OrgVal
|
|
from docker_compose.cfg.record import Record, RecordName
|
|
|
|
|
|
@final
|
|
@dataclass(frozen=True, slots=True)
|
|
class ComposeFileRendered:
|
|
path: Path
|
|
|
|
def write(self, data: str) -> None:
|
|
print(self.path)
|
|
self.path.parent.mkdir(parents=True, exist_ok=True)
|
|
with self.path.open("wt") as f:
|
|
_ = f.write(data)
|
|
|
|
|
|
# @final
|
|
# @dataclass(frozen=True, slots=True)
|
|
# class DataDirReplace(RecordCls[Path]):
|
|
# old = RecordName("data")
|
|
#
|
|
#
|
|
# @final
|
|
# @dataclass(frozen=True, slots=True)
|
|
# class DataDir:
|
|
# val: Path
|
|
# replace: DataDirReplace = field(init=False)
|
|
#
|
|
# def __post_init__(self) -> None:
|
|
# setter = super().__setattr__
|
|
# setter("replace", DataDirReplace(self.val))
|
|
#
|
|
# @classmethod
|
|
# def from_org(cls, org: OrgData) -> Self:
|
|
# cls(DATA_ROOT.joinpath(org.org.val, org.app.val))
|
|
@final
|
|
@dataclass(frozen=True, slots=True)
|
|
class DestPaths:
|
|
data_root = Record[RecordName, Path](RecordName("data_root"), DATA_ROOT)
|
|
data_path = Record[RecordName, str](
|
|
RecordName("data"),
|
|
sep.join((str(data_root.old), str(OrgVal.old), str(AppVal.old))),
|
|
)
|
|
data_dir: Path
|
|
env_file: Path = field(init=False)
|
|
compose_file: ComposeFileRendered = field(init=False)
|
|
|
|
def __post_init__(self) -> None:
|
|
setter = super(DestPaths, self).__setattr__
|
|
path_join = self.data_dir.joinpath
|
|
setter("env_file", path_join(".env"))
|
|
setter("compose_file", ComposeFileRendered(path_join("docker-compose.yml")))
|
|
|
|
@classmethod
|
|
def from_org(cls, org: OrgData) -> Self:
|
|
return cls.from_path(DATA_ROOT.joinpath(org.org.val, org.app.val))
|
|
|
|
@classmethod
|
|
def from_path(cls, path: Path) -> Self:
|
|
return cls(path)
|
|
|
|
# def mk_compose_dir(self) -> None:
|
|
# folder = self.data_dir
|
|
# if folder.exists():
|
|
# return
|
|
# folder.mkdir(parents=True)
|
|
|
|
@property
|
|
def pre_render_funcs(self) -> Iterator[Callable[[str], str]]:
|
|
yield self.data_path
|
|
|
|
@property
|
|
def render_funcs(self) -> Iterator[Callable[[str], str]]:
|
|
yield self.data_root
|