kiwi-scp/kiwi_scp/services.py

63 lines
1.4 KiB
Python
Raw Normal View History

2022-01-27 14:26:10 +00:00
from pathlib import Path
from typing import List, Generator, Optional, TYPE_CHECKING
2021-12-02 16:08:14 +00:00
import attr
2022-01-24 17:06:11 +00:00
from .yaml import YAML
2021-12-02 16:08:14 +00:00
2022-01-27 14:26:10 +00:00
if TYPE_CHECKING:
from .instance import Instance
from .service import Service
2021-12-02 16:08:14 +00:00
@attr.s
class Services:
2022-01-27 14:26:10 +00:00
content: List["Service"] = attr.ib()
2021-12-02 16:08:14 +00:00
def __str__(self) -> str:
return YAML().dump({
"services": {
service.name: service.content
for service in self.content
}
}).strip()
def __bool__(self) -> bool:
return bool(self.content)
2022-01-27 14:26:10 +00:00
@property
def parent_instance(self) -> Optional["Instance"]:
if not self:
return
return self.content[0].parent_instance
@property
def configs(self) -> Generator[Path, None, None]:
for service in self.content:
yield from service.configs
# def copy_configs(self) -> None:
# instance = self.parent_instance
#
# if instance is None:
# return
2022-01-24 17:07:19 +00:00
#
2022-01-27 14:26:10 +00:00
# print(list(self.configs))
2022-01-24 17:07:19 +00:00
#
# # Rootkit("rsync").
2021-12-02 16:08:14 +00:00
@property
def names(self) -> Generator[str, None, None]:
return (
service.name
for service in self.content
)
def filter_existing(self, service_names: List[str]) -> "Services":
return Services([
service
for service in self.content
if service.name in service_names
2022-01-24 17:06:11 +00:00
])