This commit is contained in:
2025-12-15 23:53:10 -06:00
parent 9d3ce193e8
commit ca23b44a25
5 changed files with 58 additions and 32 deletions

View File

@@ -4,13 +4,14 @@ from compose.dest_path.factory import dest_paths_factory
from compose.rendered.entity import Rendered from compose.rendered.entity import Rendered
from compose.template.entity import Template from compose.template.entity import Template
from compose.template.get import template_get_proxy, template_get_vols from compose.template.get import template_get_proxy, template_get_vols
from compose.template.util import replace
def rendered_factory(template: Template) -> Rendered: def rendered_factory(template: Template) -> Rendered:
yml = template.compose.as_yaml() yml = template.compose.as_yaml()
if template.replace_args is not None: if template.replace_args is not None:
yml = reduce( yml = reduce(
lambda s, f: f.replace(s), lambda s, f: replace(f, s),
template.replace_args, template.replace_args,
yml, yml,
) )

View File

@@ -1,8 +1,16 @@
from collections.abc import Iterator
from dataclasses import dataclass from dataclasses import dataclass
from typing import final from typing import final
from compose.compose.entity import Compose, TraefikCompose from compose.compose.entity import Compose, TraefikCompose
from compose.template.val_obj import DataDir, NameVal, OrgVal, RecordCls, Url from compose.template.val_obj import (
DataDir,
NameVal,
OrgVal,
RecordCls,
T_RecordCls,
Url,
)
@final @final
@@ -14,7 +22,7 @@ class ReplaceArgs:
data: RecordCls[DataDir] data: RecordCls[DataDir]
url: RecordCls[Url] url: RecordCls[Url]
def __iter__(self): def __iter__(self) -> Iterator[T_RecordCls]:
yield self.org yield self.org
yield self.name yield self.name
yield self.org_name yield self.org_name

View File

@@ -1,8 +1,10 @@
from collections.abc import Iterable from collections.abc import Iterable
from functools import partial
from pathlib import Path from pathlib import Path
from compose.net.entities import Net from compose.net.entities import Net
from compose.template.entity import Template from compose.template.entity import Template
from compose.template.util import replace
def template_get_vols(template: Template) -> Iterable[Path]: def template_get_vols(template: Template) -> Iterable[Path]:
@@ -13,7 +15,7 @@ def template_get_vols(template: Template) -> Iterable[Path]:
if not vols: if not vols:
return return
r_args = template.replace_args r_args = template.replace_args
re = _lambda if r_args is None else r_args.data.replace re = _lambda if r_args is None else partial(replace, r_args.data)
for vol in vols: for vol in vols:
yield Path(re(vol)) yield Path(re(vol))
@@ -33,4 +35,4 @@ def template_get_proxy(template: Template) -> str | None:
r_args = template.replace_args r_args = template.replace_args
if r_args is None: if r_args is None:
return net return net
return r_args.name.replace(net) return replace(r_args.name, net)

View File

@@ -0,0 +1,5 @@
from compose.template.val_obj import T_RecordCls
def replace(record: T_RecordCls, string: str) -> str:
return str.replace(string, record.name, record.val.to_str())

View File

@@ -1,27 +1,40 @@
from abc import ABC, abstractmethod
from dataclasses import dataclass from dataclasses import dataclass
from pathlib import Path from pathlib import Path
from typing import TypeVar, final, override from typing import Protocol, final
from compose.util import get_replace_name from compose.util import get_replace_name
# class RecordVal(ABC):
class RecordVal(ABC): # @abstractmethod
@abstractmethod # def to_str(self) -> str:
def to_str(self) -> str: # pass
pass
TCo_RecordVal = TypeVar( @final
"TCo_RecordVal", class RecordVal(Protocol):
bound=RecordVal, def to_str(self) -> str: ...
covariant=True,
)
TCon_RecordVal = TypeVar( # TCo_RecordVal = TypeVar(
"TCon_RecordVal", # "TCo_RecordVal",
bound=RecordVal, # bound=RecordVal,
contravariant=True, # covariant=True,
) # )
# TCon_RecordVal = TypeVar(
# "TCon_RecordVal",
# bound=RecordVal,
# contravariant=True,
# )
@final
class T_RecordCls(Protocol):
# name: str
# val: RecordVal
@property
def name(self) -> str: ...
@property
def val(self) -> RecordVal: ...
@final @final
@@ -35,16 +48,16 @@ class RecordCls[T: RecordVal]:
# name:str # name:str
# val: RecordVal # val: RecordVal
def replace(self, string: str) -> str:
return str.replace(string, self.name, self.val.to_str()) # def replace(self:RecordCls[TCo_RecordVal], string: str) -> str:
# return str.replace(string, self.name, self.val.to_str())
@final @final
@dataclass(frozen=True, slots=True) @dataclass(frozen=True, slots=True)
class OrgVal(RecordVal): class OrgVal:
val: str | None val: str | None
@override
def to_str(self) -> str: def to_str(self) -> str:
if self.val is None: if self.val is None:
return "personal" return "personal"
@@ -56,30 +69,27 @@ class OrgVal(RecordVal):
@final @final
@dataclass(frozen=True, slots=True) @dataclass(frozen=True, slots=True)
class NameVal(RecordVal): class NameVal:
val: str val: str
@override
def to_str(self) -> str: def to_str(self) -> str:
return self.val return self.val
@final @final
@dataclass(frozen=True, slots=True) @dataclass(frozen=True, slots=True)
class DataDir(RecordVal): class DataDir:
path: Path path: Path
@override
def to_str(self) -> str: def to_str(self) -> str:
return str(self.path) return str(self.path)
@final @final
@dataclass(frozen=True, slots=True) @dataclass(frozen=True, slots=True)
class Url(RecordVal): class Url:
sub_url: str | None sub_url: str | None
@override
def to_str(self) -> str: def to_str(self) -> str:
if self.sub_url is None: if self.sub_url is None:
return "" return ""