From 365b4544ac580da14531bf47d2b8981fc2ce80f4 Mon Sep 17 00:00:00 2001 From: Matthias Veit Date: Wed, 15 Nov 2023 12:01:45 +0100 Subject: [PATCH] revert code change --- resotocore/resotocore/cli/__init__.py | 7 ++++++- resotocore/resotocore/cli/command.py | 10 +++++----- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/resotocore/resotocore/cli/__init__.py b/resotocore/resotocore/cli/__init__.py index d7dd33a1f8..dd88a54a04 100644 --- a/resotocore/resotocore/cli/__init__.py +++ b/resotocore/resotocore/cli/__init__.py @@ -10,7 +10,7 @@ from resotocore.model.graph_access import Section from resotocore.types import JsonElement -from resotocore.util import utc, parse_utc +from resotocore.util import utc, parse_utc, AnyT from resotolib.durations import parse_duration, DurationRe from resotolib.parse_util import ( make_parser, @@ -156,6 +156,11 @@ def parse_time_or_delta(time_or_delta: str) -> datetime: return utc() - parse_duration(time_or_delta) if DurationRe.fullmatch(time_or_delta) else parse_utc(time_or_delta) +def js_value_get(element: JsonElement, path_or_name: Union[List[str], str], if_none: AnyT) -> AnyT: + result = js_value_at(element, path_or_name) + return result if result and isinstance(result, type(if_none)) else if_none # type: ignore + + def js_value_at(element: JsonElement, path_or_name: Union[List[str], str]) -> Optional[Any]: path = path_or_name if isinstance(path_or_name, list) else path_or_name.split(".") at = len(path) diff --git a/resotocore/resotocore/cli/command.py b/resotocore/resotocore/cli/command.py index 5a3659a2f1..636958a18f 100644 --- a/resotocore/resotocore/cli/command.py +++ b/resotocore/resotocore/cli/command.py @@ -76,6 +76,7 @@ strip_quotes, key_value_parser, JsStream, + js_value_get, ) from resotocore.cli.model import ( CLICommand, @@ -156,7 +157,6 @@ restart_service, combine_optional, value_in_path, - value_in_path_get, ) from resotocore.web.content_renderer import ( respond_ndjson, @@ -1331,7 +1331,7 @@ async def to_count(in_stream: AsyncIterator[JsonElement]) -> AsyncIterator[JsonE async with in_streamer.stream() as streamer: async for elem in streamer: name = js_value_at(elem, name_path) - count = value_in_path_get(elem, count_path, 0) + count = js_value_get(elem, count_path, 0) if name is None: null_value = count else: @@ -2086,9 +2086,9 @@ async def set_desired( reason = f"Reason: {strip_quotes(arg)}" if arg else "No reason provided." async for elem in super().set_desired(arg, graph_name, patch, items): uid = js_value_at(elem, NodePath.node_id) - r_id = value_in_path_get(elem, NodePath.reported_id, "") - r_name = value_in_path_get(elem, NodePath.reported_name, "") - r_kind = value_in_path_get(elem, NodePath.reported_kind, "") + r_id = js_value_get(elem, NodePath.reported_id, "") + r_name = js_value_get(elem, NodePath.reported_name, "") + r_kind = js_value_get(elem, NodePath.reported_kind, "") log.info(f"Node id={r_id}, name={r_name}, kind={r_kind} marked for cleanup. {reason}. ({uid})") yield elem