Skip to content

Commit

Permalink
revert code change
Browse files Browse the repository at this point in the history
  • Loading branch information
aquamatthias committed Nov 15, 2023
1 parent 60283bb commit 365b454
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
7 changes: 6 additions & 1 deletion resotocore/resotocore/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions resotocore/resotocore/cli/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
strip_quotes,
key_value_parser,
JsStream,
js_value_get,
)
from resotocore.cli.model import (
CLICommand,
Expand Down Expand Up @@ -156,7 +157,6 @@
restart_service,
combine_optional,
value_in_path,
value_in_path_get,
)
from resotocore.web.content_renderer import (
respond_ndjson,
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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, "<no id>")
r_name = value_in_path_get(elem, NodePath.reported_name, "<no name>")
r_kind = value_in_path_get(elem, NodePath.reported_kind, "<no kind>")
r_id = js_value_get(elem, NodePath.reported_id, "<no id>")
r_name = js_value_get(elem, NodePath.reported_name, "<no name>")
r_kind = js_value_get(elem, NodePath.reported_kind, "<no kind>")
log.info(f"Node id={r_id}, name={r_name}, kind={r_kind} marked for cleanup. {reason}. ({uid})")
yield elem

Expand Down

0 comments on commit 365b454

Please sign in to comment.