diff options
author | Ilya Etingof <etingof@gmail.com> | 2019-02-26 08:56:24 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-02-26 08:56:24 +0100 |
commit | 3f2f132a9fdf7a48ec6131d5498145dded3cfcad (patch) | |
tree | 63e6170b35f6b392bf2e3d3feb6996b886e4d36f /pysnmp/proto/api | |
parent | 2ad26f8bfef0e39b3789d9e6d4fcbf76820c9867 (diff) | |
download | pysnmp-git-3f2f132a9fdf7a48ec6131d5498145dded3cfcad.tar.gz |
PEP-8 long lines and dunders (#245)
This patch massively reformats the whole codebase mainly wrapping
long lines and eliminating dundered private attributes.
Diffstat (limited to 'pysnmp/proto/api')
-rw-r--r-- | pysnmp/proto/api/v1.py | 104 | ||||
-rw-r--r-- | pysnmp/proto/api/v2c.py | 43 | ||||
-rw-r--r-- | pysnmp/proto/api/verdec.py | 6 |
3 files changed, 114 insertions, 39 deletions
diff --git a/pysnmp/proto/api/v1.py b/pysnmp/proto/api/v1.py index 827a6a0d..f13920e7 100644 --- a/pysnmp/proto/api/v1.py +++ b/pysnmp/proto/api/v1.py @@ -39,10 +39,18 @@ class VarBindAPI(object): @staticmethod def setOIDVal(varBind, oidVal): oid, val = oidVal[0], oidVal[1] + varBind.setComponentByPosition(0, oid) + if val is None: val = null - varBind.setComponentByPosition(1).getComponentByPosition(1).setComponentByType(val.getTagSet(), val, verifyConstraints=False, matchTags=False, matchConstraints=False, innerFlag=True) + + (varBind.setComponentByPosition(1) + .getComponentByPosition(1) + .setComponentByType(val.getTagSet(), val, + verifyConstraints=False, matchTags=False, + matchConstraints=False, innerFlag=True)) + return varBind @staticmethod @@ -61,14 +69,17 @@ class PDUAPI(object): def setDefaults(self, pdu): pdu.setComponentByPosition( - 0, getNextRequestID(), verifyConstraints=False, matchTags=False, matchConstraints=False - ) + 0, getNextRequestID(), verifyConstraints=False, matchTags=False, + matchConstraints=False) + pdu.setComponentByPosition( - 1, self._errorStatus, verifyConstraints=False, matchTags=False, matchConstraints=False - ) + 1, self._errorStatus, verifyConstraints=False, matchTags=False, + matchConstraints=False) + pdu.setComponentByPosition( - 2, self._errorIndex, verifyConstraints=False, matchTags=False, matchConstraints=False - ) + 2, self._errorIndex, verifyConstraints=False, matchTags=False, + matchConstraints=False) + pdu.setComponentByPosition(3) @staticmethod @@ -90,12 +101,14 @@ class PDUAPI(object): @staticmethod def getErrorIndex(pdu, muteErrors=False): errorIndex = pdu.getComponentByPosition(2) + if errorIndex > len(pdu[3]): if muteErrors: return errorIndex.clone(len(pdu[3])) + raise error.ProtocolError( - 'Error index out of range: %s > %s' % (errorIndex, len(pdu[3])) - ) + 'Error index out of range: %s > %s' % (errorIndex, len(pdu[3]))) + return errorIndex @staticmethod @@ -119,20 +132,23 @@ class PDUAPI(object): @staticmethod def getVarBinds(pdu): - return [apiVarBind.getOIDVal(varBind) for varBind in pdu.getComponentByPosition(3)] + return [apiVarBind.getOIDVal(varBind) + for varBind in pdu.getComponentByPosition(3)] @staticmethod def setVarBinds(pdu, varBinds): varBindList = pdu.setComponentByPosition(3).getComponentByPosition(3) + varBindList.clear() + for idx, varBind in enumerate(varBinds): if isinstance(varBind, VarBind): varBindList.setComponentByPosition(idx, varBind) + else: varBindList.setComponentByPosition(idx) apiVarBind.setOIDVal( - varBindList.getComponentByPosition(idx), varBind - ) + varBindList.getComponentByPosition(idx), varBind) def getResponse(self, reqPDU): rspPDU = GetResponsePDU() @@ -143,8 +159,10 @@ class PDUAPI(object): def getVarBindTable(self, reqPDU, rspPDU): if apiPDU.getErrorStatus(rspPDU) == 2: varBindRow = [(vb[0], null) for vb in apiPDU.getVarBinds(reqPDU)] + else: varBindRow = apiPDU.getVarBinds(rspPDU) + return [varBindRow] def getNextVarBinds(self, varBinds, errorIndex=None): @@ -173,14 +191,32 @@ class TrapPDUAPI(object): try: import socket agentAddress = IpAddress(socket.gethostbyname(socket.gethostname())) + except Exception: agentAddress = IpAddress('0.0.0.0') + self._networkAddress = NetworkAddress().setComponentByPosition(0, agentAddress) - pdu.setComponentByPosition(0, self._entOid, verifyConstraints=False, matchTags=False, matchConstraints=False) - pdu.setComponentByPosition(1, self._networkAddress, verifyConstraints=False, matchTags=False, matchConstraints=False) - pdu.setComponentByPosition(2, self._genericTrap, verifyConstraints=False, matchTags=False, matchConstraints=False) - pdu.setComponentByPosition(3, self._zeroInt, verifyConstraints=False, matchTags=False, matchConstraints=False) - pdu.setComponentByPosition(4, self._zeroTime, verifyConstraints=False, matchTags=False, matchConstraints=False) + + pdu.setComponentByPosition( + 0, self._entOid, verifyConstraints=False, matchTags=False, + matchConstraints=False) + + pdu.setComponentByPosition( + 1, self._networkAddress, verifyConstraints=False, matchTags=False, + matchConstraints=False) + + pdu.setComponentByPosition( + 2, self._genericTrap, verifyConstraints=False, matchTags=False, + matchConstraints=False) + + pdu.setComponentByPosition( + 3, self._zeroInt, verifyConstraints=False, matchTags=False, + matchConstraints=False) + + pdu.setComponentByPosition( + 4, self._zeroTime, verifyConstraints=False, matchTags=False, + matchConstraints=False) + pdu.setComponentByPosition(5) @staticmethod @@ -197,7 +233,9 @@ class TrapPDUAPI(object): @staticmethod def setAgentAddr(pdu, value): - pdu.setComponentByPosition(1).getComponentByPosition(1).setComponentByPosition(0, value) + (pdu.setComponentByPosition(1) + .getComponentByPosition(1) + .setComponentByPosition(0, value)) @staticmethod def getGenericTrap(pdu): @@ -233,24 +271,28 @@ class TrapPDUAPI(object): @staticmethod def getVarBinds(pdu): - varBinds = [] - for varBind in pdu.getComponentByPosition(5): - varBinds.append(apiVarBind.getOIDVal(varBind)) - return varBinds + return [apiVarBind.getOIDVal(varBind) + for varBind in pdu.getComponentByPosition(5)] @staticmethod def setVarBinds(pdu, varBinds): varBindList = pdu.setComponentByPosition(5).getComponentByPosition(5) + varBindList.clear() + idx = 0 + for varBind in varBinds: if isinstance(varBind, VarBind): varBindList.setComponentByPosition(idx, varBind) + else: varBindList.setComponentByPosition(idx) + apiVarBind.setOIDVal( varBindList.getComponentByPosition(idx), varBind ) + idx += 1 @@ -262,8 +304,14 @@ class MessageAPI(object): _community = univ.OctetString('public') def setDefaults(self, msg): - msg.setComponentByPosition(0, self._version, verifyConstraints=False, matchTags=False, matchConstraints=False) - msg.setComponentByPosition(1, self._community, verifyConstraints=False, matchTags=False, matchConstraints=False) + msg.setComponentByPosition( + 0, self._version, verifyConstraints=False, matchTags=False, + matchConstraints=False) + + msg.setComponentByPosition( + 1, self._community, verifyConstraints=False, matchTags=False, + matchConstraints=False) + return msg @staticmethod @@ -288,14 +336,20 @@ class MessageAPI(object): @staticmethod def setPDU(msg, value): - msg.setComponentByPosition(2).getComponentByPosition(2).setComponentByType(value.getTagSet(), value, verifyConstraints=False, matchTags=False, matchConstraints=False, innerFlag=True) + (msg.setComponentByPosition(2) + .getComponentByPosition(2) + .setComponentByType(value.getTagSet(), value, verifyConstraints=False, + matchTags=False, matchConstraints=False, + innerFlag=True)) def getResponse(self, reqMsg): rspMsg = Message() + self.setDefaults(rspMsg) self.setVersion(rspMsg, self.getVersion(reqMsg)) self.setCommunity(rspMsg, self.getCommunity(reqMsg)) self.setPDU(rspMsg, apiPDU.getResponse(self.getPDU(reqMsg))) + return rspMsg diff --git a/pysnmp/proto/api/v2c.py b/pysnmp/proto/api/v2c.py index 05de2da1..c415ca9a 100644 --- a/pysnmp/proto/api/v2c.py +++ b/pysnmp/proto/api/v2c.py @@ -53,7 +53,8 @@ apiVarBind = v1.apiVarBind class PDUAPI(v1.PDUAPI): _errorStatus = rfc1905.errorStatus.clone(0) - _errorIndex = univ.Integer(0).subtype(subtypeSpec=constraint.ValueRangeConstraint(0, rfc1905.max_bindings)) + _errorIndex = univ.Integer(0).subtype( + subtypeSpec=constraint.ValueRangeConstraint(0, rfc1905.max_bindings)) def getResponse(self, reqPDU): rspPDU = ResponsePDU() @@ -86,14 +87,14 @@ class PDUAPI(v1.PDUAPI): def setEndOfMibError(self, pdu, errorIndex): varBindList = self.getVarBindList(pdu) varBindList[errorIndex - 1].setComponentByPosition( - 1, rfc1905.endOfMibView, verifyConstraints=False, matchTags=False, matchConstraints=False - ) + 1, rfc1905.endOfMibView, verifyConstraints=False, matchTags=False, + matchConstraints=False) def setNoSuchInstanceError(self, pdu, errorIndex): varBindList = self.getVarBindList(pdu) varBindList[errorIndex - 1].setComponentByPosition( - 1, rfc1905.noSuchInstance, verifyConstraints=False, matchTags=False, matchConstraints=False - ) + 1, rfc1905.noSuchInstance, verifyConstraints=False, matchTags=False, + matchConstraints=False) apiPDU = PDUAPI() @@ -105,15 +106,19 @@ class BulkPDUAPI(PDUAPI): def setDefaults(self, pdu): PDUAPI.setDefaults(self, pdu) + pdu.setComponentByPosition( - 0, getNextRequestID(), verifyConstraints=False, matchTags=False, matchConstraints=False - ) + 0, getNextRequestID(), verifyConstraints=False, matchTags=False, + matchConstraints=False) + pdu.setComponentByPosition( - 1, self._nonRepeaters, verifyConstraints=False, matchTags=False, matchConstraints=False - ) + 1, self._nonRepeaters, verifyConstraints=False, matchTags=False, + matchConstraints=False) + pdu.setComponentByPosition( - 2, self._maxRepetitions, verifyConstraints=False, matchTags=False, matchConstraints=False - ) + 2, self._maxRepetitions, verifyConstraints=False, matchTags=False, + matchConstraints=False) + pdu.setComponentByPosition(3) @staticmethod @@ -152,9 +157,11 @@ class BulkPDUAPI(PDUAPI): if R: for i in range(0, len(rspVarBinds) - N, R): varBindRow = rspVarBinds[:N] + rspVarBinds[N + i:N + R + i] + # ignore stray OIDs / non-rectangular table if len(varBindRow) == N + R: varBindTable.append(varBindRow) + elif N: varBindTable.append(rspVarBinds[:N]) @@ -175,9 +182,11 @@ class TrapPDUAPI(v1.PDUAPI): def setDefaults(self, pdu): v1.PDUAPI.setDefaults(self, pdu) + varBinds = [(self.sysUpTime, self._zeroTime), # generic trap (self.snmpTrapOID, self._genTrap)] + self.setVarBinds(pdu, varBinds) @@ -188,16 +197,24 @@ class MessageAPI(v1.MessageAPI): _version = rfc1901.version.clone(1) def setDefaults(self, msg): - msg.setComponentByPosition(0, self._version, verifyConstraints=False, matchTags=False, matchConstraints=False) - msg.setComponentByPosition(1, self._community, verifyConstraints=False, matchTags=False, matchConstraints=False) + msg.setComponentByPosition( + 0, self._version, verifyConstraints=False, matchTags=False, + matchConstraints=False) + + msg.setComponentByPosition( + 1, self._community, verifyConstraints=False, matchTags=False, + matchConstraints=False) + return msg def getResponse(self, reqMsg): rspMsg = Message() + self.setDefaults(rspMsg) self.setVersion(rspMsg, self.getVersion(reqMsg)) self.setCommunity(rspMsg, self.getCommunity(reqMsg)) self.setPDU(rspMsg, apiPDU.getResponse(self.getPDU(reqMsg))) + return rspMsg diff --git a/pysnmp/proto/api/verdec.py b/pysnmp/proto/api/verdec.py index e6ec7fd9..142a68c2 100644 --- a/pysnmp/proto/api/verdec.py +++ b/pysnmp/proto/api/verdec.py @@ -18,13 +18,17 @@ def decodeMessageVersion(wholeMsg): wholeMsg, asn1Spec=univ.Sequence(), recursiveFlag=False, substrateFun=lambda a, b, c: (a, b[:c]) ) + ver, wholeMsg = decoder.decode( wholeMsg, asn1Spec=univ.Integer(), recursiveFlag=False, substrateFun=lambda a, b, c: (a, b[:c]) ) + if eoo.endOfOctets.isSameTypeWith(ver): raise ProtocolError('EOO at SNMP version component') + return ver except PyAsn1Error as exc: - raise ProtocolError('Invalid BER at SNMP version component: %s' % exc) + raise ProtocolError('Invalid BER at SNMP version ' + 'component: %s' % exc) |