|
| 1 | +#!/usr/bin/env python3 |
1 | 2 | # ----------------------------------------------------------------------------- |
2 | 3 | # (C) Crown copyright Met Office. All rights reserved. |
3 | 4 | # The file LICENCE, distributed with this code, contains details of the terms |
@@ -144,21 +145,31 @@ def build_table(data: ProjectData, reviewer_list: list, repos: list) -> PrettyTa |
144 | 145 | return table |
145 | 146 |
|
146 | 147 |
|
147 | | -def print_table(title: str, table: PrettyTable, sortTotal: bool) -> None: |
| 148 | +def print_table( |
| 149 | + title: str, table: PrettyTable, sortTotal: bool, html_output: str = "" |
| 150 | +) -> None: |
148 | 151 | """ |
149 | 152 | Print a pretty table and its title. |
150 | 153 |
|
151 | 154 | title: str Title of table to be printed first |
152 | 155 | table: PrettyTable table to be printed |
153 | 156 | """ |
154 | | - print(title) |
155 | | - # table.set_style(TableStyle.MARKDOWN) #requires newer version |
| 157 | + |
156 | 158 | table.align["Reviewer"] = "l" |
157 | 159 |
|
158 | 160 | if sortTotal: |
159 | 161 | table.sortby = "Total" |
160 | 162 |
|
161 | | - print(table) |
| 163 | + if not html_output: |
| 164 | + print(title) |
| 165 | + print(table) |
| 166 | + return |
| 167 | + |
| 168 | + table.format = True |
| 169 | + html_table = table.get_html_string() |
| 170 | + with open(html_output, "a") as f: |
| 171 | + f.write(f"<h1>{title}</h1>") |
| 172 | + f.write(html_table) |
162 | 173 |
|
163 | 174 |
|
164 | 175 | def parse_args(): |
@@ -192,6 +203,12 @@ def parse_args(): |
192 | 203 | help="Filepath to test data for either capture the project status, " |
193 | 204 | "or use as input data.", |
194 | 205 | ) |
| 206 | + parser.add_argument( |
| 207 | + "--html", |
| 208 | + default="", |
| 209 | + help="html file to output table contents to. If not set, an ascii formatted " |
| 210 | + "table will be outputted to stdout", |
| 211 | + ) |
195 | 212 |
|
196 | 213 | args = parser.parse_args() |
197 | 214 |
|
@@ -236,8 +253,17 @@ def main(total: bool, test: bool, capture_project: bool, file: Path): |
236 | 253 | tables["LFRic"] = build_table(data, reviewers, repo_list) |
237 | 254 |
|
238 | 255 | # Print tables |
| 256 | + # Check html path is valid |
| 257 | + if args.html: |
| 258 | + html_path = Path(args.html) |
| 259 | + if html_path.is_dir(): |
| 260 | + raise ValueError("--html option cannot be a directory") |
| 261 | + html_dir = html_path.parent |
| 262 | + html_dir.mkdir(parents=True, exist_ok=True) |
| 263 | + html_path.unlink(missing_ok=True) |
| 264 | + |
239 | 265 | for name, table in tables.items(): |
240 | | - print_table(name, table, total) |
| 266 | + print_table(name, table, total, args.html) |
241 | 267 |
|
242 | 268 |
|
243 | 269 | if __name__ == "__main__": |
|
0 commit comments