From 3740b6f011e31c7369c76a5445e67de3c05f9bf6 Mon Sep 17 00:00:00 2001 From: Xyphuz Date: Thu, 26 May 2022 11:06:06 +0800 Subject: [PATCH 1/3] feat: new supported output formats --- README.md | 20 +-- compose_viz/__init__.py | 2 +- compose_viz/cli.py | 22 ++- compose_viz/graph.py | 2 +- compose_viz/viz_formats.py | 45 ++++++ examples/voting-app/compose-viz.svg | 210 ++++++++++++++++++++++++++++ pyproject.toml | 4 +- 7 files changed, 277 insertions(+), 28 deletions(-) create mode 100644 compose_viz/viz_formats.py create mode 100644 examples/voting-app/compose-viz.svg diff --git a/README.md b/README.md index 508f715..20f913a 100644 --- a/README.md +++ b/README.md @@ -56,7 +56,7 @@ ## About The Project -`compose-viz` is a compose file visualization tool that supports [compose-spec](https://github.com/compose-spec/compose-spec/blob/master/spec.md) and allows you to gernerate graph in [DOT](https://graphviz.org/doc/info/lang.html) format or `.png`. +`compose-viz` is a compose file visualization tool that supports [compose-spec](https://github.com/compose-spec/compose-spec/blob/master/spec.md) and allows you to gernerate graph in several formats. If you are looking for a compose file vizualization tool, and you are using one of the [compose-spec](https://github.com/compose-spec/compose-spec/blob/master/spec.md) implementations (e.g. [docker-compose](https://github.com/docker/compose)/[podman-compose](https://github.com/containers/podman-compose)), then `compose-viz` is a great choice for you. @@ -70,7 +70,7 @@ If you are looking for a compose file vizualization tool, and you are using one #### Graphviz -If you want to generate PNG (which is the default option), you need to install [Graphviz](https://graphviz.org/download/). +You need to install [Graphviz](https://graphviz.org/download/) to generate graphs. ### Installation @@ -93,9 +93,9 @@ cpv docker-compose.yml And this is what the result looks like: -![compose-viz.png](https://github.com/compose-viz/compose-viz/blob/main/examples/voting-app/compose-viz.png) +![compose-viz.svg](https://github.com/compose-viz/compose-viz/blob/main/examples/voting-app/compose-viz.svg) -Check out the result [here](https://github.com/compose-viz/compose-viz/blob/main/examples/voting-app/compose-viz.png). +Check out the result [here](https://github.com/compose-viz/compose-viz/blob/main/examples/voting-app). ### Usage @@ -103,12 +103,12 @@ Check out the result [here](https://github.com/compose-viz/compose-viz/blob/main ### Options -| Option | Description | -| ------------------------ | ------------------------------------------------------------------------------ | -| `-o, --output-path` | Output path for the generated visualization file. [default: ./compose-viz.png] | -| `-m, --format [PNG,DOT]` | Output format for the generated visualization file. [default: PNG] | -| `-v, --version` | Show the version of compose-viz. | -| `--help` | Show help and exit. | +| Option | Description | +| ------------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `-o, --output-filename` | Output filename for the generated visualization file. [default: compose-viz] | +| `-m, --format` | Output format for the generated visualization file. See [supported formats](https://github.com/compose-viz/compose-viz/blob/main/compose_viz/viz_formats.py). [default: png] | +| `-v, --version` | Show the version of compose-viz. | +| `--help` | Show help and exit. |

(back to top)

