File: msgmod.py

package info (click to toggle)
python-pysnmp4-apps 0.4.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 284 kB
  • sloc: python: 2,397; makefile: 3
file content (60 lines) | stat: -rw-r--r-- 1,422 bytes parent folder | download
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
#
# This file is part of pysnmp-apps software.
#
# Copyright (c) 2005-2016, Ilya Etingof <ilya@glas.net>
# License: http://pysnmp.sf.net/license.html
#
from pysnmp_apps.cli import base
from pysnmp import error

# Usage

def getUsage():
    return "\
SNMP message processing options:\n\
   -v VERSION             SNMP version: \"1\"|\"2c\"|\"3\"\n\
"

# Scanner

class MPScannerMixIn:
    def t_version(self, s):
        r' -v '
        self.rv.append(base.ConfigToken('version'))

# Parser

class MPParserMixIn:
    def p_mpSpec(self, args):
        '''
        Option ::= SnmpVersionId
        SnmpVersionId ::= version string
        SnmpVersionId ::= version whitespace string
        '''

# Generator

class __MPGenerator(base.GeneratorTemplate):
    _versionIdMap = {
        '1':  0,
        '2':  1,
        '2c': 1,
        '3':  3
        }
    def n_SnmpVersionId(self, cbCtx, node):
        snmpEngine, ctx = cbCtx
        if len(node) > 2:
            versionId = node[2].attr
        else:
            versionId = node[1].attr
        if versionId in self._versionIdMap:
            ctx['versionId'] = self._versionIdMap[versionId]
        else:
            raise error.PySnmpError('Bad version value %s' % versionId)

def generator(cbCtx, ast):
    snmpEngine, ctx = cbCtx
    __MPGenerator().preorder((snmpEngine, ctx), ast)
    # Commit defaults
    if 'versionId' not in ctx:
        ctx['versionId'] = 3