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

Commit

Permalink
cloud list: use jinja2 (#2392)
Browse files Browse the repository at this point in the history
  • Loading branch information
Roman Inflianskas authored Dec 22, 2023
1 parent 8a45f82 commit 03ead82
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 45 deletions.
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

0 comments on commit 03ead82

Please sign in to comment.