1
0
Fork 0
mirror of https://github.com/yavook/kiwi-scp.git synced 2024-11-24 13:43:01 +00:00

Service, Project, Instance classes

This commit is contained in:
Jörn-Michael Miehe 2021-10-22 17:50:26 +02:00
parent 1ef0303c41
commit cb464052b2
2 changed files with 104 additions and 0 deletions

72
kiwi_scp/instance.py Normal file
View file

@ -0,0 +1,72 @@
import functools
import re
from pathlib import Path
from typing import Iterable, List, Dict, Any
import attr
import yaml
from ._constants import COMPOSE_FILE_NAME
from .config import Config
_RE_CONFDIR = re.compile(r"^\s*\$(?:CONFDIR|{CONFDIR})/+(.*)$", flags=re.UNICODE)
@attr.s
class Service:
name: str = attr.ib()
configs: List[Path] = attr.ib()
@classmethod
def from_description(cls, name: str, description: Dict[str, Any]):
configs: List[Path] = []
if "volumes" in description:
volumes: List[str] = description["volumes"]
for volume in volumes:
host_part = volume.split(":")[0]
confdir = _RE_CONFDIR.match(host_part)
if confdir:
configs.append(Path(confdir.group(1)))
return cls(
name=name,
configs=configs,
)
@attr.s
class Project:
directory: Path = attr.ib()
services: List[Service] = attr.ib()
@classmethod
@functools.lru_cache(maxsize=10)
def from_directory(cls, directory: Path):
with open(directory.joinpath(COMPOSE_FILE_NAME), "r") as cf:
yml = yaml.safe_load(cf)
return cls(
directory=directory,
services=[
Service.from_description(name, description)
for name, description in yml["services"].items()
],
)
@attr.s
class Instance:
directory: Path = attr.ib(default=Path('.'))
@property
def config(self) -> Config:
"""shorthand: get the current configuration"""
return Config.from_instance(self.directory)
@property
def projects(self) -> Iterable[Project]:
return []

32
tests/test_project.py Normal file
View file

@ -0,0 +1,32 @@
from pathlib import Path
from kiwi_scp.instance import Project
def test_example():
p = Project.from_directory(Path("example/hello-world.project"))
assert p.directory == Path("example/hello-world.project")
assert len(p.services) == 5
s = p.services[0]
assert s.name == "greeter"
assert len(s.configs) == 0
s = p.services[1]
assert s.name == "web"
assert len(s.configs) == 0
s = p.services[2]
assert s.name == "db"
assert len(s.configs) == 0
s = p.services[3]
assert s.name == "adminer"
assert len(s.configs) == 0
s = p.services[4]
assert s.name == "another-web"
assert len(s.configs) == 1
assert s.configs[0] == Path("html/index.html")