fix flake8 issues

add_visitor
Richard Piazza 2018-11-18 12:05:38 -05:00
parent 1142d729df
commit 4f5aafd91b
1 changed files with 2 additions and 33 deletions

View File

@ -1,5 +1,4 @@
import stix2 import stix2
from antlr4 import *
import six import six
from stix2patterns.grammars.STIXPatternParser import * from stix2patterns.grammars.STIXPatternParser import *
from stix2patterns.grammars.STIXPatternVisitor import STIXPatternVisitor from stix2patterns.grammars.STIXPatternVisitor import STIXPatternVisitor
@ -22,6 +21,7 @@ def collapse_lists(lists):
# This class defines a complete generic visitor for a parse tree produced by STIXPatternParser. # This class defines a complete generic visitor for a parse tree produced by STIXPatternParser.
class STIXPatternVisitorForSTIX2(STIXPatternVisitor): class STIXPatternVisitorForSTIX2(STIXPatternVisitor):
classes = {} classes = {}
@ -49,7 +49,6 @@ class STIXPatternVisitorForSTIX2(STIXPatternVisitor):
children = self.visitChildren(ctx) children = self.visitChildren(ctx)
return children[0] return children[0]
# Visit a parse tree produced by STIXPatternParser#observationExpressions. # Visit a parse tree produced by STIXPatternParser#observationExpressions.
def visitObservationExpressions(self, ctx): def visitObservationExpressions(self, ctx):
children = self.visitChildren(ctx) children = self.visitChildren(ctx)
@ -66,7 +65,6 @@ class STIXPatternVisitorForSTIX2(STIXPatternVisitor):
else: else:
return self.instantiate("OrObservationExpression", [children[0], children[2]]) return self.instantiate("OrObservationExpression", [children[0], children[2]])
# Visit a parse tree produced by STIXPatternParser#observationExpressionAnd. # Visit a parse tree produced by STIXPatternParser#observationExpressionAnd.
def visitObservationExpressionAnd(self, ctx): def visitObservationExpressionAnd(self, ctx):
children = self.visitChildren(ctx) children = self.visitChildren(ctx)
@ -75,37 +73,31 @@ class STIXPatternVisitorForSTIX2(STIXPatternVisitor):
else: else:
return self.instantiate("AndObservationExpression", [children[0], children[2]]) return self.instantiate("AndObservationExpression", [children[0], children[2]])
# Visit a parse tree produced by STIXPatternParser#observationExpressionRepeated. # Visit a parse tree produced by STIXPatternParser#observationExpressionRepeated.
def visitObservationExpressionRepeated(self, ctx): def visitObservationExpressionRepeated(self, ctx):
children = self.visitChildren(ctx) children = self.visitChildren(ctx)
return self.instantiate("QualifiedObservationExpression", children[0], children[1]) return self.instantiate("QualifiedObservationExpression", children[0], children[1])
# Visit a parse tree produced by STIXPatternParser#observationExpressionSimple. # Visit a parse tree produced by STIXPatternParser#observationExpressionSimple.
def visitObservationExpressionSimple(self, ctx): def visitObservationExpressionSimple(self, ctx):
children = self.visitChildren(ctx) children = self.visitChildren(ctx)
return self.instantiate("ObservationExpression", children[1]) return self.instantiate("ObservationExpression", children[1])
# Visit a parse tree produced by STIXPatternParser#observationExpressionCompound. # Visit a parse tree produced by STIXPatternParser#observationExpressionCompound.
def visitObservationExpressionCompound(self, ctx): def visitObservationExpressionCompound(self, ctx):
children = self.visitChildren(ctx) children = self.visitChildren(ctx)
return self.instantiate("ObservationExpression", children[1]) return self.instantiate("ObservationExpression", children[1])
# Visit a parse tree produced by STIXPatternParser#observationExpressionWithin. # Visit a parse tree produced by STIXPatternParser#observationExpressionWithin.
def visitObservationExpressionWithin(self, ctx): def visitObservationExpressionWithin(self, ctx):
children = self.visitChildren(ctx) children = self.visitChildren(ctx)
return self.instantiate("QualifiedObservationExpression", children[0], children[1]) return self.instantiate("QualifiedObservationExpression", children[0], children[1])
# Visit a parse tree produced by STIXPatternParser#observationExpressionStartStop. # Visit a parse tree produced by STIXPatternParser#observationExpressionStartStop.
def visitObservationExpressionStartStop(self, ctx): def visitObservationExpressionStartStop(self, ctx):
children = self.visitChildren(ctx) children = self.visitChildren(ctx)
return self.instantiate("QualifiedObservationExpression", children[0], children[1]) return self.instantiate("QualifiedObservationExpression", children[0], children[1])
# Visit a parse tree produced by STIXPatternParser#comparisonExpression. # Visit a parse tree produced by STIXPatternParser#comparisonExpression.
def visitComparisonExpression(self, ctx): def visitComparisonExpression(self, ctx):
children = self.visitChildren(ctx) children = self.visitChildren(ctx)
@ -114,7 +106,6 @@ class STIXPatternVisitorForSTIX2(STIXPatternVisitor):
else: else:
return self.instantiate("OrBooleanExpression", [children[0], children[2]]) return self.instantiate("OrBooleanExpression", [children[0], children[2]])
# Visit a parse tree produced by STIXPatternParser#comparisonExpressionAnd. # Visit a parse tree produced by STIXPatternParser#comparisonExpressionAnd.
def visitComparisonExpressionAnd(self, ctx): def visitComparisonExpressionAnd(self, ctx):
# TODO: NOT # TODO: NOT
@ -124,20 +115,18 @@ class STIXPatternVisitorForSTIX2(STIXPatternVisitor):
else: else:
return self.instantiate("AndBooleanExpression", [children[0], children[2]]) return self.instantiate("AndBooleanExpression", [children[0], children[2]])
# Visit a parse tree produced by STIXPatternParser#propTestEqual. # Visit a parse tree produced by STIXPatternParser#propTestEqual.
def visitPropTestEqual(self, ctx): def visitPropTestEqual(self, ctx):
children = self.visitChildren(ctx) children = self.visitChildren(ctx)
if len(children) == 4: if len(children) == 4:
operator = children[2].symbol.type operator = children[2].symbol.type
negated = negated=operator == STIXPatternParser.EQ negated = negated = operator == STIXPatternParser.EQ
return self.instantiate("EqualityComparisonExpression", children[0], children[3], negated) return self.instantiate("EqualityComparisonExpression", children[0], children[3], negated)
else: else:
operator = children[1].symbol.type operator = children[1].symbol.type
negated = negated = operator != STIXPatternParser.EQ negated = negated = operator != STIXPatternParser.EQ
return self.instantiate("EqualityComparisonExpression", children[0], children[2], negated) return self.instantiate("EqualityComparisonExpression", children[0], children[2], negated)
# Visit a parse tree produced by STIXPatternParser#propTestOrder. # Visit a parse tree produced by STIXPatternParser#propTestOrder.
def visitPropTestOrder(self, ctx): def visitPropTestOrder(self, ctx):
children = self.visitChildren(ctx) children = self.visitChildren(ctx)
@ -151,61 +140,51 @@ class STIXPatternVisitorForSTIX2(STIXPatternVisitor):
elif operator == STIXPatternParser.LE: elif operator == STIXPatternParser.LE:
return self.instantiate("LessThanEqualComparisonExpression", children[0], children[2], False) return self.instantiate("LessThanEqualComparisonExpression", children[0], children[2], False)
# Visit a parse tree produced by STIXPatternParser#propTestSet. # Visit a parse tree produced by STIXPatternParser#propTestSet.
def visitPropTestSet(self, ctx): def visitPropTestSet(self, ctx):
# TODO # TODO
return self.visitChildren(ctx) return self.visitChildren(ctx)
# Visit a parse tree produced by STIXPatternParser#propTestLike. # Visit a parse tree produced by STIXPatternParser#propTestLike.
def visitPropTestLike(self, ctx): def visitPropTestLike(self, ctx):
children = self.visitChildren(ctx) children = self.visitChildren(ctx)
return self.instantiate("LikeComparisonExpression", children[0], children[2], False) return self.instantiate("LikeComparisonExpression", children[0], children[2], False)
# Visit a parse tree produced by STIXPatternParser#propTestRegex. # Visit a parse tree produced by STIXPatternParser#propTestRegex.
def visitPropTestRegex(self, ctx): def visitPropTestRegex(self, ctx):
children = self.visitChildren(ctx) children = self.visitChildren(ctx)
return self.instantiate("MatchesComparisonExpression", children[0], children[2], False) return self.instantiate("MatchesComparisonExpression", children[0], children[2], False)
# Visit a parse tree produced by STIXPatternParser#propTestIsSubset. # Visit a parse tree produced by STIXPatternParser#propTestIsSubset.
def visitPropTestIsSubset(self, ctx): def visitPropTestIsSubset(self, ctx):
children = self.visitChildren(ctx) children = self.visitChildren(ctx)
return stix2.IsSubsetComparisonExpression(children[0], children[2]) return stix2.IsSubsetComparisonExpression(children[0], children[2])
# Visit a parse tree produced by STIXPatternParser#propTestIsSuperset. # Visit a parse tree produced by STIXPatternParser#propTestIsSuperset.
def visitPropTestIsSuperset(self, ctx): def visitPropTestIsSuperset(self, ctx):
children = self.visitChildren(ctx) children = self.visitChildren(ctx)
return stix2.IsSupersetComparisonExpression(children[0], children[2]) return stix2.IsSupersetComparisonExpression(children[0], children[2])
# Visit a parse tree produced by STIXPatternParser#propTestParen. # Visit a parse tree produced by STIXPatternParser#propTestParen.
def visitPropTestParen(self, ctx): def visitPropTestParen(self, ctx):
children = self.visitChildren(ctx) children = self.visitChildren(ctx)
return self.instantiate("ParentheticalExpression", children[1]) return self.instantiate("ParentheticalExpression", children[1])
# Visit a parse tree produced by STIXPatternParser#startStopQualifier. # Visit a parse tree produced by STIXPatternParser#startStopQualifier.
def visitStartStopQualifier(self, ctx): def visitStartStopQualifier(self, ctx):
children = self.visitChildren(ctx) children = self.visitChildren(ctx)
return stix2.StartStopQualifier(children[1], children[3]) return stix2.StartStopQualifier(children[1], children[3])
# Visit a parse tree produced by STIXPatternParser#withinQualifier. # Visit a parse tree produced by STIXPatternParser#withinQualifier.
def visitWithinQualifier(self, ctx): def visitWithinQualifier(self, ctx):
children = self.visitChildren(ctx) children = self.visitChildren(ctx)
return stix2.WithinQualifier(children[1]) return stix2.WithinQualifier(children[1])
# Visit a parse tree produced by STIXPatternParser#repeatedQualifier. # Visit a parse tree produced by STIXPatternParser#repeatedQualifier.
def visitRepeatedQualifier(self, ctx): def visitRepeatedQualifier(self, ctx):
children = self.visitChildren(ctx) children = self.visitChildren(ctx)
return stix2.RepeatQualifier(children[1]) return stix2.RepeatQualifier(children[1])
# Visit a parse tree produced by STIXPatternParser#objectPath. # Visit a parse tree produced by STIXPatternParser#objectPath.
def visitObjectPath(self, ctx): def visitObjectPath(self, ctx):
children = self.visitChildren(ctx) children = self.visitChildren(ctx)
@ -226,13 +205,11 @@ class STIXPatternVisitorForSTIX2(STIXPatternVisitor):
i += 1 i += 1
return self.instantiate("ObjectPath", children[0].getText(), property_path) return self.instantiate("ObjectPath", children[0].getText(), property_path)
# Visit a parse tree produced by STIXPatternParser#objectType. # Visit a parse tree produced by STIXPatternParser#objectType.
def visitObjectType(self, ctx): def visitObjectType(self, ctx):
children = self.visitChildren(ctx) children = self.visitChildren(ctx)
return children[0] return children[0]
# Visit a parse tree produced by STIXPatternParser#firstPathComponent. # Visit a parse tree produced by STIXPatternParser#firstPathComponent.
def visitFirstPathComponent(self, ctx): def visitFirstPathComponent(self, ctx):
children = self.visitChildren(ctx) children = self.visitChildren(ctx)
@ -242,18 +219,15 @@ class STIXPatternVisitorForSTIX2(STIXPatternVisitor):
# else: # else:
return stix2.BasicObjectPathComponent(step) return stix2.BasicObjectPathComponent(step)
# Visit a parse tree produced by STIXPatternParser#indexPathStep. # Visit a parse tree produced by STIXPatternParser#indexPathStep.
def visitIndexPathStep(self, ctx): def visitIndexPathStep(self, ctx):
children = self.visitChildren(ctx) children = self.visitChildren(ctx)
return children[1] return children[1]
# Visit a parse tree produced by STIXPatternParser#pathStep. # Visit a parse tree produced by STIXPatternParser#pathStep.
def visitPathStep(self, ctx): def visitPathStep(self, ctx):
return collapse_lists(self.visitChildren(ctx)) return collapse_lists(self.visitChildren(ctx))
# Visit a parse tree produced by STIXPatternParser#keyPathStep. # Visit a parse tree produced by STIXPatternParser#keyPathStep.
def visitKeyPathStep(self, ctx): def visitKeyPathStep(self, ctx):
children = self.visitChildren(ctx) children = self.visitChildren(ctx)
@ -263,25 +237,20 @@ class STIXPatternVisitorForSTIX2(STIXPatternVisitor):
else: else:
return stix2.BasicObjectPathComponent(children[1].getText(), is_key=True) return stix2.BasicObjectPathComponent(children[1].getText(), is_key=True)
# Visit a parse tree produced by STIXPatternParser#setLiteral. # Visit a parse tree produced by STIXPatternParser#setLiteral.
def visitSetLiteral(self, ctx): def visitSetLiteral(self, ctx):
return self.visitChildren(ctx) return self.visitChildren(ctx)
# Visit a parse tree produced by STIXPatternParser#primitiveLiteral. # Visit a parse tree produced by STIXPatternParser#primitiveLiteral.
def visitPrimitiveLiteral(self, ctx): def visitPrimitiveLiteral(self, ctx):
children = self.visitChildren(ctx) children = self.visitChildren(ctx)
return children[0] return children[0]
# Visit a parse tree produced by STIXPatternParser#orderableLiteral. # Visit a parse tree produced by STIXPatternParser#orderableLiteral.
def visitOrderableLiteral(self, ctx): def visitOrderableLiteral(self, ctx):
children = self.visitChildren(ctx) children = self.visitChildren(ctx)
return children[0] return children[0]
def visitTerminal(self, node): def visitTerminal(self, node):
if node.symbol.type == STIXPatternParser.IntPosLiteral or node.symbol.type == STIXPatternParser.IntNegLiteral: if node.symbol.type == STIXPatternParser.IntPosLiteral or node.symbol.type == STIXPatternParser.IntNegLiteral:
return stix2.IntegerConstant(node.getText()) return stix2.IntegerConstant(node.getText())