diff --git a/compose_viz/__init__.py b/compose_viz/__init__.py index 467be58..606db7b 100644 --- a/compose_viz/__init__.py +++ b/compose_viz/__init__.py @@ -1,2 +1,2 @@ __app_name__ = "compose_viz" -__version__ = "0.1.1" +__version__ = "0.1.2" diff --git a/compose_viz/cli.py b/compose_viz/cli.py index e028d70..ffb10bd 100644 --- a/compose_viz/cli.py +++ b/compose_viz/cli.py @@ -1,4 +1,3 @@ -from enum import Enum from typing import Optional import typer @@ -6,12 +5,7 @@ import typer from compose_viz import __app_name__, __version__ from compose_viz.graph import Graph from compose_viz.parser import Parser - - -class VisualizationFormats(str, Enum): - png = "PNG" - dot = "DOT" - +from compose_viz.viz_formats import VizFormats app = typer.Typer( invoke_without_command=True, @@ -30,14 +24,14 @@ def _version_callback(value: bool) -> None: @app.callback() def compose_viz( input_path: str, - output_path: str = typer.Option( - "./compose-viz.png", - "--output-path", + output_filename: str = typer.Option( + "compose-viz", + "--output-filename", "-o", - help="Output path for the generated visualization file.", + help="Output filename for the generated visualization file.", ), - format: VisualizationFormats = typer.Option( - "PNG", + format: VizFormats = typer.Option( + "png", "--format", "-m", help="Output format for the generated visualization file.", @@ -57,7 +51,7 @@ def compose_viz( if compose: typer.echo(f"Successfully parsed {input_path}") - Graph(compose, output_path).render(format) + Graph(compose, output_filename).render(format) raise typer.Exit() diff --git a/compose_viz/graph.py b/compose_viz/graph.py index 57e34f5..32c5569 100644 --- a/compose_viz/graph.py +++ b/compose_viz/graph.py @@ -93,4 +93,4 @@ class Graph: for depends_on in service.depends_on: self.add_edge(service.name, depends_on, "depends_on") - self.dot.render(outfile=self.filename, format=format, cleanup=cleanup) + self.dot.render(outfile=f"{self.filename}.{format}", format=format, cleanup=cleanup) diff --git a/compose_viz/viz_formats.py b/compose_viz/viz_formats.py new file mode 100644 index 0000000..e81b832 --- /dev/null +++ b/compose_viz/viz_formats.py @@ -0,0 +1,45 @@ +from enum import Enum + + +class VizFormats(str, Enum): + png = "png" + dot = "dot" + jpeg = "jpeg" + json = "json" + svg = "svg" + + bmp = "bmp" + canon = "canon" + cmap = "cmap" + cmapx = "cmapx" + cmapx_np = "cmapx_np" + dot_json = "dot_json" + emf = "emf" + emfplus = "emfplus" + eps = "eps" + fig = "fig" + gif = "gif" + gv = "gv" + imap = "imap" + imap_np = "imap_np" + ismap = "ismap" + jpe = "jpe" + jpg = "jpg" + json0 = "json0" + metafile = "metafile" + mp = "mp" + pdf = "pdf" + pic = "pic" + plain = "plain" + plain_ext = "plain-ext" + pov = "pov" + ps = "ps" + ps2 = "ps2" + tif = "tif" + tiff = "tiff" + tk = "tk" + vml = "vml" + xdot = "xdot" + xdot1_2 = "xdot1.2" + xdot1_4 = "xdot1.4" + xdot_json = "xdot_json" diff --git a/examples/voting-app/compose-viz.svg b/examples/voting-app/compose-viz.svg new file mode 100644 index 0000000..f16a8ee --- /dev/null +++ b/examples/voting-app/compose-viz.svg @@ -0,0 +1,210 @@ + + + + + + + + + +redis + + + +redis +(redis:alpine) + + + +frontend + +net:frontend + + + +redis->frontend + + + + + +0.0.0.06379 + +0.0.0.0:6379 + + + +0.0.0.06379->redis + + + +6379 + + + +db + + + +db +(postgres:9.4) + + + +backend + +net:backend + + + +db->backend + + + + + +db-data + +db-data + + + +db->db-data + + + +/var/lib/postgresql/data + + + +vote + + + +vote +(dockersamples/examplevotingapp_vote:before) + + + +vote->redis + + + + + +vote->frontend + + + + + +0.0.0.05000 + +0.0.0.0:5000 + + + +0.0.0.05000->vote + + + +80 + + + +result + + + +result +(dockersamples/examplevotingapp_result:before) + + + +result->db + + + + + +result->backend + + + + + +0.0.0.05001 + +0.0.0.0:5001 + + + +0.0.0.05001->result + + + +80 + + + +worker + + + +worker +(dockersamples/examplevotingapp_worker) + + + +worker->frontend + + + + + +worker->backend + + + + + +visualizer + + + +visualizer +(dockersamples/visualizer) + + + +/var/run/docker.sock + +/var/run/docker.sock + + + +visualizer->/var/run/docker.sock + + + +/var/run/docker.sock + + + +0.0.0.08080 + +0.0.0.0:8080 + + + +0.0.0.08080->visualizer + + + +8080 + + + diff --git a/pyproject.toml b/pyproject.toml index 43a2b36..4ba3c53 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,7 +1,7 @@ [tool.poetry] name = "compose-viz" -version = "0.1.1" -description = "A compose file visualization tool that supports compose-spec and allows you to gernerate graph in DOT format or PNG." +version = "0.1.2" +description = "A compose file visualization tool that supports compose-spec and allows you to gernerate graph in several formats." authors = ["Xyphuz Wu "] readme = "README.md" license = "MIT" From 329639e3ac4591628aeae8a5b56a5f0f3179d1b3 Mon Sep 17 00:00:00 2001 From: Xyphuz Date: Thu, 26 May 2022 11:08:20 +0800 Subject: [PATCH 2/3] chore: update example in README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 20f913a..6180550 100644 --- a/README.md +++ b/README.md @@ -88,7 +88,7 @@ This example yml is from [docker compose beginner tutorial](https://github.com/d ```bash cd examples/voting-app/ -cpv docker-compose.yml +cpv -m svg docker-compose.yml ``` And this is what the result looks like: From 910086507bbb7d666daba8462fc209e9cfa910da Mon Sep 17 00:00:00 2001 From: Xyphuz Date: Thu, 26 May 2022 11:10:19 +0800 Subject: [PATCH 3/3] fix: wrong expected value in tests --- tests/test_cli.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/tests/test_cli.py b/tests/test_cli.py index bfcdbcd..4e2f37e 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -24,11 +24,12 @@ runner = CliRunner() ) def test_cli(test_file_path: str) -> None: input_path = f"{test_file_path}" - output_path = "compose-viz-test.png" - result = runner.invoke(cli.app, ["-o", output_path, input_path]) + output_filename = "compose-viz-test" + default_format = "png" + result = runner.invoke(cli.app, ["-o", output_filename, input_path]) assert result.exit_code == 0 assert f"Successfully parsed {input_path}\n" in result.stdout - assert os.path.exists(output_path) + assert os.path.exists(f"{output_filename}.{default_format}") - os.remove(output_path) + os.remove(f"{output_filename}.{default_format}")