Skip to content

Commit

Permalink
add distributions-links after record prepared
Browse files Browse the repository at this point in the history
  • Loading branch information
maaikelimper committed Dec 2, 2024
1 parent 1efb9d3 commit 6fdabe2
Showing 1 changed file with 20 additions and 22 deletions.
42 changes: 20 additions & 22 deletions wis2box-management/wis2box/metadata/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,14 @@ def generate(self, mcf: dict) -> str:
LOGGER.debug('Adding data policy')
md['identification']['wmo_data_policy'] = mqtt_topic.split('/')[5]

# md set 'distribution' to empty object, we add links later
md['distribution'] = {}

LOGGER.debug('Generating OARec discovery metadata')
record = WMOWCMP2OutputSchema().write(md, stringify=False)
record['properties']['wmo:topicHierarchy'] = mqtt_topic
record['wis2box'] = mcf['wis2box']

LOGGER.debug('Adding distribution links')
distribution_links = self.get_distribution_links(
record,
format_='wcmp2')
# update links, do not extend or we get duplicates
record['links'] = distribution_links

if record['properties']['contacts'][0].get('organization') is None:
record['properties']['contacts'][0]['organization'] = record['properties']['contacts'][0].pop('name', "NOTSET") # noqa

Expand Down Expand Up @@ -128,12 +124,12 @@ def get_distribution_links(self,
:returns: `list` of distribution links
"""

LOGGER.debug('Adding distribution links')
LOGGER.info('Adding distribution links')

identifier = record['id']
topic = record['properties']['wmo:topicHierarchy']

oafeat_link = None
links = []
plugins = get_plugins(record)
# check if any plugin-names contains 2geojson
has_2geojson = any('2geojson' in plugin for plugin in plugins)
Expand All @@ -145,6 +141,7 @@ def get_distribution_links(self,
'description': f'Observations in json format for {identifier}',
'rel': 'collection'
}
links.append(oafeat_link)

mqp_link = {
'href': get_broker_public_endpoint(),
Expand All @@ -154,6 +151,7 @@ def get_distribution_links(self,
'rel': 'items',
'channel': topic
}
links.append(mqp_link)

canonical_link = {
'href': f"{API_URL}/collections/discovery-metadata/items/{identifier}", # noqa
Expand All @@ -162,12 +160,13 @@ def get_distribution_links(self,
'description': identifier,
'rel': 'canonical'
}
links.append(canonical_link)

if format_ == 'mcf':
for link in [oafeat_link, mqp_link, canonical_link]:
for link in links:
link['url'] = link.pop('href')

return oafeat_link, mqp_link, canonical_link
return links


def publish_broker_message(record: dict, storage_path: str,
Expand Down Expand Up @@ -246,22 +245,21 @@ def publish_discovery_metadata(metadata: Union[dict, str]):
LOGGER.info('Adding WCMP2 record from dictionary')
record = metadata
dm = DiscoveryMetadata()
distribution_links = dm.get_distribution_links(
record,
format_='wcmp2')
# update links, do not extend or we get duplicates
record['links'] = distribution_links
for link in record['links']:
if 'description' in link:
link['title'] = link.pop('description')
if 'url' in link:
link['href'] = link.pop('url')
else:
LOGGER.debug('Transforming MCF into WCMP2 record')
LOGGER.info('Transforming MCF into WCMP2 record')
dm = DiscoveryMetadata()
record_mcf = dm.parse_record(metadata)
record = dm.generate(record_mcf)

distribution_links = dm.get_distribution_links(record, format_='wcmp2')
# update links, do not extend or we get duplicates
record['links'] = distribution_links
for link in record['links']:
if 'description' in link:
link['title'] = link.pop('description')
if 'url' in link:
link['href'] = link.pop('url')

if 'x-wmo' in record['id']:
msg = 'Change x-wmo to wmo in metadata identifier'
LOGGER.error(msg)
Expand Down

0 comments on commit 6fdabe2

Please sign in to comment.