Skip to content

Commit

Permalink
more items
Browse files Browse the repository at this point in the history
  • Loading branch information
pcrespov committed Nov 20, 2024
1 parent 2a9e1df commit 1fa79af
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 43 deletions.
106 changes: 77 additions & 29 deletions scripts/echo_services_markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
./scripts/echo_services_markdown.py >services.md
"""

import itertools
import sys
from collections.abc import Iterable
from datetime import datetime
from pathlib import Path
from typing import Final
from typing import Final, NamedTuple

CURRENT_FILE = Path(sys.argv[0] if __name__ == "__main__" else __file__).resolve()
CURRENT_DIR = CURRENT_FILE.parent
Expand All @@ -18,46 +19,82 @@
str
] = "https://raw.githubusercontent.com/ITISFoundation/osparc-simcore/refs/heads/master"

_REDOC_URL_PREFIX: Final[str] = f"https://redocly.github.io/redoc/?url={_URL_PREFIX}"
_SWAGGER_URL_PREFIX: Final[str] = f"https://petstore.swagger.io/?url={_URL_PREFIX}"

_DOCKER_IMAGE_SIZE: Final[
str
] = "[![Docker Image Size](https://img.shields.io/docker/image-size/itisfoundation/{0})](https://hub.docker.com/r/itisfoundation/{0}/tags)"

# SEE https://shields.io/badges


def to_row(values: Iterable):
def _to_row(values: Iterable):
return f"| {'|'.join(map(str, values))} |\n"


def generate_markdown_table(openapi_files: list[tuple[Path, Path]]) -> str:
class CaptureTuple(NamedTuple):
service_name: str
file_path: Path


def _by_service_name(e: CaptureTuple):
return e.service_name


service_names_aliases: dict[str, str] = {"web": "webserver"}


def generate_markdown_table(
*catpured_files: Iterable[CaptureTuple],
) -> str:
title = ("service", "rest API", " ", "docker")
lines = ["-" * 10] * len(title)

table = to_row(title)
table += to_row(lines)
table = _to_row(title)
table += _to_row(lines)

aliases: dict[str, str] = {"web": "webserver"}
found = itertools.groupby(
sorted(itertools.chain(*catpured_files), key=_by_service_name),
key=_by_service_name,
)

for service_name, file_path in openapi_files:
redoc_url = f"https://redocly.github.io/redoc/?url={_URL_PREFIX}/{file_path}"
swagger_url = f"https://petstore.swagger.io/?url={_URL_PREFIX}/{file_path}"
# SEE https://shields.io/badges
openapi_badges = "".join(
for name, service_files in found:
table += _to_row(
(
f"[![ReDoc](https://img.shields.io/badge/OpenAPI-ReDoc-85ea2d?logo=openapiinitiative)]({redoc_url}) "
f"[![Swagger UI](https://img.shields.io/badge/OpenAPI-Swagger_UI-85ea2d?logo=swagger)]({swagger_url})",
f"**{name.upper()}**",
"",
"",
"",
)
)
docker_badges = "".join(
(_DOCKER_IMAGE_SIZE.format(aliases.get(f"{service_name}") or service_name),)
)
table += to_row(
(
service_name,
f"[{file_path}](./{file_path})",
openapi_badges,
docker_badges,
for _, file_path in service_files:
linked_path = f"[{file_path}](./{file_path})"

# SEE https://shields.io/badges
docker_badges = []
openapi_badges = []

if file_path.stem.lower() == "dockerfile":
repo = service_names_aliases.get(f"{name}") or name
docker_badges = [
f"[![Docker Image Size](https://img.shields.io/docker/image-size/itisfoundation/{repo})](https://hub.docker.com/r/itisfoundation/{repo}/tags)"
]

elif file_path.stem.lower() == "openapi":
openapi_badges = [
f"[![ReDoc](https://img.shields.io/badge/OpenAPI-ReDoc-85ea2d?logo=openapiinitiative)]({_REDOC_URL_PREFIX}/{file_path}) "
f"[![Swagger UI](https://img.shields.io/badge/OpenAPI-Swagger_UI-85ea2d?logo=swagger)]({_SWAGGER_URL_PREFIX}/{file_path})",
]

table += _to_row(
(
"",
linked_path,
" ".join(openapi_badges),
" ".join(docker_badges),
)
)
)

table += _to_row(["" * 10] * len(title))

return table


Expand All @@ -66,13 +103,24 @@ def generate_markdown_table(openapi_files: list[tuple[Path, Path]]) -> str:
repo_base_path = CURRENT_DIR.parent.resolve()
services_path = repo_base_path / "services"

openapi_files_found = sorted(
(file.relative_to(services_path).parents[-2], file.relative_to(repo_base_path))
def _to_tuple(file: Path):
return CaptureTuple(
f"{file.relative_to(services_path).parents[-2]}",
file.relative_to(repo_base_path),
)

dockerfiles_found = (_to_tuple(file) for file in services_path.rglob("Dockerfile"))

openapi_files_found = (
_to_tuple(file)
for file in services_path.rglob("openapi.*")
if file.suffix in {".json", ".yaml", ".yml"}
)

markdown_table = generate_markdown_table(openapi_files_found)
markdown_table = generate_markdown_table(
openapi_files_found,
dockerfiles_found,
)
now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")

print(
Expand Down
Loading

0 comments on commit 1fa79af

Please sign in to comment.