# SNMP entity context
from pysnmp import error

class SnmpContext:
    def __init__(self, snmpEngine, contextEngineId=None):
        if contextEngineId is None:
            # Default to local snmpEngineId
            contextEngineId,= snmpEngine.msgAndPduDsp.mibInstrumController.mibBuilder.importSymbols('__SNMP-FRAMEWORK-MIB', 'snmpEngineID')
            contextEngineId = contextEngineId.syntax
        self.contextEngineId = contextEngineId
        self.contextNames = {
            '': snmpEngine.msgAndPduDsp.mibInstrumController # Default name
            } 

    def registerContextName(self, contextName, mibInstrum=None):
        if self.contextNames.has_key(contextName):
            raise error.PySnmpError(
                'Duplicate contextName %s' % contextName
                )
        if mibInstrum is None:
            self.contextNames[contextName] = self.contextNames['']
        else:
            self.contextNames[contextName] = mibInstrum
            
    def unregisterContextName(self, contextName):
        if self.contextNames.has_key(contextName):
            del self.contextNames[contextName]

    def getMibInstrum(self, contextName):
        if not self.contextNames.has_key(contextName):
            raise error.PySnmpError(
                'Missing contextName %s' % contextName
                )
        else:
            return self.contextNames[contextName]

@@ -39,16 +39,41 @@
 rt error
+%0Afrom pysnmp import debug
 %0A%0Aclass 
@@ -455,32 +455,143 @@
 contextEngineId%0A
+        debug.logger & debug.flagIns and debug.logger('SnmpContext: contextEngineId %5C%22%25s%5C%22' %25 contextEngineId)%0A
         self.con
@@ -916,32 +916,183 @@
               )%0A
+        debug.logger & debug.flagIns and debug.logger('registerContextName: registered contextName %5C%22%25s%5C%22, mibInstrum %25s' %25 (contextName, mibInstrum))%0A
         if mibIn
@@ -1349,32 +1349,162 @@
 y(contextName):%0A
+            debug.logger & debug.flagIns and debug.logger('unregisterContextName: unregistered contextName %5C%22%25s%5C%22' %25 contextName)%0A
             del 
@@ -1648,119 +1648,400 @@
     
-raise error.PySnmpError(%0A                'Missing contextName %25s' %25 contextName%0A                )%0A        else:
+debug.logger & debug.flagIns and debug.logger('getMibInstrum: contextName %5C%22%25s%5C%22 not registered' %25 contextName)%0A            raise error.PySnmpError(%0A                'Missing contextName %25s' %25 contextName%0A                )%0A        else:%0A            debug.logger & debug.flagIns and debug.logger('getMibInstrum: contextName %5C%22%25s%5C%22, mibInstum %25s' %25 (contextName, self.contextNames%5BcontextName%5D))
 %0A   

add debugging for context operations