<commit_before>from pysnmp.proto import rfc1157, rfc1905, api
from pysnmp.proto import rfc1905, rfc3411, error
import pysnmp.smi.error

vacmID = 3

# 3.2
class CmdRspBase:
    pduTypes = ()

    def __init__(self, snmpEngine, contextEngineId=None):
        snmpEngine.msgAndPduDsp.registerContextEngineId(
            contextEngineId, self.pduTypes, self.processPdu
            )
        self.__contextEngineId = contextEngineId # for unregistration
        self.__pendingReqs = {}
        self.__contextNames = {}

    def registerContextName(self, contextName, mibInstrumController):
        if self.__contextNames.has_key(contextName):
            raise error.ProtocolError(
                'Duplicate contextNames %s' % contextName
                )
        self.__contextNames[contextName] = mibInstrumController

    def unregisterContextName(self, contextName):
        if not self.__contextNames.has_key(contextName):
            raise error.ProtocolError(
                'No such contextName %s' % contextName
                )
        del self.__contextNames[contextName]
        
    def close(self, snmpEngine):
        snmpEngine.msgAndPduDsp.unregisterContextEngineId(
            self.__contextEngineId, self.pduTypes
            )

    def __sendResponse(self, snmpEngine, errorStatus, errorIndex,
                       varBinds, stateReference):
        ( messageProcessingModel,
          securityModel,
          securityName,
          securityLevel,
          contextEngineID,
          contextName,
          pduVersion,
          PDU,
          maxSizeResponseScopedPDU,
          statusInformation ) = self.__pendingReqs[stateReference]

        del self.__pendingReqs[stateReference]

        pMod = api.protoModules[pduVersion]

        pMod.apiPDU.setErrorStatus(PDU, errorStatus)
        pMod.apiPDU.setErrorIndex(PDU, errorIndex)
        pMod.apiPDU.setVarBinds(PDU, varBinds)

        # 3.2.6
        try:
            snmpEngine.msgAndPduDsp.returnResponsePdu(
                snmpEngine,
                messageProcessingModel,
                securityModel,
                securityName,
                securityLevel,
                contextEngineID,
                contextName,
                pduVersion,
                PDU,
                maxSizeResponseScopedPDU,
                stateReference,
                statusInformation
                )
        except error.StatusInformation:
            snmpSilentDrops, = snmpEngine.msgAndPduDsp.mibInstrumController.mibBuilder.importSymbols('SNMPv2-MIB', 'snmpSilentDrops')
            snmpSilentDrops.syntax = snmpSilentDrops.syntax + 1

    _getRequestType = rfc1905.GetRequestPDU.tagSet
    _getNextRequestType = rfc1905.GetNextRequestPDU.tagSet
    _setRequestType = rfc1905.SetRequestPDU.tagSet
    
    def processPdu(
        self,
        snmpEngine,
        messageProcessingModel,
        securityModel,
        securityName,
        securityLevel,
        contextEngineID,
        contextName,
        pduVersion,
        PDU,
        maxSizeResponseScopedPDU,
        stateReference
        ):
        pMod = api.protoModules[pduVersion]

        # 3.2.1
        if rfc3411.readClassPDUs.has_key(PDU.tagSet):
            viewType = 'read'
        elif rfc3411.writeClassPDUs.has_key(PDU.tagSet):
            viewType = 'write'
        else:
            raise error.ProtocolError('Unexpected PDU class %s' % PDU.tagSet)
        
        # 3.2.2
        requestId = pMod.apiPDU.getRequestID(PDU)

        # 3.2.4
        rspPDU = pMod.apiPDU.getResponse(PDU)

        statusInformation = {}
        
        self.__pendingReqs[stateReference] = (
            messageProcessingModel,
            securityModel,
            securityName,
            securityLevel,
            contextEngineID,
            contextName,
            pduVersion,
            rspPDU,
            maxSizeResponseScopedPDU,
            statusInformation
            )

        if self.__contextNames.has_key(str(contextName)):
            contextMibInstrumCtl =  self.__contextNames[contextName]
        else:
            contextMibInstrumCtl = snmpEngine.msgAndPduDsp.mibInstrumController

        acCtx = (
            snmpEngine, securityModel, securityName, securityLevel, contextName
            )
            
        # 3.2.5
        varBinds = pMod.apiPDU.getVarBinds(PDU)
        errorStatus, errorIndex = 'noError', 0
        try:
            errorStatus, errorIndex, varBinds = self._handleManagementOperation(
                snmpEngine, contextMibInstrumCtl, pMod, PDU,
                (self.__verifyAccess, acCtx)
                )
        # SNMPv2 SMI exceptions
        except pysnmp.smi.error.GenError, errorIndication:
            if errorIndication.has_key('oid'):
                # Request REPORT generation
                statusInformation['oid'] = errorIndication['oid'] 
                statusInformation['val'] = errorIndication['val']

        # PDU-level SMI errors
        except pysnmp.smi.error.NoAccessError, errorIndication:
            errorStatus, errorIndex = 'noAccess', errorIndication['idx']
        except pysnmp.smi.error.WrongTypeError, errorIndication:
            errorStatus, errorIndex = 'wrongType', errorIndication['idx']<commit_msg>make use of multi-lingual proxy<commit_after>from pysnmp.proto import rfc1157, rfc1905, api
from pysnmp.proto import rfc1905, rfc3411, error
from pysnmp.proto.proxy import rfc2576
import pysnmp.smi.error

vacmID = 3

