From 83b11f1238466c16c3ee564a82e7d4e68fb86f31 Mon Sep 17 00:00:00 2001 From: elie Date: Sat, 20 Jun 2015 12:14:48 +0000 Subject: - a few additional MIB tree management methods added to MibViewController to better address ordered nature of MIB tree nodes (namely, getFirst*, getLast* family of methods) - rfc1902.ObjectIdentity() now accepts 'last' keyword parameter to position on last (otherwise, first) object in given MIB module. --- pysnmp/smi/view.py | 37 ++++++++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 7 deletions(-) (limited to 'pysnmp/smi/view.py') diff --git a/pysnmp/smi/view.py b/pysnmp/smi/view.py index f7f6118..61ea964 100644 --- a/pysnmp/smi/view.py +++ b/pysnmp/smi/view.py @@ -142,13 +142,19 @@ class MibViewController: # Module management - def getFirstModuleName(self): + def getOrderedModuleName(self, index): self.indexMib() modNames = self.__mibSymbolsIdx.keys() if modNames: - return modNames[0] + return modNames[index] raise error.SmiError('No modules loaded at %s' % self) + def getFirstModuleName(self): + return self.getOrderedModuleName(0) + + def getLastModuleName(self): + return self.getOrderedModuleName(-1) + def getNextModuleName(self, modName): self.indexMib() try: @@ -234,7 +240,7 @@ class MibViewController: oid + suffix + nodeName[1:], modName ) - def getFirstNodeName(self, modName=''): + def getOrderedNodeName(self, index, modName=''): self.indexMib() if modName in self.__mibSymbolsIdx: mibMod = self.__mibSymbolsIdx[modName] @@ -245,10 +251,21 @@ class MibViewController: if not mibMod['oidToLabelIdx']: raise error.NoSuchObjectError( str='No variables at MIB module %s at %s' % (modName, self) - ) - oid, label = mibMod['oidToLabelIdx'].items()[0] + ) + try: + oid, label = mibMod['oidToLabelIdx'].items()[index] + except KeyError: + raise error.NoSuchObjectError( + str='No symbol at position %s in MIB module %s at %s' % (index, modName, self) + ) return oid, label, () + + def getFirstNodeName(self, modName=''): + return self.getOrderedNodeName(0, modName) + def getLastNodeName(self, modName=''): + return self.getOrderedNodeName(-1, modName) + def getNextNodeName(self, nodeName, modName=''): oid, label, suffix = self.getNodeName(nodeName, modName) try: @@ -291,7 +308,7 @@ class MibViewController: ) return m, typeName - def getFirstTypeName(self, modName=''): + def getOrderedTypeName(self, index, modName=''): self.indexMib() if modName in self.__mibSymbolsIdx: mibMod = self.__mibSymbolsIdx[modName] @@ -303,9 +320,15 @@ class MibViewController: raise error.NoSuchObjectError( str='No types at MIB module %s at %s' % (modName, self) ) - t = mibMod['typeToModIdx'].keys()[0] + t = mibMod['typeToModIdx'].keys()[index] return mibMod['typeToModIdx'][t], t + def getFirstTypeName(self, modName=''): + return self.getOrderedTypeName(0, modName) + + def getLastTypeName(self, modName=''): + return self.getOrderedTypeName(-1, modName) + def getNextType(self, typeName, modName=''): m, t = self.getTypeName(typeName, modName) try: -- cgit v1.2.1