File: mibvar.py

package info (click to toggle)
python-pysnmp4 4.1.9a-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 1,068 kB
  • ctags: 1,969
  • sloc: python: 10,608; sh: 44; makefile: 10
file content (65 lines) | stat: -rw-r--r-- 2,387 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# MIB variable pretty printers/parsers
import types
from pyasn1.type import univ
from pysnmp.smi.error import NoSuchObjectError

# Name

def mibNameToOid(mibView, name):
    if type(name[0]) == types.TupleType:
        modName, symName = apply(lambda x='',y='': (x,y), name[0])
        if modName: # load module if needed
            mibView.mibBuilder.loadModules(modName)
        else:
            mibView.mibBuilder.loadModules() # load all (slow)
        if symName:
            oid, label, suffix = mibView.getNodeNameByDesc(symName, modName)
        else:
            oid, label, suffix = mibView.getFirstNodeName(modName)
        suffix = name[1:]
        modName, symName, _s = mibView.getNodeLocation(oid)
        mibNode, = mibView.mibBuilder.importSymbols(
            modName, symName
            )
        if hasattr(mibNode, 'createTest'): # table column XXX
            modName, symName, _s = mibView.getNodeLocation(oid[:-1])
            rowNode, = mibView.mibBuilder.importSymbols(modName, symName)
            return oid, apply(rowNode.getInstIdFromIndices, suffix)
        else: # scalar or incomplete spec
            return oid, suffix
    else:
        oid, label, suffix = mibView.getNodeNameByOid(name)
        return oid, suffix

__scalarSuffix = (univ.Integer(0),)

def oidToMibName(mibView, oid):
    _oid, label, suffix = mibView.getNodeNameByOid(tuple(oid))
    modName, symName, __suffix = mibView.getNodeLocation(_oid)
    mibNode, = mibView.mibBuilder.importSymbols(
        modName, symName
        )
    if hasattr(mibNode, 'createTest'): # table column
        __modName, __symName, __s = mibView.getNodeLocation(_oid[:-1])
        rowNode, = mibView.mibBuilder.importSymbols(__modName, __symName)
        return (symName, modName), rowNode.getIndicesFromInstId(suffix)
    elif not suffix: # scalar
        return (symName, modName), suffix
    elif suffix == (0,): # scalar
        return (symName, modName), __scalarSuffix
    else:
        raise NoSuchObjectError(
            str='No MIB info for %s (closest parent %s)' %
            (oid, mibNode.name)
            )

# Value

def cloneFromMibValue(mibView, modName, symName, value):
    mibNode, = mibView.mibBuilder.importSymbols(
        modName, symName
        )
    if hasattr(mibNode, 'syntax'): # scalar
        return mibNode.syntax.clone(value)
    else:
        return   # identifier