# 3.2
class CmdRspBase:
    pduTypes = ()

    def __init__(self, snmpEngine, contextEngineId=None):
        snmpEngine.msgAndPduDsp.registerContextEngineId(
            contextEngineId, self.pduTypes, self.processPdu
            )
        self.__contextEngineId = contextEngineId # for unregistration
        self.__pendingReqs = {}
        self.__contextNames = {}

    def registerContextName(self, contextName, mibInstrumController):
        if self.__contextNames.has_key(contextName):
            raise error.ProtocolError(
                'Duplicate contextNames %s' % contextName
                )
        self.__contextNames[contextName] = mibInstrumController

    def unregisterContextName(self, contextName):
        if not self.__contextNames.has_key(contextName):
            raise error.ProtocolError(
                'No such contextName %s' % contextName
                )
        del self.__contextNames[contextName]
        
    def close(self, snmpEngine):
        snmpEngine.msgAndPduDsp.unregisterContextEngineId(
            self.__contextEngineId, self.pduTypes
            )

    def __sendResponse(self, snmpEngine, errorStatus, errorIndex,
                       varBinds, stateReference):
        ( messageProcessingModel,
          securityModel,
          securityName,
          securityLevel,
          contextEngineID,
          contextName,
          pduVersion,
          PDU,
          maxSizeResponseScopedPDU,
          statusInformation ) = self.__pendingReqs[stateReference]

        del self.__pendingReqs[stateReference]

        pMod = api.protoModules[pduVersion]

        pMod.apiPDU.setErrorStatus(PDU, errorStatus)
        pMod.apiPDU.setErrorIndex(PDU, errorIndex)
        pMod.apiPDU.setVarBinds(PDU, varBinds)

        # Agent-side API complies with SMIv2
        if messageProcessingModel == 0:
            PDU = rfc2576.v2ToV1(PDU)

        # 3.2.6
        try:
            snmpEngine.msgAndPduDsp.returnResponsePdu(
                snmpEngine,
                messageProcessingModel,
                securityModel,
                securityName,
                securityLevel,
                contextEngineID,
                contextName,
                pduVersion,
                PDU,
                maxSizeResponseScopedPDU,
                stateReference,
                statusInformation
                )
        except error.StatusInformation:
            snmpSilentDrops, = snmpEngine.msgAndPduDsp.mibInstrumController.mibBuilder.importSymbols('SNMPv2-MIB', 'snmpSilentDrops')
            snmpSilentDrops.syntax = snmpSilentDrops.syntax + 1

    _getRequestType = rfc1905.GetRequestPDU.tagSet
    _getNextRequestType = rfc1905.GetNextRequestPDU.tagSet
    _setRequestType = rfc1905.SetRequestPDU.tagSet
    
    def processPdu(
        self,
        snmpEngine,
        messageProcessingModel,
        securityModel,
        securityName,
        securityLevel,
        contextEngineID,
        contextName,
        pduVersion,
        PDU,
        maxSizeResponseScopedPDU,
        stateReference
        ):
        pMod = api.protoModules[pduVersion]

        # 3.2.1
        if rfc3411.readClassPDUs.has_key(PDU.tagSet):
            viewType = 'read'
        elif rfc3411.writeClassPDUs.has_key(PDU.tagSet):
            viewType = 'write'
        else:
            raise error.ProtocolError('Unexpected PDU class %s' % PDU.tagSet)
        
        # 3.2.2
        requestId = pMod.apiPDU.getRequestID(PDU)

        # 3.2.4
        rspPDU = pMod.apiPDU.getResponse(PDU)

        statusInformation = {}
        
        self.__pendingReqs[stateReference] = (
            messageProcessingModel,
            securityModel,
            securityName,
            securityLevel,
            contextEngineID,
            contextName,
            pduVersion,
            rspPDU,
            maxSizeResponseScopedPDU,
            statusInformation
            )

        if self.__contextNames.has_key(str(contextName)):
            contextMibInstrumCtl =  self.__contextNames[contextName]
        else:
            contextMibInstrumCtl = snmpEngine.msgAndPduDsp.mibInstrumController

        acCtx = (
            snmpEngine, securityModel, securityName, securityLevel, contextName
            )

        # Agent-side API complies with SMIv2
        if messageProcessingModel == 0:
            PDU = rfc2576.v1ToV2(PDU)
        
        # 3.2.5
        varBinds = pMod.apiPDU.getVarBinds(PDU)
        errorStatus, errorIndex = 'noError', 0
        try:
            errorStatus, errorIndex, varBinds = self._handleManagementOperation(
                snmpEngine, contextMibInstrumCtl, pMod, PDU,
                (self.__verifyAccess, acCtx)
                )
        # SNMPv2 SMI exceptions
        except pysnmp.smi.error.GenError, errorIndication:
            if errorIndication.has_key('oid'):
                # Request REPORT generation
                statusInformation['oid'] = errorIndication['oid'] 
                statusInformation['val'] = errorIndication['val']

        # PDU-level SMI errors
        except pysnmp.smi.error.NoAccessError, errorIndication:
            errorStatus, errorIndex = 'noAccess', errorIndication['idx']
        except pysnmp.smi.error.WrongTypeError, errorIndication:
            errorStatus, errorIndex = 'wrongType', errorIndication['idx']
{'fraction_non_alphanumeric': 0.07225460264594319, 'fraction_numerical': 0.012582107503006753, 'mean_word_length': 1.9551667577911427, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1662516', 'n_tokens_mistral': 3219, 'n_tokens_neox': 3052, 'n_words': 505}