Skip to content

Commit

Permalink
source code pep8'ed
Browse files Browse the repository at this point in the history
  • Loading branch information
etingof committed Aug 9, 2017
1 parent 73d3655 commit 4ceca26
Show file tree
Hide file tree
Showing 16 changed files with 420 additions and 278 deletions.
1 change: 1 addition & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Revision 0.4.2, released 09-08-2017
- Bumped pysnmp dependency to 4.3.9
- Fixed crash on string value rendering due to improper
low-level pyasn1 .prettyOut() call
- Source code PEP8'ed
- Author's e-mail changed, copyright extended towards 2017

Revision 0.4.1, released 12-02-2016
Expand Down
82 changes: 61 additions & 21 deletions pysnmp_apps/cli/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,50 @@

# AST


class ConfigToken:
# Abstract grammar token
def __init__(self, type, attr=None):
self.type = type
def __init__(self, typ, attr=None):
self.type = typ
self.attr = attr
def __eq__(self, other): return self.type == other
def __ne__(self, other): return self.type != other
def __lt__(self, other): return self.type < other
def __le__(self, other): return self.type <= other
def __gt__(self, other): return self.type > other
def __ge__(self, other): return self.type >= other
def __repr__(self): return self.attr or self.type

def __eq__(self, other):
return self.type == other

def __ne__(self, other):
return self.type != other

def __lt__(self, other):
return self.type < other

def __le__(self, other):
return self.type <= other

def __gt__(self, other):
return self.type > other

def __ge__(self, other):
return self.type >= other

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
def __init__(self, typ, attr=None):
self.type, self.attr = typ, attr
self._kids = []

def __getitem__(self, i):
return self._kids[i]

def __len__(self):
return len(self._kids)
if sys.version_info[0] < 3:
Expand All @@ -42,31 +61,47 @@ def __setslice__(self, low, high, seq):
else:
def __setitem__(self, idx, seq):
self._kids[idx] = seq
def __eq__(self, other): return self.type == other
def __ne__(self, other): return self.type != other
def __lt__(self, other): return self.type < other
def __le__(self, other): return self.type <= other
def __gt__(self, other): return self.type > other
def __ge__(self, other): return self.type >= other

def __eq__(self, other):
return self.type == other

def __ne__(self, other):
return self.type != other

def __lt__(self, other):
return self.type < other

def __le__(self, other):
return self.type <= other

def __gt__(self, other):
return self.type > other

def __ge__(self, other):
return self.type >= other

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):
def tokenize(self, data):
self.rv = []
spark.GenericScanner.tokenize(self, input)
spark.GenericScanner.tokenize(self, data)
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' : '
Expand All @@ -90,10 +125,12 @@ def t_whitespace(self, s):

ScannerTemplate = __SecondLevelScanner


# Parser

class ParserTemplate(spark.GenericASTBuilder):
initialSymbol = None

def __init__(self, startSymbol=None):
if startSymbol is None:
startSymbol = self.initialSymbol
Expand All @@ -103,10 +140,12 @@ 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 __init__(self): # Skip superclass constructor
pass

def typestring(self, node):
return node.type
Expand All @@ -132,4 +171,5 @@ def preorder(self, client, node):

return client

def default(self, client, node): pass
def default(self, client, node):
pass
23 changes: 14 additions & 9 deletions pysnmp_apps/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

# Usage


def getUsage():
return "\
Command-line SNMP tools version %s, written by Ilya Etingof <[email protected]>\n\
Expand All @@ -42,14 +43,15 @@ def getUsage():
-V software release information\n\
-d dump raw packets\n\
-D category enable debugging [%s]\n\
" % ( pysnmpAppsVersion,
pysmiVersion,
pysnmpVersion,
pysnmpMibsVersion,
pyasn1Version,
sys.version.replace('\n', ''),
debug and ','.join(debug.flagMap.keys()) or "")
" % (pysnmpAppsVersion,
pysmiVersion,
pysnmpVersion,
pysnmpMibsVersion,
pyasn1Version,
sys.version.replace('\n', ''),
debug and ','.join(debug.flagMap.keys()) or "")


# Scanner

class MainScannerMixIn:
Expand All @@ -71,13 +73,14 @@ def t_debug(self, s):

# Parser


class MainParserMixIn:
initialSymbol = 'Cmdline'

def error(self, token):
raise error.PySnmpError(
'Command-line parser error at token %s\n' % token
)
)

def p_cmdline(self, args):
'''
Expand All @@ -104,9 +107,10 @@ def p_cmdlineExt(self, args):
Debug ::= debug string
Debug ::= debug whitespace string
'''

# Generator


class __MainGenerator(base.GeneratorTemplate):
# SNMPv1/v2
def n_VersionInfo(self, cbCtx, node):
Expand All @@ -127,6 +131,7 @@ def n_Debug(self, cbCtx, node):
f = node[1].attr
debug.setLogger(debug.Debug(*f.split(',')))


def generator(cbCtx, ast):
snmpEngine, ctx = cbCtx
ctx['mibViewController'] = view.MibViewController(
Expand Down
Loading

0 comments on commit 4ceca26

Please sign in to comment.