base.py :  » Network » Python-SNMP » pysnmp-apps-0.2.9a » pysnmp_apps » cli » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Network » Python SNMP 
Python SNMP » pysnmp apps 0.2.9a » pysnmp_apps » cli » base.py
# Abstract interface to SNMP objects initializers
from pysnmp_apps.cli import spark

# AST

class ConfigToken:
    # Abstract grammar token
    def __init__(self, type, attr=None):
        self.type = type
        self.attr = attr
    def __cmp__(self, o):
        return cmp(self.type, o)
    def __repr__(self):
        return self.attr or self.type
    def __str__(self):
        if self.attr is None:
            return '%s' % self.type
        else:
            return '%s(%s)' % (self.type, self.attr)
    
class ConfigNode:
    # AST node class -- N-ary tree
    def __init__(self, type, attr=None):
        self.type, self.attr = type, attr
        self._kids = []
    def __getitem__(self, i):
        return self._kids[i]
    def __len__(self):
        return len(self._kids)
    def __setslice__(self, low, high, seq):
        self._kids[low:high] = seq
    def __cmp__(self, o):
        return cmp(self.type, o)
    def __str__(self):
        if self.attr is None:
            return self.type
        else:
            return '%s(%s)' % (self.type, self.attr)

# Scanner

class __ScannerTemplate(spark.GenericScanner):
    def tokenize(self, input):
        self.rv = []
        spark.GenericScanner.tokenize(self, input)
        return self.rv

class __FirstLevelScanner(__ScannerTemplate):
    def t_string(self, s):
        r' [!#\$%&\'\(\)\*\+,\.//0-9<=>\?@A-Z\\\^_`a-z\{\|\}~][!#\$%&\'\(\)\*\+,\-\.//0-9<=>\?@A-Z\\\^_`a-z\{\|\}~]* '
        self.rv.append(ConfigToken('string', s))

class __SecondLevelScanner(__FirstLevelScanner):
    def t_semicolon(self, s):
        r' : '
        self.rv.append(ConfigToken('semicolon'))

    def t_lparen(self, s):
        r' \[ '
        self.rv.append(ConfigToken('lparen'))

    def t_rparen(self, s):
        r' \] '
        self.rv.append(ConfigToken('rparen'))

    def t_quote(self, s):
        r' \" '
        self.rv.append(ConfigToken('quote'))

    def t_whitespace(self, s):
        r' \s+ '
        self.rv.append(ConfigToken('whitespace'))

ScannerTemplate = __SecondLevelScanner

# Parser

class ParserTemplate(spark.GenericASTBuilder):
    initialSymbol = None
    def __init__(self, startSymbol=None):
        if startSymbol is None:
            startSymbol = self.initialSymbol
        spark.GenericASTBuilder.__init__(self, ConfigNode, startSymbol)

    def terminal(self, token):
        #  Reduce to homogeneous AST.
        return ConfigNode(token.type, token.attr)

# Generator

class GeneratorTemplate(spark.GenericASTTraversal):
    def __init__(self): pass  # Skip superclass constructor

    def typestring(self, node):
        return node.type

    def preorder(self, client, node):
        try:
            name = 'n_' + self.typestring(node)
            if hasattr(self, name):
                func = getattr(self, name)
                func(client, node)
            else:
                self.default(client, node)
        except spark.GenericASTTraversalPruningException:
            return client

        for kid in node:
            self.preorder(client, kid)

        name = name + '_exit'
        if hasattr(self, name):
            func = getattr(self, name)
            func(client, node)

        return client
    
    def default(self, client, node): pass
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.