From 244f0626832053f62de4467122392127b7918b85 Mon Sep 17 00:00:00 2001 From: Maarten Sebregts Date: Wed, 21 May 2025 09:43:01 +0200 Subject: [PATCH] Warn only once for paths that do not exist in the target IDS per convert_ids call This prevents polluting the output of the CLI `imas convert` and explict `imas.convert_ids` calls with lots of repeated warnings. --- imas/ids_convert.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/imas/ids_convert.py b/imas/ids_convert.py index 295a87be..c4e752e0 100644 --- a/imas/ids_convert.py +++ b/imas/ids_convert.py @@ -27,6 +27,9 @@ from imas.ids_toplevel import IDSToplevel logger = logging.getLogger(__name__) +# Store for which paths we already emitted a warning that the target could not be found +# to prevent polluting the output with lots of repeated items. +_missing_paths_warning = set() def iter_parents(path: str) -> Iterator[str]: @@ -481,6 +484,8 @@ def convert_ids( source_version, target_version, ) + global _missing_paths_warning + _missing_paths_warning = set() # clear for which paths we emitted a warning source_tree = toplevel._parent._etree target_tree = target._parent._etree @@ -566,11 +571,14 @@ def _get_target_item( if path in rename_map: if rename_map.path[path] is None: if path not in rename_map.ignore_missing_paths: - if path in rename_map.type_change: - msg = "Element %r changed type in the target IDS." - else: - msg = "Element %r does not exist in the target IDS." - logger.warning(msg + " Data is not copied.", path) + # Only warn the first time that we encounter this path: + if path not in _missing_paths_warning: + if path in rename_map.type_change: + msg = "Element %r changed type in the target IDS." + else: + msg = "Element %r does not exist in the target IDS." + logger.warning(msg + " Data is not copied.", path) + _missing_paths_warning.add(path) return None else: return IDSPath(rename_map.path[path]).goto(target)