sync
This commit is contained in:
@@ -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,
|
||||
)
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
5
src/compose/template/util.py
Normal file
5
src/compose/template/util.py
Normal 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())
|
||||
@@ -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 ""
|
||||
|
||||
Reference in New Issue
Block a user