Skip to content
This repository has been archived by the owner on Jan 29, 2024. It is now read-only.

cloud list: use jinja2 #2392

Merged
merged 1 commit into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ pygments==2.15.0
python-dotenv==0.21.0
algoliasearch==3.0.0
natsort==8.4.0
Jinja2==3.1.2
79 changes: 34 additions & 45 deletions scripts/aiven/clouds.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,31 @@
import argparse
import jinja2
import re
import requests
from dataclasses import dataclass
from typing import Dict, Self
from typing import cast, Self
from natsort import natsorted

CLOUD_ENTRIES_TEMPLATE = """\
{% set state = namespace(prev_cloud_vendor_code=None) %}
{%- for cloud_entry in cloud_entries -%}
{% if cloud_entry.vendor_code != state.prev_cloud_vendor_code %}
{% set state.prev_cloud_vendor_code = cloud_entry.vendor_code %}
{{ cloud_entry.vendor_name }}
-----------------------------------------------------
.. list-table::
:header-rows: 1

* - Region
- Cloud
- Description
{%- endif %}
* - {{ cloud_entry.geo_region }}
- ``{{ cloud_entry.name }}``
- {{ cloud_entry.description }}
{%- endfor -%}
"""


@dataclass
class CloudEntry:
Expand All @@ -24,32 +45,26 @@ def from_dict(cls: type[Self], cloud: dict[str, str | float], /) -> Self:

description_parts = [
description_part.strip()
for description_part in re.split(r"[,:-]", cloud["cloud_description"])
for description_part in re.split(
r"[,:-]", cast(str, cloud["cloud_description"])
)
]
vendor_name = description_parts.pop(2)
description = f"{description_parts[0]}, {description_parts[1]}: {description_parts[2]}"
cloud_name = cloud["cloud_name"]
vendor_code = cloud_name[0:cloud_name.index("-")]
description = (
f"{description_parts[0]}, {description_parts[1]}: {description_parts[2]}"
)
cloud_name = cast(str, cloud["cloud_name"])
vendor_code = cloud_name[0 : cloud_name.index("-")]
return cls(
description=description,
geo_region=cloud["geo_region"].title(), # Printing in title case to make it look better
geo_region=cast(
str, cloud["geo_region"]
).title(), # Printing in title case to make it look better
name=cloud_name,
vendor_code=vendor_code,
vendor_name=vendor_name,
)

def to_str(self) -> str:
"""Creates cloud entry with formatted info.

:returns: formatted string with cloud info
:rtype: str
"""
result = ""
result += f" * - {self.geo_region}\n"
result += f" - ``{self.name}``\n"
result += f" - {self.description}"
return result


def main():
parser = argparse.ArgumentParser(description="List available cloud regions.")
Expand All @@ -68,33 +83,7 @@ def main():
key=lambda cloud: (cloud.vendor_code, cloud.geo_region, cloud.name),
)

# This helps creating a new section every time there is a change in the Cloud vendor
prev_cloud_vendor_code = None
res = ""
for cloud_entry in cloud_entries:
res += "\n"
# If current_cloud is different than the previous cloud, let's create a new title, section, table
if cloud_entry.vendor_code != prev_cloud_vendor_code:
prev_cloud_vendor_code = cloud_entry.vendor_code
res += "\n"
res += cloud_entry.vendor_name
res += "\n"
res += "-----------------------------------------------------"
res += "\n"

res += ".. list-table::"
res += "\n"
res += " :header-rows: 1"
res += "\n\n"

res += " * - Region"
res += "\n"
res += " - Cloud"
res += "\n"
res += " - Description"
res += "\n"

res += cloud_entry.to_str()
res = jinja2.Template(CLOUD_ENTRIES_TEMPLATE).render(cloud_entries=cloud_entries)

with open(filename, "w") as text_file:
text_file.write(res)
Expand Down
Loading