kiwi-scp/kiwi_scp/instance.py

48 lines
1.3 KiB
Python
Raw Normal View History

2021-10-22 15:50:26 +00:00
from pathlib import Path
2021-12-02 18:32:38 +00:00
from typing import Generator, Dict, Sequence
2021-10-22 15:50:26 +00:00
import attr
2022-01-20 09:20:57 +00:00
from ._constants import KIWI_CONF_NAME
2021-12-02 16:08:14 +00:00
from .config import KiwiConfig
from .project import Project
2021-11-03 15:32:01 +00:00
@attr.s
2021-11-03 15:32:01 +00:00
class Instance:
directory: Path = attr.ib(default=Path('.'))
@property
def config(self) -> KiwiConfig:
"""shorthand: get the current configuration"""
return KiwiConfig.from_directory(self.directory)
2022-01-20 09:20:57 +00:00
def save_config(self, config: KiwiConfig) -> None:
with open(self.directory.joinpath(KIWI_CONF_NAME), "w") as file:
config.dump_kiwi_yml(file)
@property
def projects(self) -> Generator[Project, None, None]:
2021-12-02 01:38:29 +00:00
for project in self.config.projects:
yield Project(
directory=self.directory.joinpath(project.name),
parent=self,
)
2021-12-02 18:32:38 +00:00
def get_projects(self, project_names: Sequence[str]) -> Dict[str, Project]:
existing_projects = {
project.name: project
for project in self.projects
if project.name in project_names
}
nonexistent_projects = {
name: None
for name in project_names
if name not in existing_projects
}
return {
**existing_projects,
**nonexistent_projects
}