Skip to content

Commit c4dfd73

Browse files
benjaminleightonjeremyh
authored andcommitted
Ability to group products by regex name on match
1 parent 4c4168c commit c4dfd73

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,12 @@ You can alter default [Flask](http://flask.pocoo.org/docs/1.0/config/) or
129129
# Default product to display (picks first available)
130130
CUBEDASH_DEFAULT_PRODUCTS = ('ls8_nbar_albers', 'ls7_nbar_albers')
131131

132+
# Limited regex syntax and group name pairs. Within pairs regex and group name seperated by
133+
# pairs seperated by ; regex matches on product name
134+
# eg ".*albers.*,Albers projection;.*level1.*,Level 1 products" defaults to None
135+
# items are only categorized into the first group matched even if subsequent regexes match
136+
# if defined overrules CUBEDASH_PRODUCT_GROUP_BY_FIELD
137+
CUBEDASH_PRODUCT_GROUP_BY_REGEX = ".*albers.*,Albers projection;.*level1.*,Level 1 products"
132138
# Which field should we use when grouping products in the top menu?
133139
CUBEDASH_PRODUCT_GROUP_BY_FIELD = 'product_type'
134140
# Ungrouped products will be grouped together in this size.

cubedash/_pages.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import itertools
22
from datetime import datetime, timedelta
33
from typing import List, Tuple
4+
import re
45

56
import cubedash
67
import datacube
@@ -275,10 +276,30 @@ def _get_grouped_products() -> List[Tuple[str, List[ProductWithSummary]]]:
275276
# Which field should we use when grouping products in the top menu?
276277
group_by_field = app.config.get("CUBEDASH_PRODUCT_GROUP_BY_FIELD", "product_type")
277278
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
282303

283304
grouped_product_summarise = sorted(
284305
(

0 commit comments

Comments
 (0)