Use tabulate in wiki to create better tables (#2241)

This commit is contained in:
Ayaz Salikhov
2025-02-24 09:25:27 +00:00
committed by GitHub
parent b603a70822
commit 8f1b27da65

View File

@@ -5,9 +5,11 @@ import argparse
import datetime import datetime
import logging import logging
import shutil import shutil
import textwrap
from pathlib import Path from pathlib import Path
import plumbum import plumbum
import tabulate
from dateutil import relativedelta from dateutil import relativedelta
git = plumbum.local["git"] git = plumbum.local["git"]
@@ -48,7 +50,7 @@ def calculate_monthly_stat(
def generate_home_wiki_page(wiki_dir: Path, repository: str) -> None: def generate_home_wiki_page(wiki_dir: Path, repository: str) -> None:
YEAR_MONTHLY_TABLES = "<!-- YEAR_MONTHLY_TABLES -->\n" YEAR_MONTHLY_TABLES = "<!-- YEAR_MONTHLY_TABLES -->"
wiki_home_content = (THIS_DIR / "Home.md").read_text() wiki_home_content = (THIS_DIR / "Home.md").read_text()
@@ -58,25 +60,21 @@ def generate_home_wiki_page(wiki_dir: Path, repository: str) -> None:
] ]
wiki_home_content = wiki_home_content.format(REPOSITORY=repository) wiki_home_content = wiki_home_content.format(REPOSITORY=repository)
YEAR_TABLE_HEADER = """\
## {year}
| Month | Builds | Images | Commits |
| ---------------------- | ------ | ------ | ----------------------------------------------------------------------------------------------- |
"""
GITHUB_COMMITS_URL = ( GITHUB_COMMITS_URL = (
f"[{{}}](https://github.com/{repository}/commits/main/?since={{}}&until={{}})" f"[{{}}](https://github.com/{repository}/commits/main/?since={{}}&until={{}})"
) )
YEAR_TABLE_HEADERS = ["Month", "Builds", "Images", "Commits"]
for year_dir in sorted((wiki_dir / "monthly-files").glob("*"), reverse=True): for year_dir in sorted((wiki_dir / "monthly-files").glob("*"), reverse=True):
wiki_home_content += "\n" + YEAR_TABLE_HEADER.format(year=year_dir.name) year = int(year_dir.name)
wiki_home_content += f"\n\n## {year}\n\n"
year_table_rows = []
year_builds, year_images, year_commits = 0, 0, 0 year_builds, year_images, year_commits = 0, 0, 0
for year_month_file in sorted(year_dir.glob("*.md"), reverse=True): for year_month_file in sorted(year_dir.glob("*.md"), reverse=True):
year_month = year_month_file.stem year_month = year_month_file.stem
year_month_date = datetime.date( year_month_date = datetime.date(year=year, month=int(year_month[5:]), day=1)
year=int(year_month[:4]), month=int(year_month[5:]), day=1
)
builds, images, commits = calculate_monthly_stat( builds, images, commits = calculate_monthly_stat(
year_month_file, year_month_date year_month_file, year_month_date
) )
@@ -88,13 +86,21 @@ def generate_home_wiki_page(wiki_dir: Path, repository: str) -> None:
year_month_date, year_month_date,
year_month_date + relativedelta.relativedelta(day=31), year_month_date + relativedelta.relativedelta(day=31),
) )
monthly_line = f"| [`{year_month}`](./{year_month}) | {builds: <6} | {images: <6} | {commits_url: <95} |\n" year_table_rows.append(
wiki_home_content += monthly_line [f"[`{year_month}`](./{year_month})", builds, images, commits_url]
)
year_commits_url = GITHUB_COMMITS_URL.format( year_commits_url = GITHUB_COMMITS_URL.format(
year_commits, f"{year_dir.name}-01-01", f"{year_dir.name}-12-31" year_commits, f"{year}-01-01", f"{year}-12-31"
) )
year_total_line = f"| **Total** | {year_builds: <6} | {year_images: <6} | {year_commits_url: <95} |\n" year_table_rows.append(
wiki_home_content += year_total_line ["**Total**", year_builds, year_images, year_commits_url]
)
wiki_home_content += tabulate.tabulate(
year_table_rows, YEAR_TABLE_HEADERS, tablefmt="github"
)
wiki_home_content += "\n"
(wiki_dir / "Home.md").write_text(wiki_home_content) (wiki_dir / "Home.md").write_text(wiki_home_content)
LOGGER.info("Updated Home page") LOGGER.info("Updated Home page")
@@ -103,12 +109,14 @@ def generate_home_wiki_page(wiki_dir: Path, repository: str) -> None:
def update_monthly_wiki_page( def update_monthly_wiki_page(
wiki_dir: Path, year_month: str, build_history_line: str wiki_dir: Path, year_month: str, build_history_line: str
) -> None: ) -> None:
MONTHLY_PAGE_HEADER = f"""\ MONTHLY_PAGE_HEADER = textwrap.dedent(
# Images built during {year_month} f"""\
# Images built during {year_month}
| Date | Image | Links | | Date | Image | Links |
| - | - | - | | - | - | - |
""" """
)
year = year_month[:4] year = year_month[:4]
monthly_page = wiki_dir / "monthly-files" / year / (year_month + ".md") monthly_page = wiki_dir / "monthly-files" / year / (year_month + ".md")
if not monthly_page.exists(): if not monthly_page.exists():