From ca23b44a2523d8272d7b975798495e3154abcf3aba369cda2e6e4972da6bb1ba Mon Sep 17 00:00:00 2001 From: Christian Camper Date: Mon, 15 Dec 2025 23:53:10 -0600 Subject: [PATCH] sync --- src/compose/rendered/factory.py | 3 +- src/compose/template/entity.py | 12 +++++-- src/compose/template/get.py | 6 ++-- src/compose/template/util.py | 5 +++ src/compose/template/val_obj.py | 64 +++++++++++++++++++-------------- 5 files changed, 58 insertions(+), 32 deletions(-) create mode 100644 src/compose/template/util.py diff --git a/src/compose/rendered/factory.py b/src/compose/rendered/factory.py index e3f9ac0..d2f981b 100644 --- a/src/compose/rendered/factory.py +++ b/src/compose/rendered/factory.py @@ -4,13 +4,14 @@ from compose.dest_path.factory import dest_paths_factory from compose.rendered.entity import Rendered from compose.template.entity import Template from compose.template.get import template_get_proxy, template_get_vols +from compose.template.util import replace def rendered_factory(template: Template) -> Rendered: yml = template.compose.as_yaml() if template.replace_args is not None: yml = reduce( - lambda s, f: f.replace(s), + lambda s, f: replace(f, s), template.replace_args, yml, ) diff --git a/src/compose/template/entity.py b/src/compose/template/entity.py index f1b2e1b..c596cfb 100644 --- a/src/compose/template/entity.py +++ b/src/compose/template/entity.py @@ -1,8 +1,16 @@ +from collections.abc import Iterator from dataclasses import dataclass from typing import final 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 @@ -14,7 +22,7 @@ class ReplaceArgs: data: RecordCls[DataDir] url: RecordCls[Url] - def __iter__(self): + def __iter__(self) -> Iterator[T_RecordCls]: yield self.org yield self.name yield self.org_name diff --git a/src/compose/template/get.py b/src/compose/template/get.py index 239d046..f6ab888 100644 --- a/src/compose/template/get.py +++ b/src/compose/template/get.py @@ -1,8 +1,10 @@ from collections.abc import Iterable +from functools import partial from pathlib import Path from compose.net.entities import Net from compose.template.entity import Template +from compose.template.util import replace def template_get_vols(template: Template) -> Iterable[Path]: @@ -13,7 +15,7 @@ def template_get_vols(template: Template) -> Iterable[Path]: if not vols: return 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: yield Path(re(vol)) @@ -33,4 +35,4 @@ def template_get_proxy(template: Template) -> str | None: r_args = template.replace_args if r_args is None: return net - return r_args.name.replace(net) + return replace(r_args.name, net) diff --git a/src/compose/template/util.py b/src/compose/template/util.py new file mode 100644 index 0000000..d32269b --- /dev/null +++ b/src/compose/template/util.py @@ -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()) diff --git a/src/compose/template/val_obj.py b/src/compose/template/val_obj.py index b301609..6e4a35b 100644 --- a/src/compose/template/val_obj.py +++ b/src/compose/template/val_obj.py @@ -1,27 +1,40 @@ -from abc import ABC, abstractmethod from dataclasses import dataclass from pathlib import Path -from typing import TypeVar, final, override +from typing import Protocol, final from compose.util import get_replace_name - -class RecordVal(ABC): - @abstractmethod - def to_str(self) -> str: - pass +# class RecordVal(ABC): +# @abstractmethod +# def to_str(self) -> str: +# pass -TCo_RecordVal = TypeVar( - "TCo_RecordVal", - bound=RecordVal, - covariant=True, -) -TCon_RecordVal = TypeVar( - "TCon_RecordVal", - bound=RecordVal, - contravariant=True, -) +@final +class RecordVal(Protocol): + def to_str(self) -> str: ... + + +# TCo_RecordVal = TypeVar( +# "TCo_RecordVal", +# bound=RecordVal, +# 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 @@ -35,16 +48,16 @@ class RecordCls[T: RecordVal]: # name:str # 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 @dataclass(frozen=True, slots=True) -class OrgVal(RecordVal): +class OrgVal: val: str | None - @override def to_str(self) -> str: if self.val is None: return "personal" @@ -56,30 +69,27 @@ class OrgVal(RecordVal): @final @dataclass(frozen=True, slots=True) -class NameVal(RecordVal): +class NameVal: val: str - @override def to_str(self) -> str: return self.val @final @dataclass(frozen=True, slots=True) -class DataDir(RecordVal): +class DataDir: path: Path - @override def to_str(self) -> str: return str(self.path) @final @dataclass(frozen=True, slots=True) -class Url(RecordVal): +class Url: sub_url: str | None - @override def to_str(self) -> str: if self.sub_url is None: return ""