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

View file

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

View file

@ -14,12 +14,6 @@ class TestDefault:
assert pc.name == "hello-world.project" 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): def test_empty(self):
i = Instance() i = Instance()