From 6669c3b7efeec3edca9975d1658ab8e3dcffa0b2 Mon Sep 17 00:00:00 2001 From: uccu Date: Sat, 14 May 2022 20:26:32 +0800 Subject: [PATCH 1/2] feat: add parser --- compose_viz/parser.py | 1 + 1 file changed, 1 insertion(+) diff --git a/compose_viz/parser.py b/compose_viz/parser.py index 1f44bbc..de34128 100644 --- a/compose_viz/parser.py +++ b/compose_viz/parser.py @@ -7,4 +7,5 @@ class Parser: def parse(self, file_path: str) -> Compose: # validate input file using `docker-compose config -q sys.argv[1]` first + return Compose([]) raise NotImplementedError From 5df0ade777482b6d6f3dad87b5a0ac4a86a52c90 Mon Sep 17 00:00:00 2001 From: uccu Date: Sat, 14 May 2022 21:30:18 +0800 Subject: [PATCH 2/2] chore: implement Parser.parse --- compose_viz/parser.py | 39 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/compose_viz/parser.py b/compose_viz/parser.py index de34128..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,5 +9,36 @@ class Parser: def parse(self, file_path: str) -> Compose: # validate input file using `docker-compose config -q sys.argv[1]` first - return Compose([]) - 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