From 03ead824342b0f9c1cc9ee7969d055437bcfbed9 Mon Sep 17 00:00:00 2001 From: Roman Inflianskas Date: Fri, 22 Dec 2023 15:08:05 +0200 Subject: [PATCH] cloud list: use jinja2 (#2392) --- requirements.txt | 1 + scripts/aiven/clouds.py | 79 ++++++++++++++++++----------------------- 2 files changed, 35 insertions(+), 45 deletions(-) diff --git a/requirements.txt b/requirements.txt index e76a9b9e4f..b2428427d2 100644 --- a/requirements.txt +++ b/requirements.txt @@ -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 diff --git a/scripts/aiven/clouds.py b/scripts/aiven/clouds.py index 792f7940af..6a7a2a120a 100644 --- a/scripts/aiven/clouds.py +++ b/scripts/aiven/clouds.py @@ -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: @@ -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.") @@ -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)