5
5
import datetime
6
6
import logging
7
7
import shutil
8
+ import textwrap
8
9
from pathlib import Path
9
10
10
11
import plumbum
12
+ import tabulate
11
13
from dateutil import relativedelta
12
14
13
15
git = plumbum .local ["git" ]
@@ -48,7 +50,7 @@ def calculate_monthly_stat(
48
50
49
51
50
52
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 -->"
52
54
53
55
wiki_home_content = (THIS_DIR / "Home.md" ).read_text ()
54
56
@@ -58,25 +60,21 @@ def generate_home_wiki_page(wiki_dir: Path, repository: str) -> None:
58
60
]
59
61
wiki_home_content = wiki_home_content .format (REPOSITORY = repository )
60
62
61
- YEAR_TABLE_HEADER = """\
62
- ## {year}
63
-
64
- | Month | Builds | Images | Commits |
65
- | ---------------------- | ------ | ------ | ----------------------------------------------------------------------------------------------- |
66
- """
67
-
68
63
GITHUB_COMMITS_URL = (
69
64
f"[{{}}](https://github.com/{ repository } /commits/main/?since={{}}&until={{}})"
70
65
)
71
66
67
+ YEAR_TABLE_HEADERS = ["Month" , "Builds" , "Images" , "Commits" ]
68
+
72
69
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
+
74
74
year_builds , year_images , year_commits = 0 , 0 , 0
75
75
for year_month_file in sorted (year_dir .glob ("*.md" ), reverse = True ):
76
76
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 )
80
78
builds , images , commits = calculate_monthly_stat (
81
79
year_month_file , year_month_date
82
80
)
@@ -88,13 +86,21 @@ def generate_home_wiki_page(wiki_dir: Path, repository: str) -> None:
88
86
year_month_date ,
89
87
year_month_date + relativedelta .relativedelta (day = 31 ),
90
88
)
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
+
93
93
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 ]
95
98
)
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 "
98
104
99
105
(wiki_dir / "Home.md" ).write_text (wiki_home_content )
100
106
LOGGER .info ("Updated Home page" )
@@ -103,12 +109,14 @@ def generate_home_wiki_page(wiki_dir: Path, repository: str) -> None:
103
109
def update_monthly_wiki_page (
104
110
wiki_dir : Path , year_month : str , build_history_line : str
105
111
) -> 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 }
108
115
109
- | Date | Image | Links |
110
- | - | - | - |
111
- """
116
+ | Date | Image | Links |
117
+ | - | - | - |
118
+ """
119
+ )
112
120
year = year_month [:4 ]
113
121
monthly_page = wiki_dir / "monthly-files" / year / (year_month + ".md" )
114
122
if not monthly_page .exists ():
0 commit comments