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

Obsolete "Project" class

This commit is contained in:
Jörn-Michael Miehe 2021-10-26 15:59:38 +02:00
parent 8260ddef3c
commit 0ecee93241
4 changed files with 29 additions and 64 deletions

View file

@ -155,6 +155,15 @@ class KiwiConfig(BaseModel):
return cls()
def get_project_config(self, name: str) -> ProjectConfig:
"""returns the config of a project with a given name"""
for project in self.projects:
if project.name == name:
return project
raise ValueError("No Such Project")
@property
def kiwi_dict(self) -> Dict[str, Any]:
"""write this object as a dictionary of strings"""

View file

@ -5,10 +5,10 @@ from typing import List, Dict, Any, Generator
import attr
import click
import ruamel.yaml
from ruamel.yaml import YAML
from ._constants import COMPOSE_FILE_NAME
from .config import Config
from .config import KiwiConfig, ProjectConfig
_RE_CONFDIR = re.compile(r"^\s*\$(?:CONFDIR|{CONFDIR})/+(.*)$", flags=re.UNICODE)
@ -38,44 +38,29 @@ class Service:
)
@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 = ruamel.yaml.round_trip_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:
def config(self) -> KiwiConfig:
"""shorthand: get the current configuration"""
return Config.from_directory(self.directory)
return KiwiConfig.from_directory(self.directory)
def get_project(self, name: str) -> Project:
return Project.from_directory(self.directory.joinpath(name))
@classmethod
@functools.lru_cache(maxsize=10)
def _parse_compose_file(cls, directory: Path):
with open(directory.joinpath(COMPOSE_FILE_NAME), "r") as cf:
yml = YAML()
return yml.load(cf)
def get_services(self, project_name: str) -> Generator[Service, None, None]:
yml = Instance._parse_compose_file(self.directory.joinpath(project_name))
@property
def projects(self) -> Generator[Project, None, None]:
return (
self.get_project(project.name)
for project in self.config.projects
Service.from_description(name, description)
for name, description in yml["services"].items()
)

View file

@ -7,19 +7,18 @@ def test_example():
i = Instance(Path("example"))
assert i.config is not None
assert len(list(i.projects)) == 1
assert len(i.config.projects) == 1
p = next(i.projects)
p = i.config.projects[0]
assert p.directory == Path("example/hello-world.project")
assert p.name == "hello-world.project"
def test_empty():
i = Instance()
assert i.directory == Path(".")
assert i.config is not None
assert len(list(i.projects)) == 0
assert len(i.config.projects) == 0
def test_no_such_dir():
@ -28,4 +27,4 @@ def test_no_such_dir():
assert i.directory == nonexistent_path
assert i.config is not None
assert len(list(i.projects)) == 0
assert len(i.config.projects) == 0

View file

@ -1,28 +0,0 @@
from pathlib import Path
import pytest
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 p.services != []
def test_caching():
p = Project.from_directory(Path("example/hello-world.project"))
assert p is Project.from_directory(Path("example/hello-world.project"))
def test_no_such_dir():
nonexistent_path = Path("nonexistent")
with pytest.raises(FileNotFoundError) as exc_info:
Project.from_directory(nonexistent_path)
from kiwi_scp._constants import COMPOSE_FILE_NAME
assert exc_info.value.filename == str(nonexistent_path.joinpath(COMPOSE_FILE_NAME))