Obsolete "Project" class
This commit is contained in:
parent
8260ddef3c
commit
0ecee93241
4 changed files with 29 additions and 64 deletions
|
@ -155,6 +155,15 @@ class KiwiConfig(BaseModel):
|
||||||
|
|
||||||
return cls()
|
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
|
@property
|
||||||
def kiwi_dict(self) -> Dict[str, Any]:
|
def kiwi_dict(self) -> Dict[str, Any]:
|
||||||
"""write this object as a dictionary of strings"""
|
"""write this object as a dictionary of strings"""
|
||||||
|
|
|
@ -5,10 +5,10 @@ from typing import List, Dict, Any, Generator
|
||||||
|
|
||||||
import attr
|
import attr
|
||||||
import click
|
import click
|
||||||
import ruamel.yaml
|
from ruamel.yaml import YAML
|
||||||
|
|
||||||
from ._constants import COMPOSE_FILE_NAME
|
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)
|
_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
|
@attr.s
|
||||||
class Instance:
|
class Instance:
|
||||||
directory: Path = attr.ib(default=Path('.'))
|
directory: Path = attr.ib(default=Path('.'))
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def config(self) -> Config:
|
def config(self) -> KiwiConfig:
|
||||||
"""shorthand: get the current configuration"""
|
"""shorthand: get the current configuration"""
|
||||||
|
|
||||||
return Config.from_directory(self.directory)
|
return KiwiConfig.from_directory(self.directory)
|
||||||
|
|
||||||
def get_project(self, name: str) -> Project:
|
@classmethod
|
||||||
return Project.from_directory(self.directory.joinpath(name))
|
@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 (
|
return (
|
||||||
self.get_project(project.name)
|
Service.from_description(name, description)
|
||||||
for project in self.config.projects
|
for name, description in yml["services"].items()
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -7,19 +7,18 @@ def test_example():
|
||||||
i = Instance(Path("example"))
|
i = Instance(Path("example"))
|
||||||
|
|
||||||
assert i.config is not None
|
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():
|
def test_empty():
|
||||||
i = Instance()
|
i = Instance()
|
||||||
|
|
||||||
assert i.directory == Path(".")
|
|
||||||
assert i.config is not None
|
assert i.config is not None
|
||||||
assert len(list(i.projects)) == 0
|
assert len(i.config.projects) == 0
|
||||||
|
|
||||||
|
|
||||||
def test_no_such_dir():
|
def test_no_such_dir():
|
||||||
|
@ -28,4 +27,4 @@ def test_no_such_dir():
|
||||||
|
|
||||||
assert i.directory == nonexistent_path
|
assert i.directory == nonexistent_path
|
||||||
assert i.config is not None
|
assert i.config is not None
|
||||||
assert len(list(i.projects)) == 0
|
assert len(i.config.projects) == 0
|
||||||
|
|
|
@ -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))
|
|
Loading…
Reference in a new issue