From fe9a83a2eaf0c44e02fe6fe53bca6766edb4fe82 Mon Sep 17 00:00:00 2001 From: Tom Kralidis Date: Tue, 6 Aug 2024 22:22:20 -0400 Subject: [PATCH] add option to publish specific stations to a certain topic (#720) --- .../wis2box/metadata/station.py | 50 ++++++++++++++----- 1 file changed, 37 insertions(+), 13 deletions(-) diff --git a/wis2box-management/wis2box/metadata/station.py b/wis2box-management/wis2box/metadata/station.py index d1b729d78..685905103 100644 --- a/wis2box-management/wis2box/metadata/station.py +++ b/wis2box-management/wis2box/metadata/station.py @@ -251,7 +251,8 @@ def check_station_datasets(wigos_station_identifier: str) -> Iterator[dict]: yield topic -def add_topic_hierarchy(new_topic: str, territory_name: str, wsi: str) -> None: +def add_topic_hierarchy(new_topic: str, territory_name: str = None, + wsi: str = None) -> None: """ Adds topic hierarchy to topic @@ -264,9 +265,9 @@ def add_topic_hierarchy(new_topic: str, territory_name: str, wsi: str) -> None: valid_topics = [topic for link, topic in load_datasets()] if new_topic not in valid_topics: - msg = f'ERROR: Invalid topic: {new_topic}\n' - msg += 'Valid topics:\n' - msg += '\n'.join(valid_topics) + msg = (f'ERROR: Invalid topic: {new_topic}\n' + 'Valid topics:\n' + '\n'.join(valid_topics)) LOGGER.error(msg) return @@ -293,23 +294,36 @@ def add_topic_hierarchy(new_topic: str, territory_name: str, wsi: str) -> None: upsert_collection_item('stations', feature) -def publish_from_csv() -> None: +def publish_from_csv(path: Path = None, topic: str = None) -> None: """ Publishes station collection to API config and backend from csv + :param path: `Path` to station list CSV (default is `None`) + :param topic: `str` of topic hierarchy (default is `None`) + :returns: `None` """ - if not STATIONS.exists(): - msg = f'Please create a station metadata file in {STATION_METADATA}' - LOGGER.error(msg) - raise RuntimeError(msg) + stations_csv_to_publish = STATIONS + + if path is not None: + if not path.exists(): + msg = f'Station file {path} does not exist' + LOGGER.error(msg) + raise RuntimeError(msg) + + stations_csv_to_publish = path + else: + if not STATIONS.exists(): + msg = f'Please create a station metadata file in {STATION_METADATA}' # noqa + LOGGER.error(msg) + raise RuntimeError(msg) oscar_baseurl = 'https://oscar.wmo.int/surface/#/search/station/stationReportDetails' # noqa - LOGGER.debug(f'Publishing station list from {STATIONS}') + LOGGER.debug(f'Publishing station list from {stations_csv_to_publish}') station_list = [] - with STATIONS.open() as fh: + with stations_csv_to_publish.open() as fh: reader = csv.DictReader(fh) for row in reader: @@ -378,6 +392,13 @@ def publish_from_csv() -> None: LOGGER.debug(f'Station does not exist: {err}') upsert_collection_item('stations', feature) + if None not in [path, topic]: + msg = f"Adding topic {topic} to station {row['wigos_station_identifier']}" # noqa + LOGGER.debug(msg) + + add_topic_hierarchy(topic, row['territory_name'], + row['wigos_station_identifier']) + LOGGER.info(f'Updated station list: {station_list}') # inform mqtt-metrics-collector notify_msg = { @@ -495,11 +516,14 @@ def setup(ctx, verbosity): @click.command() @click.pass_context +@cli_helpers.OPTION_PATH +@cli_helpers.OPTION_TOPIC_HIERARCHY @cli_helpers.OPTION_VERBOSITY -def publish_collection(ctx, verbosity): +def publish_collection(ctx, path, topic, verbosity): """Publish from station_list.csv""" - publish_from_csv() + publish_from_csv(path, topic) + click.echo('Done')