Instance.get_project() -> Instance.projects

This commit is contained in:
Jörn-Michael Miehe 2021-12-02 16:01:19 +01:00
parent d9f66f069c
commit 03c3c68fa9
3 changed files with 21 additions and 25 deletions

View file

@ -133,8 +133,9 @@ class KiwiCommand:
_logger.debug(f"{instance.directory!r}: {project_names!r}, {service_names!r}")
projects = [
instance.get_project(project_name)
for project_name in project_names
project
for project in instance.projects
if project.name in project_names
]
if not projects:
@ -175,12 +176,11 @@ class KiwiCommand:
@classmethod
def run_for_instance(cls, instance: Instance, **kwargs) -> None:
for project_config in instance.config.projects:
if cls.enabled_only and not project_config.enabled:
cls.print_header(f"Skipping disabled project {project_config.name}")
for project in instance.projects:
if cls.enabled_only and not project.config.enabled:
cls.print_header(f"Skipping disabled project {project.name}")
continue
project = instance.get_project(project_config.name)
cls.run_for_project(instance, project, **kwargs)
@classmethod

View file

@ -17,7 +17,7 @@ from .misc import YAML
class Service:
name: str = attr.ib()
content: CommentedMap = attr.ib()
parent: Optional["Project"] = attr.ib(default=None)
parent: "Project" = attr.ib()
_RE_CONFDIR = re.compile(r"^\s*\$(?:CONFDIR|{CONFDIR})/+(.*)$", flags=re.UNICODE)
@ -80,7 +80,7 @@ class Services:
@attr.s
class Project:
directory: Path = attr.ib()
parent: Optional["Instance"] = attr.ib(default=None)
parent: "Instance" = attr.ib()
@staticmethod
@functools.lru_cache(maxsize=10)
@ -93,7 +93,7 @@ class Project:
return self.directory.name
@property
def config(self) -> ProjectConfig:
def config(self) -> Optional[ProjectConfig]:
return self.parent.config.get_project_config(self.name)
@property
@ -125,12 +125,15 @@ class Project:
yml = Project._parse_compose_file(self.directory)
return Services([
Service(name, content, self)
for name, content in yml["services"].items()
Service(
name=name,
content=content,
parent=self,
) for name, content in yml["services"].items()
])
@attr.s(frozen=True)
@attr.s
class Instance:
directory: Path = attr.ib(default=Path('.'))
@ -140,11 +143,10 @@ class Instance:
return KiwiConfig.from_directory(self.directory)
@functools.lru_cache(maxsize=None)
def get_project(self, project_name: str) -> Optional[Project]:
@property
def projects(self) -> Generator[Project, None, None]:
for project in self.config.projects:
if project.name == project_name:
return Project(
yield Project(
directory=self.directory.joinpath(project.name),
parent=self,
)

View file

@ -14,12 +14,6 @@ class TestDefault:
assert pc.name == "hello-world.project"
p = i.get_project("hello-world.project")
assert p.directory == Path("example/hello-world.project")
assert i.get_project("nonexistent") is None
def test_empty(self):
i = Instance()