Skip to content

Commit 8f1b27d

Browse files
authored
Use tabulate in wiki to create better tables (#2241)
1 parent b603a70 commit 8f1b27d

File tree

1 file changed

+30
-22
lines changed

1 file changed

+30
-22
lines changed

wiki/update_wiki.py

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
import datetime
66
import logging
77
import shutil
8+
import textwrap
89
from pathlib import Path
910

1011
import plumbum
12+
import tabulate
1113
from dateutil import relativedelta
1214

1315
git = plumbum.local["git"]
@@ -48,7 +50,7 @@ def calculate_monthly_stat(
4850

4951

5052
def generate_home_wiki_page(wiki_dir: Path, repository: str) -> None:
51-
YEAR_MONTHLY_TABLES = "<!-- YEAR_MONTHLY_TABLES -->\n"
53+
YEAR_MONTHLY_TABLES = "<!-- YEAR_MONTHLY_TABLES -->"
5254

5355
wiki_home_content = (THIS_DIR / "Home.md").read_text()
5456

@@ -58,25 +60,21 @@ def generate_home_wiki_page(wiki_dir: Path, repository: str) -> None:
5860
]
5961
wiki_home_content = wiki_home_content.format(REPOSITORY=repository)
6062

61-
YEAR_TABLE_HEADER = """\
62-
## {year}
63-
64-
| Month | Builds | Images | Commits |
65-
| ---------------------- | ------ | ------ | ----------------------------------------------------------------------------------------------- |
66-
"""
67-
6863
GITHUB_COMMITS_URL = (
6964
f"[{{}}](https://github.com/{repository}/commits/main/?since={{}}&until={{}})"
7065
)
7166

67+
YEAR_TABLE_HEADERS = ["Month", "Builds", "Images", "Commits"]
68+
7269
for year_dir in sorted((wiki_dir / "monthly-files").glob("*"), reverse=True):
73-
wiki_home_content += "\n" + YEAR_TABLE_HEADER.format(year=year_dir.name)
70+
year = int(year_dir.name)
71+
wiki_home_content += f"\n\n## {year}\n\n"
72+
year_table_rows = []
73+
7474
year_builds, year_images, year_commits = 0, 0, 0
7575
for year_month_file in sorted(year_dir.glob("*.md"), reverse=True):
7676
year_month = year_month_file.stem
77-
year_month_date = datetime.date(
78-
year=int(year_month[:4]), month=int(year_month[5:]), day=1
79-
)
77+
year_month_date = datetime.date(year=year, month=int(year_month[5:]), day=1)
8078
builds, images, commits = calculate_monthly_stat(
8179
year_month_file, year_month_date
8280
)
@@ -88,13 +86,21 @@ def generate_home_wiki_page(wiki_dir: Path, repository: str) -> None:
8886
year_month_date,
8987
year_month_date + relativedelta.relativedelta(day=31),
9088
)
91-
monthly_line = f"| [`{year_month}`](./{year_month}) | {builds: <6} | {images: <6} | {commits_url: <95} |\n"
92-
wiki_home_content += monthly_line
89+
year_table_rows.append(
90+
[f"[`{year_month}`](./{year_month})", builds, images, commits_url]
91+
)
92+
9393
year_commits_url = GITHUB_COMMITS_URL.format(
94-
year_commits, f"{year_dir.name}-01-01", f"{year_dir.name}-12-31"
94+
year_commits, f"{year}-01-01", f"{year}-12-31"
95+
)
96+
year_table_rows.append(
97+
["**Total**", year_builds, year_images, year_commits_url]
9598
)
96-
year_total_line = f"| **Total** | {year_builds: <6} | {year_images: <6} | {year_commits_url: <95} |\n"
97-
wiki_home_content += year_total_line
99+
100+
wiki_home_content += tabulate.tabulate(
101+
year_table_rows, YEAR_TABLE_HEADERS, tablefmt="github"
102+
)
103+
wiki_home_content += "\n"
98104

99105
(wiki_dir / "Home.md").write_text(wiki_home_content)
100106
LOGGER.info("Updated Home page")
@@ -103,12 +109,14 @@ def generate_home_wiki_page(wiki_dir: Path, repository: str) -> None:
103109
def update_monthly_wiki_page(
104110
wiki_dir: Path, year_month: str, build_history_line: str
105111
) -> None:
106-
MONTHLY_PAGE_HEADER = f"""\
107-
# Images built during {year_month}
112+
MONTHLY_PAGE_HEADER = textwrap.dedent(
113+
f"""\
114+
# Images built during {year_month}
108115
109-
| Date | Image | Links |
110-
| - | - | - |
111-
"""
116+
| Date | Image | Links |
117+
| - | - | - |
118+
"""
119+
)
112120
year = year_month[:4]
113121
monthly_page = wiki_dir / "monthly-files" / year / (year_month + ".md")
114122
if not monthly_page.exists():

0 commit comments

Comments
 (0)