From 6a8be74675254c5660b8c355356f15893b17b9d9 Mon Sep 17 00:00:00 2001 From: uccu Date: Sat, 21 May 2022 15:52:11 +0800 Subject: [PATCH 01/10] feat: add build from image path --- compose_viz/parser.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/compose_viz/parser.py b/compose_viz/parser.py index bfac019..d3a9afa 100644 --- a/compose_viz/parser.py +++ b/compose_viz/parser.py @@ -37,6 +37,9 @@ class Parser: if service.get("image"): service_image = service["image"] print("image: {}".format(service_image)) + elif service.get("build"): + service_image = "build from " + service["build"] + print("image: {}".format(service_image)) service_networks: List[str] = [] if service.get("networks"): From daaeba4ee07ae9aa7ebdd5a873faad8e27eb78f8 Mon Sep 17 00:00:00 2001 From: uccu Date: Sat, 21 May 2022 16:08:35 +0800 Subject: [PATCH 02/10] chore: implement service ports parse --- compose_viz/parser.py | 58 +++++++++++++++++++++++++++++-------------- 1 file changed, 39 insertions(+), 19 deletions(-) diff --git a/compose_viz/parser.py b/compose_viz/parser.py index d3a9afa..b46cb9f 100644 --- a/compose_viz/parser.py +++ b/compose_viz/parser.py @@ -18,28 +18,31 @@ class Parser: yaml_data = yaml.load(f) except Exception as e: raise RuntimeError(f"Error parsing file '{file_path}': {e}") - # validate the yaml file if not yaml_data: raise RuntimeError("Empty yaml file, aborting.") - if not yaml_data.get("services"): raise RuntimeError("No services found, aborting.") # parse services data into Service objects services_data = yaml_data["services"] + services = self.parse_service_data(yaml_data["services"]) + + # create Compose object + compose = Compose(services) + + return compose + + def parse_service_data(self, services_yaml_data: List): services = [] - - for service, service_name in zip(services_data.values(), services_data.keys()): - print("name: {}".format(service_name)) - + for service, service_name in zip(services_yaml_data.values(), services_yaml_data.keys()): + service_image: Optional[str] = None if service.get("image"): service_image = service["image"] - print("image: {}".format(service_image)) elif service.get("build"): service_image = "build from " + service["build"] - print("image: {}".format(service_image)) + service_networks: List[str] = [] if service.get("networks"): @@ -47,23 +50,40 @@ class Parser: service_networks = service["networks"] else: service_networks = list(service["networks"].keys()) - print("networks: {}".format(service_networks)) - - service_image: Optional[str] = None - if service.get("image"): - service_image = service["image"] - print("image: {}".format(service_image)) + service_extends: Optional[Extends] = None if service.get("extends"): service_extends = Extends(service_name=service["extends"]["service"]) - print("extends: {}".format(service_extends)) + + + service_ports: List[str] = [] + if service.get("ports"): + service_ports = service["ports"] + + service_depends_on: List[str] = [] + if service.get("depends_on"): + service_depends_on = service["depends_on"] services.append( - Service(name=service_name, image=service_image, networks=service_networks, extends=service_extends) + Service( + name=service_name, + image=service_image, + networks=service_networks, + extends=service_extends, + ports=service_ports, + depends_on=service_depends_on, + ) ) + # Service print debug + #print("--------------------") + #print("Service name: {}".format(service_name)) + #print("image: {}".format(service_image)) + #print("networks: {}".format(service_networks)) + #print("image: {}".format(service_image)) + #print("extends: {}".format(service_extends)) + #print("ports: {}".format(service_ports)) + #print("depends: {}".format(service_depends_on)) - # create Compose object - compose = Compose(services) - return compose + return services From b877dad590797233e66e9985342524d6afe56110 Mon Sep 17 00:00:00 2001 From: uccu Date: Sat, 21 May 2022 16:33:04 +0800 Subject: [PATCH 03/10] fix: fix 001110 test case error --- tests/test_parse_file.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_parse_file.py b/tests/test_parse_file.py index 751d267..8524e58 100644 --- a/tests/test_parse_file.py +++ b/tests/test_parse_file.py @@ -309,7 +309,7 @@ from compose_viz.volume import Volume, VolumeType ), Service( name="backend", - image="awesome/backend", + extends=Extends(service_name="frontend"), ports=["8000:5001"], ), ] From 78d7d3af28b04d0df1a16f5b83d71ae34b4ec2b2 Mon Sep 17 00:00:00 2001 From: uccu Date: Sat, 21 May 2022 18:07:30 +0800 Subject: [PATCH 04/10] chore: refactor parser parse --- compose_viz/parser.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/compose_viz/parser.py b/compose_viz/parser.py index b46cb9f..8a711cf 100644 --- a/compose_viz/parser.py +++ b/compose_viz/parser.py @@ -5,6 +5,15 @@ from ruamel.yaml import YAML from compose_viz.compose import Compose, Service from compose_viz.extends import Extends +class service_parse_rule: + def __init__( + self, + name: str, parse_path: List[str], + target: List[str] + ) -> None: + self.name = name + self.parse_path = parse_path + self.target = target class Parser: def __init__(self): From 2b22de5193ecbbb83e8fc742785103732f7e7800 Mon Sep 17 00:00:00 2001 From: uccu Date: Sat, 21 May 2022 18:17:37 +0800 Subject: [PATCH 05/10] chore: delete service_parse_rule class --- compose_viz/parser.py | 9 --------- 1 file changed, 9 deletions(-) diff --git a/compose_viz/parser.py b/compose_viz/parser.py index 8a711cf..b46cb9f 100644 --- a/compose_viz/parser.py +++ b/compose_viz/parser.py @@ -5,15 +5,6 @@ from ruamel.yaml import YAML from compose_viz.compose import Compose, Service from compose_viz.extends import Extends -class service_parse_rule: - def __init__( - self, - name: str, parse_path: List[str], - target: List[str] - ) -> None: - self.name = name - self.parse_path = parse_path - self.target = target class Parser: def __init__(self): From 114f778727b055febcc274bd34defc3e6595af91 Mon Sep 17 00:00:00 2001 From: uccu Date: Sat, 21 May 2022 21:57:18 +0800 Subject: [PATCH 06/10] feat: implment Parser parse service volumes --- compose_viz/parser.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/compose_viz/parser.py b/compose_viz/parser.py index b46cb9f..b9959fa 100644 --- a/compose_viz/parser.py +++ b/compose_viz/parser.py @@ -4,7 +4,7 @@ from ruamel.yaml import YAML from compose_viz.compose import Compose, Service from compose_viz.extends import Extends - +from compose_viz.volume import Volume, VolumeType class Parser: def __init__(self): @@ -65,6 +65,22 @@ class Parser: if service.get("depends_on"): service_depends_on = service["depends_on"] + service_volumes: List[Volume] = [] + if service.get("volumes"): + for volume_data in service["volumes"]: + if type(volume_data) is dict: + volume_source = volume_data["source"] + volume_target = volume_data["target"] + volume_type = VolumeType[volume_data["type"]] + service_volumes.append(Volume(source=volume_source, target=volume_target, type=volume_type)) + elif type(volume_data) is str: + spilt_data = volume_data.split(":",1) + volume_source = spilt_data[0] + volume_target = spilt_data[1] + service_volumes.append(Volume(source=volume_source, target=volume_target)) + + + services.append( Service( name=service_name, @@ -73,6 +89,7 @@ class Parser: extends=service_extends, ports=service_ports, depends_on=service_depends_on, + volumes=service_volumes, ) ) # Service print debug From 87422471be9fe76ba4b039aaff962d75d1270d51 Mon Sep 17 00:00:00 2001 From: uccu Date: Sat, 21 May 2022 22:00:38 +0800 Subject: [PATCH 07/10] feat: implment Parser parse link --- compose_viz/parser.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/compose_viz/parser.py b/compose_viz/parser.py index b9959fa..04a40be 100644 --- a/compose_viz/parser.py +++ b/compose_viz/parser.py @@ -79,7 +79,9 @@ class Parser: volume_target = spilt_data[1] service_volumes.append(Volume(source=volume_source, target=volume_target)) - + service_links: List[str] = [] + if service.get("links"): + service_links = service["links"] services.append( Service( @@ -90,6 +92,7 @@ class Parser: ports=service_ports, depends_on=service_depends_on, volumes=service_volumes, + links=service_links ) ) # Service print debug From 56d8c6aaa7e32630283b9300e542aca1d6a5f7d0 Mon Sep 17 00:00:00 2001 From: uccu Date: Sat, 21 May 2022 22:13:48 +0800 Subject: [PATCH 08/10] fix: fix 011100 test case error --- tests/test_parse_file.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/test_parse_file.py b/tests/test_parse_file.py index 8524e58..f9f63ad 100644 --- a/tests/test_parse_file.py +++ b/tests/test_parse_file.py @@ -670,10 +670,12 @@ from compose_viz.volume import Volume, VolumeType Service( name="frontend", image="awesome/webapp", + ports=["8000:5000"], ), Service( name="monitoring", image="awesome/monitoring", + ports=["8000:5010"], volumes=[ Volume(source="db-data", target="/data"), Volume( From 603b9cb299eede7b4ae056479fc3bef42eafa396 Mon Sep 17 00:00:00 2001 From: uccu Date: Sat, 21 May 2022 22:14:23 +0800 Subject: [PATCH 09/10] fix: fix 011111 test case error --- tests/test_parse_file.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_parse_file.py b/tests/test_parse_file.py index f9f63ad..c3305d5 100644 --- a/tests/test_parse_file.py +++ b/tests/test_parse_file.py @@ -781,7 +781,7 @@ from compose_viz.volume import Volume, VolumeType ), ], depends_on=["monitoring"], - extends=Extends(service_name="monitoring"), + extends=Extends(service_name="frontend"), ports=["8000:5010"], ), ] From a58e2c3af070262b9ff2d161e7137c1ed585cf21 Mon Sep 17 00:00:00 2001 From: Xyphuz Date: Sat, 21 May 2022 22:32:59 +0800 Subject: [PATCH 10/10] chore: apply pre-commit hooks --- compose_viz/parser.py | 34 +++++++++++++++------------------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/compose_viz/parser.py b/compose_viz/parser.py index 04a40be..2f74c93 100644 --- a/compose_viz/parser.py +++ b/compose_viz/parser.py @@ -1,4 +1,4 @@ -from typing import List, Optional +from typing import Dict, List, Optional from ruamel.yaml import YAML @@ -6,6 +6,7 @@ from compose_viz.compose import Compose, Service from compose_viz.extends import Extends from compose_viz.volume import Volume, VolumeType + class Parser: def __init__(self): pass @@ -25,7 +26,6 @@ class Parser: raise RuntimeError("No services found, aborting.") # parse services data into Service objects - services_data = yaml_data["services"] services = self.parse_service_data(yaml_data["services"]) # create Compose object @@ -33,16 +33,15 @@ class Parser: return compose - def parse_service_data(self, services_yaml_data: List): - services = [] + def parse_service_data(self, services_yaml_data: Dict[str, dict]) -> List[Service]: + services: List[Service] = [] for service, service_name in zip(services_yaml_data.values(), services_yaml_data.keys()): - + service_image: Optional[str] = None if service.get("image"): service_image = service["image"] elif service.get("build"): service_image = "build from " + service["build"] - service_networks: List[str] = [] if service.get("networks"): @@ -50,12 +49,10 @@ class Parser: service_networks = service["networks"] else: service_networks = list(service["networks"].keys()) - service_extends: Optional[Extends] = None if service.get("extends"): service_extends = Extends(service_name=service["extends"]["service"]) - service_ports: List[str] = [] if service.get("ports"): @@ -74,7 +71,7 @@ class Parser: volume_type = VolumeType[volume_data["type"]] service_volumes.append(Volume(source=volume_source, target=volume_target, type=volume_type)) elif type(volume_data) is str: - spilt_data = volume_data.split(":",1) + spilt_data = volume_data.split(":", 1) volume_source = spilt_data[0] volume_target = spilt_data[1] service_volumes.append(Volume(source=volume_source, target=volume_target)) @@ -92,18 +89,17 @@ class Parser: ports=service_ports, depends_on=service_depends_on, volumes=service_volumes, - links=service_links + links=service_links, ) ) # Service print debug - #print("--------------------") - #print("Service name: {}".format(service_name)) - #print("image: {}".format(service_image)) - #print("networks: {}".format(service_networks)) - #print("image: {}".format(service_image)) - #print("extends: {}".format(service_extends)) - #print("ports: {}".format(service_ports)) - #print("depends: {}".format(service_depends_on)) - + # print("--------------------") + # print("Service name: {}".format(service_name)) + # print("image: {}".format(service_image)) + # print("networks: {}".format(service_networks)) + # print("image: {}".format(service_image)) + # print("extends: {}".format(service_extends)) + # print("ports: {}".format(service_ports)) + # print("depends: {}".format(service_depends_on)) return services