diff --git a/kiwi_scp/commands/cli.py b/kiwi_scp/commands/cli.py index 01e2087..cb538bc 100644 --- a/kiwi_scp/commands/cli.py +++ b/kiwi_scp/commands/cli.py @@ -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 diff --git a/kiwi_scp/instance.py b/kiwi_scp/instance.py index 8cf1f80..c19ad97 100644 --- a/kiwi_scp/instance.py +++ b/kiwi_scp/instance.py @@ -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( - directory=self.directory.joinpath(project.name), - parent=self, - ) + yield Project( + directory=self.directory.joinpath(project.name), + parent=self, + ) diff --git a/tests/test_instance.py b/tests/test_instance.py index 2f3f627..1a5beb3 100644 --- a/tests/test_instance.py +++ b/tests/test_instance.py @@ -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()