diff --git a/compose_viz/compose.py b/compose_viz/compose.py index 6c65a8a..4cafca1 100644 --- a/compose_viz/compose.py +++ b/compose_viz/compose.py @@ -4,7 +4,11 @@ from compose_viz.service import Service class Compose: def __init__(self, services: List[Service]) -> None: - self.services = services + self._services = services def extract_networks(self) -> List[str]: raise NotImplementedError + + @property + def services(self): + return self._services diff --git a/compose_viz/parser.py b/compose_viz/parser.py index 1f44bbc..e035e45 100644 --- a/compose_viz/parser.py +++ b/compose_viz/parser.py @@ -1,5 +1,7 @@ +from re import S from compose_viz.compose import Compose - +from compose_viz.compose import Service +import yaml class Parser: def __init__(self): @@ -7,4 +9,36 @@ class Parser: def parse(self, file_path: str) -> Compose: # validate input file using `docker-compose config -q sys.argv[1]` first - raise NotImplementedError + # load the yaml file + with open(file_path, "r") as f: + try: + yaml_data = yaml.safe_load(f) + except yaml.YAMLError as exc: + raise yaml.YAMLError + # validate the yaml file + if not yaml_data: + print("Error: empty yaml file") + raise ValueError + if not yaml_data.get("services"): + print("Error: no services found") + raise ValueError + # parse services data into Service objects + services_data = yaml_data["services"] + services = [] + for service, service_name in zip(services_data.values(), services_data.keys()): + #print("name: {}".format(service_name)) + if service.get("image"): + service_image = service["image"] + #print("image: {}".format(service_image)) + if service.get("networks"): + service_networks = service["networks"] + #print("networks: {}".format(service_networks)) + services.append(Service( + name=service_name, + image=service_image, + networks=service_networks, + )) + # create Compose object + compose = Compose(services) + + return compose diff --git a/compose_viz/service.py b/compose_viz/service.py index 6e42483..5b809e3 100644 --- a/compose_viz/service.py +++ b/compose_viz/service.py @@ -3,11 +3,43 @@ from typing import List class Service: def __init__(self, name: str, image: str, ports: List[str] = [], networks: List[str] = [], volumes: List[str] = [], depends_on: List[str] = [], links: List[str] = [], extends: List[str] = []) -> None: - self.name = name - self.image = image - self.ports = ports - self.networks = networks - self.volumes = volumes - self.depends_on = depends_on - self.links = links - self.extends = extends + self._name = name + self._image = image + self._ports = ports + self._networks = networks + self._volumes = volumes + self._depends_on = depends_on + self._links = links + self._extends = extends + + @property + def name(self): + return self._name + + @property + def image(self): + return self._image + + @property + def ports(self): + return self._ports + + @property + def networks(self): + return self._networks + + @property + def volumes(self): + return self._volumes + + @property + def depends_on(self): + return self._depends_on + + @property + def links(self): + return self._links + + @property + def extends(self): + return self._extends