Commit 971c3eea authored by Donald Hunter's avatar Donald Hunter Committed by Jakub Kicinski

tools/net/ynl: Move formatted_string method out of NlAttr

The formatted_string() class method was in NlAttr so that it could be
accessed by NlAttr.as_struct(). Now that as_struct() has been removed,
move formatted_string() to YnlFamily as an internal helper method.
Signed-off-by: default avatarDonald Hunter <donald.hunter@gmail.com>
Reviewed-by: default avatarBreno Leitao <leitao@debian.org>
Reviewed-by: default avatarJiri Pirko <jiri@nvidia.com>
Link: https://lore.kernel.org/r/20240129223458.52046-10-donald.hunter@gmail.comSigned-off-by: default avatarJakub Kicinski <kuba@kernel.org>
parent 886365cf
...@@ -113,20 +113,6 @@ class NlAttr: ...@@ -113,20 +113,6 @@ class NlAttr:
else format.little else format.little
return format.native return format.native
@classmethod
def formatted_string(cls, raw, display_hint):
if display_hint == 'mac':
formatted = ':'.join('%02x' % b for b in raw)
elif display_hint == 'hex':
formatted = bytes.hex(raw, ' ')
elif display_hint in [ 'ipv4', 'ipv6' ]:
formatted = format(ipaddress.ip_address(raw))
elif display_hint == 'uuid':
formatted = str(uuid.UUID(bytes=raw))
else:
formatted = raw
return formatted
def as_scalar(self, attr_type, byte_order=None): def as_scalar(self, attr_type, byte_order=None):
format = self.get_format(attr_type, byte_order) format = self.get_format(attr_type, byte_order)
return format.unpack(self.raw)[0] return format.unpack(self.raw)[0]
...@@ -530,7 +516,7 @@ class YnlFamily(SpecFamily): ...@@ -530,7 +516,7 @@ class YnlFamily(SpecFamily):
else: else:
decoded = attr.as_bin() decoded = attr.as_bin()
if attr_spec.display_hint: if attr_spec.display_hint:
decoded = NlAttr.formatted_string(decoded, attr_spec.display_hint) decoded = self._formatted_string(decoded, attr_spec.display_hint)
return decoded return decoded
def _decode_array_nest(self, attr, attr_spec): def _decode_array_nest(self, attr, attr_spec):
...@@ -715,7 +701,7 @@ class YnlFamily(SpecFamily): ...@@ -715,7 +701,7 @@ class YnlFamily(SpecFamily):
if m.enum: if m.enum:
value = self._decode_enum(value, m) value = self._decode_enum(value, m)
elif m.display_hint: elif m.display_hint:
value = NlAttr.formatted_string(value, m.display_hint) value = self._formatted_string(value, m.display_hint)
attrs[m.name] = value attrs[m.name] = value
return attrs return attrs
...@@ -738,6 +724,19 @@ class YnlFamily(SpecFamily): ...@@ -738,6 +724,19 @@ class YnlFamily(SpecFamily):
attr_payload += format.pack(value) attr_payload += format.pack(value)
return attr_payload return attr_payload
def _formatted_string(self, raw, display_hint):
if display_hint == 'mac':
formatted = ':'.join('%02x' % b for b in raw)
elif display_hint == 'hex':
formatted = bytes.hex(raw, ' ')
elif display_hint in [ 'ipv4', 'ipv6' ]:
formatted = format(ipaddress.ip_address(raw))
elif display_hint == 'uuid':
formatted = str(uuid.UUID(bytes=raw))
else:
formatted = raw
return formatted
def handle_ntf(self, decoded): def handle_ntf(self, decoded):
msg = dict() msg = dict()
if self.include_raw: if self.include_raw:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment