|
1 | 1 | import itertools
|
2 | 2 | from datetime import datetime, timedelta
|
3 | 3 | from typing import List, Tuple
|
| 4 | +import re |
4 | 5 |
|
5 | 6 | import cubedash
|
6 | 7 | import datacube
|
@@ -275,10 +276,30 @@ def _get_grouped_products() -> List[Tuple[str, List[ProductWithSummary]]]:
|
275 | 276 | # Which field should we use when grouping products in the top menu?
|
276 | 277 | group_by_field = app.config.get("CUBEDASH_PRODUCT_GROUP_BY_FIELD", "product_type")
|
277 | 278 | group_field_size = app.config.get("CUBEDASH_PRODUCT_GROUP_SIZE", 5)
|
278 |
| - |
279 |
| - # Group using the configured key, or fall back to the product name. |
280 |
| - def key(t): |
281 |
| - return t[0].fields.get(group_by_field) or t[0].name |
| 279 | + group_by_regex = app.config.get("CUBEDASH_PRODUCT_GROUP_BY_REGEX", None) |
| 280 | + |
| 281 | + if group_by_regex: |
| 282 | + grouped_summaries = {} |
| 283 | + grouped_product_summarise =[] |
| 284 | + group_regex = {} |
| 285 | + regex_group_pairs = group_by_regex.split(';') |
| 286 | + for regex_group_pair in regex_group_pairs: |
| 287 | + regex, group = regex_group_pair.split(',') |
| 288 | + group_regex[group] = re.compile(regex) |
| 289 | + grouped_summaries[group] = [] |
| 290 | + # group using regex |
| 291 | + def regex_key(t): |
| 292 | + for product_summary in product_summaries: |
| 293 | + for group, regex in group_regex.items(): |
| 294 | + if regex.match(t[0].name): |
| 295 | + return group |
| 296 | + return t[0].name |
| 297 | + key = regex_key |
| 298 | + else: |
| 299 | + # Group using the configured key, or fall back to the product name. |
| 300 | + def field_key(t): |
| 301 | + return t[0].fields.get(group_by_field) or t[0].name |
| 302 | + key = field_key |
282 | 303 |
|
283 | 304 | grouped_product_summarise = sorted(
|
284 | 305 | (
|
|
0 commit comments