refactor PDU decoding as class-method
@@ -38,7 +38,16 @@ class PDUContent:
error_index: int = 0
-def decode_pdu_content(data: bytes, slc: slice = slice(None)) -> PDUContent:
+class PDU(Type[PDUContent]):
+ """
+ The superclass for SNMP Messages (GET, SET, GETNEXT, ...)
+ """
+
+ TYPECLASS = TypeClass.CONTEXT
+ TAG = 0
+
+ @classmethod
+ def decode_raw(cls, data: bytes, slc: slice = slice(None)) -> PDUContent:
"""
This method takes a :py:class:`bytes` object and converts it to
an application object. This is callable from each subclass of
@@ -68,7 +77,9 @@ def decode_pdu_content(data: bytes, slc: slice = slice(None)) -> PDUContent:
else:
# Offending OID is unknown
offending_oid = None
- exception = ErrorResponse.construct(error_status.value, offending_oid)
+ exception = ErrorResponse.construct(
+ error_status.value, offending_oid
+ )
raise exception
values, next_start = cast(
@@ -95,17 +106,6 @@ def decode_pdu_content(data: bytes, slc: slice = slice(None)) -> PDUContent:
request_id.value, varbinds, error_status.value, error_index.value
)
-
-class PDU(Type[PDUContent]):
- """
- The superclass for SNMP Messages (GET, SET, GETNEXT, ...)
- """
-
- TYPECLASS = TypeClass.CONTEXT
- TAG = 0
-
- decode_raw = staticmethod(decode_pdu_content)
-
def encode_raw(self) -> bytes:
wrapped_varbinds = [
@@ -231,7 +231,6 @@ class GetRequest(PDU):
"""
TAG = 0
- decode_raw = staticmethod(decode_pdu_content)
class GetResponse(PDU):
@@ -241,7 +240,6 @@ class GetResponse(PDU):
"""
TAG = 2
- decode_raw = staticmethod(decode_pdu_content) # type: ignore
class GetNextRequest(GetRequest):
