Skip to content

Commit

Permalink
Merge pull request #162 from jjochum/bugfix/properly-handle-typedvalu…
Browse files Browse the repository at this point in the history
…e-as-leaflist-val

Properly handle TypedValue as leaflist_val
  • Loading branch information
akarneliuk authored Nov 3, 2024
2 parents 18550b1 + 108e7a2 commit c7022ae
Showing 1 changed file with 47 additions and 6 deletions.
53 changes: 47 additions & 6 deletions pygnmi/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,27 @@ def get(
elif update_msg.val.HasField("proto_bytes"):
update_container.update({"val": update_msg.val.proto_bytes})

elif update_msg.val.HasField("leaflist_val"):
val_leaflist = update_msg.val
element_val = None
if all([isinstance(e, TypedValue) for e in val_leaflist.leaflist_val.element]):
element_val = {}
for e in val_leaflist.leaflist_val.element:
if hasattr(e, "json_val"):
element_val.update(json.loads(e.json_val))
elif hasattr(e, "json_ietf_val"):
element_val.update(json.loads(e.json_ietf_val))
else:
raise TypeError(f"Neither json_val nor json_ietf_val found in element {e}.")
elif all([isinstance(e, str) for e in val_leaflist.leaflist_val.element]):
element_val = ""
for e in val_leaflist.leaflist_val.element:
element_val += e
else:
raise Exception("leaflist elements have differing types. Only str and TypedValue are supported.")

update_container.update({"val": element_val})

notification_container["update"].append(update_container)

response["notification"].append(notification_container)
Expand Down Expand Up @@ -1109,6 +1130,12 @@ def enqueue_updates():
self._updates.put(update)
except Exception as error:
self.error = error

# The connection was terminated by the server. This is generally okay and
# shouldn't raise an exception.
if isinstance(error, grpc._channel._MultiThreadedRendezvous) and error.code() == grpc.StatusCode.CANCELLED:
return

raise error

self._subscribe_thread = threading.Thread(target=enqueue_updates)
Expand Down Expand Up @@ -1378,10 +1405,24 @@ def telemetryParser(in_message=None, debug: bool = False):

elif update_msg.val.HasField("leaflist_val"):
val_leaflist = update_msg.val
element_str = ""
for element in val_leaflist.leaflist_val.element:
element_str += element
update_container.update({"val": element_str})
element_val = None
if all([isinstance(e, TypedValue) for e in val_leaflist.leaflist_val.element]):
element_val = {}
for e in val_leaflist.leaflist_val.element:
if hasattr(e, "json_val"):
element_val.update(json.loads(e.json_val))
elif hasattr(e, "json_ietf_val"):
element_val.update(json.loads(e.json_ietf_val))
else:
raise TypeError(f"Neither json_val nor json_ietf_val found in element {e}.")
elif all([isinstance(e, str) for e in val_leaflist.leaflist_val.element]):
element_val = ""
for e in val_leaflist.leaflist_val.element:
element_val += e
else:
raise Exception("leaflist elements have differing types. Only str and TypedValue are supported.")

update_container.update({"val": element_val})

response["update"]["update"].append(update_container)

Expand Down Expand Up @@ -1413,8 +1454,8 @@ def telemetryParser(in_message=None, debug: bool = False):

return response

except:
logger.error(f"Parsing of telemetry information is failed.")
except Exception as exc:
logger.error(f"Parsing of telemetry information is failed: {exc}")

return None

Expand Down

0 comments on commit c7022ae

Please sign in to comment.