pynspect.traversers module

This module contains implementation of object representations of rule tree traversers, that are supposed to be used to work with rule tree structures.

The base implementation and interface definition can be found in following class:

There are simple example implementations of rule tree traversers capable of printing given rule tree into a formated plain text or HTML strings:

There is also a base class for implementing filtering rule tree traversers, that provides many usefull tools and features and can be used to implement traversers that actually do something more interesting like filtering:

class pynspect.traversers.BaseFilteringTreeTraverser[source]

Bases: pynspect.traversers.BaseRuleTreeTraverser

Base class for all filtering rule tree traversers.

binops_comparison = {'OP_EQ': <function BaseFilteringTreeTraverser.<lambda>>, 'OP_GE': <function BaseFilteringTreeTraverser.<lambda>>, 'OP_GT': <function BaseFilteringTreeTraverser.<lambda>>, 'OP_IN': <function BaseFilteringTreeTraverser.<lambda>>, 'OP_IS': <function BaseFilteringTreeTraverser.<lambda>>, 'OP_LE': <function BaseFilteringTreeTraverser.<lambda>>, 'OP_LIKE': <function BaseFilteringTreeTraverser.<lambda>>, 'OP_LT': <function BaseFilteringTreeTraverser.<lambda>>, 'OP_NE': <function BaseFilteringTreeTraverser.<lambda>>}

Definitions of all comparison binary operations.

binops_logical = {'OP_AND': <function BaseFilteringTreeTraverser.<lambda>>, 'OP_AND_P': <function BaseFilteringTreeTraverser.<lambda>>, 'OP_OR': <function BaseFilteringTreeTraverser.<lambda>>, 'OP_OR_P': <function BaseFilteringTreeTraverser.<lambda>>, 'OP_XOR': <function BaseFilteringTreeTraverser.<lambda>>, 'OP_XOR_P': <function BaseFilteringTreeTraverser.<lambda>>}

Definitions of all logical binary operations.

binops_math = {'OP_DIVIDE': <function BaseFilteringTreeTraverser.<lambda>>, 'OP_MINUS': <function BaseFilteringTreeTraverser.<lambda>>, 'OP_MODULO': <function BaseFilteringTreeTraverser.<lambda>>, 'OP_PLUS': <function BaseFilteringTreeTraverser.<lambda>>, 'OP_TIMES': <function BaseFilteringTreeTraverser.<lambda>>}

Definitions of all mathematical binary operations.

decorate_function(name, decorator)[source]

Decorate function with given name with given decorator.

Parameters
  • name (str) – Name of the function.

  • decorator (callable) – Decorator callback.

evaluate_binop_comparison(operation, left, right, **kwargs)[source]

Evaluate given comparison binary operation with given operands.

evaluate_binop_logical(operation, left, right, **kwargs)[source]

Evaluate given logical binary operation with given operands.

evaluate_binop_math(operation, left, right, **kwargs)[source]

Evaluate given mathematical binary operation with given operands.

evaluate_unop(operation, right, **kwargs)[source]

Evaluate given unary operation with given operand.

function(rule, args, **kwargs)[source]

Implementation of pynspect.traversers.RuleTreeTraverser.function() interface.

register_function(name, callback)[source]

Register given callback as filtering rule function with given name.

Parameters
  • name (str) – Name of the function.

  • callback (callable) – Function callback.

unops = {'OP_EXISTS': <function BaseFilteringTreeTraverser.<lambda>>, 'OP_NOT': <function BaseFilteringTreeTraverser.<lambda>>}

Definitions of all unary operations.

class pynspect.traversers.BaseRuleTreeTraverser[source]

Bases: object

Base class and interface definition for all rule tree traversers. This is a mandatory interface that is required for an object to be able to traverse through given pynspect.rules.Rule tree.

binary_operation_comparison(rule, left, right, **kwargs)[source]

Callback method for rule tree traversing. Will be called at proper time from pynspect.rules.ComparisonBinOpRule.traverse method.

Parameters
  • rule (pynspect.rules.Rule) – Reference to rule.

  • left – Left operand for operation.

  • right – right operand for operation.

  • kwargs (dict) – Optional callback arguments.

binary_operation_logical(rule, left, right, **kwargs)[source]

Callback method for rule tree traversing. Will be called at proper time from pynspect.rules.LogicalBinOpRule.traverse method.

Parameters
  • rule (pynspect.rules.Rule) – Reference to rule.

  • left – Left operand for operation.

  • right – right operand for operation.

  • kwargs (dict) – Optional callback arguments.

binary_operation_math(rule, left, right, **kwargs)[source]

Callback method for rule tree traversing. Will be called at proper time from pynspect.rules.MathBinOpRule.traverse method.

Parameters
  • rule (pynspect.rules.Rule) – Reference to rule.

  • left – Left operand for operation.

  • right – right operand for operation.

  • kwargs (dict) – Optional callback arguments.

constant(rule, **kwargs)[source]

Callback method for rule tree traversing. Will be called at proper time from pynspect.rules.ConstantRule.traverse method.

Parameters
  • rule (pynspect.rules.Rule) – Reference to rule.

  • kwargs (dict) – Optional callback arguments.

datetime(rule, **kwargs)[source]

Callback method for rule tree traversing. Will be called at proper time from pynspect.rules.DatetimeRule.traverse method.

Parameters
  • rule (pynspect.rules.Rule) – Reference to rule.

  • kwargs (dict) – Optional callback arguments.

float(rule, **kwargs)[source]

Callback method for rule tree traversing. Will be called at proper time from pynspect.rules.FloatRule.traverse method.

Parameters
  • rule (pynspect.rules.Rule) – Reference to rule.

  • kwargs (dict) – Optional callback arguments.

function(rule, args, **kwargs)[source]

Callback method for rule tree traversing. Will be called at proper time from pynspect.rules.FunctionRule.traverse method.

Parameters
  • rule (pynspect.rules.Rule) – Reference to rule.

  • args – Optional function arguments.

  • kwargs (dict) – Optional callback arguments.

integer(rule, **kwargs)[source]

Callback method for rule tree traversing. Will be called at proper time from pynspect.rules.IntegerRule.traverse method.

Parameters
  • rule (pynspect.rules.Rule) – Reference to rule.

  • kwargs (dict) – Optional callback arguments.

ipv4(rule, **kwargs)[source]

Callback method for rule tree traversing. Will be called at proper time from pynspect.rules.IPV4Rule.traverse method.

Parameters
  • rule (pynspect.rules.Rule) – Reference to rule.

  • kwargs (dict) – Optional callback arguments.

ipv6(rule, **kwargs)[source]

Callback method for rule tree traversing. Will be called at proper time from pynspect.rules.IPV6Rule.traverse method.

Parameters
  • rule (pynspect.rules.Rule) – Reference to rule.

  • kwargs (dict) – Optional callback arguments.

list(rule, **kwargs)[source]

Callback method for rule tree traversing. Will be called at proper time from pynspect.rules.ListRule.traverse method.

Parameters
  • rule (pynspect.rules.Rule) – Reference to rule.

  • kwargs (dict) – Optional callback arguments.

timedelta(rule, **kwargs)[source]

Callback method for rule tree traversing. Will be called at proper time from pynspect.rules.TimedeltaRule.traverse method.

Parameters
  • rule (pynspect.rules.Rule) – Reference to rule.

  • kwargs (dict) – Optional callback arguments.

unary_operation(rule, right, **kwargs)[source]

Callback method for rule tree traversing. Will be called at proper time from pynspect.rules.UnaryOperationRule.traverse method.

Parameters
  • rule (pynspect.rules.Rule) – Reference to rule.

  • right – right operand for operation.

  • kwargs (dict) – Optional callback arguments.

variable(rule, **kwargs)[source]

Callback method for rule tree traversing. Will be called at proper time from pynspect.rules.VariableRule.traverse method.

Parameters
  • rule (pynspect.rules.Rule) – Reference to rule.

  • kwargs (dict) – Optional callback arguments.

class pynspect.traversers.HTMLTreeTraverser[source]

Bases: pynspect.traversers.BaseRuleTreeTraverser

Demonstation of simple rule tree traverser - HTML printing traverser.

binary_operation_comparison(rule, left, right, **kwargs)[source]

Callback method for rule tree traversing. Will be called at proper time from pynspect.rules.ComparisonBinOpRule.traverse method.

Parameters
  • rule (pynspect.rules.Rule) – Reference to rule.

  • left – Left operand for operation.

  • right – right operand for operation.

  • kwargs (dict) – Optional callback arguments.

binary_operation_logical(rule, left, right, **kwargs)[source]

Callback method for rule tree traversing. Will be called at proper time from pynspect.rules.LogicalBinOpRule.traverse method.

Parameters
  • rule (pynspect.rules.Rule) – Reference to rule.

  • left – Left operand for operation.

  • right – right operand for operation.

  • kwargs (dict) – Optional callback arguments.

binary_operation_math(rule, left, right, **kwargs)[source]

Callback method for rule tree traversing. Will be called at proper time from pynspect.rules.MathBinOpRule.traverse method.

Parameters
  • rule (pynspect.rules.Rule) – Reference to rule.

  • left – Left operand for operation.

  • right – right operand for operation.

  • kwargs (dict) – Optional callback arguments.

constant(rule, **kwargs)[source]

Callback method for rule tree traversing. Will be called at proper time from pynspect.rules.ConstantRule.traverse method.

Parameters
  • rule (pynspect.rules.Rule) – Reference to rule.

  • kwargs (dict) – Optional callback arguments.

datetime(rule, **kwargs)[source]

Callback method for rule tree traversing. Will be called at proper time from pynspect.rules.DatetimeRule.traverse method.

Parameters
  • rule (pynspect.rules.Rule) – Reference to rule.

  • kwargs (dict) – Optional callback arguments.

float(rule, **kwargs)[source]

Callback method for rule tree traversing. Will be called at proper time from pynspect.rules.FloatRule.traverse method.

Parameters
  • rule (pynspect.rules.Rule) – Reference to rule.

  • kwargs (dict) – Optional callback arguments.

function(rule, args, **kwargs)[source]

Callback method for rule tree traversing. Will be called at proper time from pynspect.rules.FunctionRule.traverse method.

Parameters
  • rule (pynspect.rules.Rule) – Reference to rule.

  • args – Optional function arguments.

  • kwargs (dict) – Optional callback arguments.

integer(rule, **kwargs)[source]

Callback method for rule tree traversing. Will be called at proper time from pynspect.rules.IntegerRule.traverse method.

Parameters
  • rule (pynspect.rules.Rule) – Reference to rule.

  • kwargs (dict) – Optional callback arguments.

ipv4(rule, **kwargs)[source]

Callback method for rule tree traversing. Will be called at proper time from pynspect.rules.IPV4Rule.traverse method.

Parameters
  • rule (pynspect.rules.Rule) – Reference to rule.

  • kwargs (dict) – Optional callback arguments.

ipv6(rule, **kwargs)[source]

Callback method for rule tree traversing. Will be called at proper time from pynspect.rules.IPV6Rule.traverse method.

Parameters
  • rule (pynspect.rules.Rule) – Reference to rule.

  • kwargs (dict) – Optional callback arguments.

list(rule, **kwargs)[source]

Callback method for rule tree traversing. Will be called at proper time from pynspect.rules.ListRule.traverse method.

Parameters
  • rule (pynspect.rules.Rule) – Reference to rule.

  • kwargs (dict) – Optional callback arguments.

timedelta(rule, **kwargs)[source]

Callback method for rule tree traversing. Will be called at proper time from pynspect.rules.TimedeltaRule.traverse method.

Parameters
  • rule (pynspect.rules.Rule) – Reference to rule.

  • kwargs (dict) – Optional callback arguments.

unary_operation(rule, right, **kwargs)[source]

Callback method for rule tree traversing. Will be called at proper time from pynspect.rules.UnaryOperationRule.traverse method.

Parameters
  • rule (pynspect.rules.Rule) – Reference to rule.

  • right – right operand for operation.

  • kwargs (dict) – Optional callback arguments.

variable(rule, **kwargs)[source]

Callback method for rule tree traversing. Will be called at proper time from pynspect.rules.VariableRule.traverse method.

Parameters
  • rule (pynspect.rules.Rule) – Reference to rule.

  • kwargs (dict) – Optional callback arguments.

class pynspect.traversers.ListIP(iterable=None)[source]

Bases: collections.abc.MutableSequence

Special list implementation designed to provide special handling of ‘IN’ operator. When item is being compared using ‘IN’ operator with this list, the IN operation is propagated down to each of the items in the list.

count(value) → integer -- return number of occurrences of value[source]
index(value[, start[, stop]]) → integer -- return first index of value.[source]

Raises ValueError if the value is not present.

insert(idx, val)[source]

S.insert(index, value) – insert value before index

pop([index]) → item -- remove and return item at index (default last).[source]

Raise IndexError if list is empty or index is out of range.

reverse()[source]

S.reverse() – reverse IN PLACE

class pynspect.traversers.PrintingTreeTraverser[source]

Bases: pynspect.traversers.BaseRuleTreeTraverser

Demonstation of simple rule tree traverser - printing traverser.

binary_operation_comparison(rule, left, right, **kwargs)[source]

Callback method for rule tree traversing. Will be called at proper time from pynspect.rules.ComparisonBinOpRule.traverse method.

Parameters
  • rule (pynspect.rules.Rule) – Reference to rule.

  • left – Left operand for operation.

  • right – right operand for operation.

  • kwargs (dict) – Optional callback arguments.

binary_operation_logical(rule, left, right, **kwargs)[source]

Callback method for rule tree traversing. Will be called at proper time from pynspect.rules.LogicalBinOpRule.traverse method.

Parameters
  • rule (pynspect.rules.Rule) – Reference to rule.

  • left – Left operand for operation.

  • right – right operand for operation.

  • kwargs (dict) – Optional callback arguments.

binary_operation_math(rule, left, right, **kwargs)[source]

Callback method for rule tree traversing. Will be called at proper time from pynspect.rules.MathBinOpRule.traverse method.

Parameters
  • rule (pynspect.rules.Rule) – Reference to rule.

  • left – Left operand for operation.

  • right – right operand for operation.

  • kwargs (dict) – Optional callback arguments.

constant(rule, **kwargs)[source]

Callback method for rule tree traversing. Will be called at proper time from pynspect.rules.ConstantRule.traverse method.

Parameters
  • rule (pynspect.rules.Rule) – Reference to rule.

  • kwargs (dict) – Optional callback arguments.

datetime(rule, **kwargs)[source]

Callback method for rule tree traversing. Will be called at proper time from pynspect.rules.DatetimeRule.traverse method.

Parameters
  • rule (pynspect.rules.Rule) – Reference to rule.

  • kwargs (dict) – Optional callback arguments.

float(rule, **kwargs)[source]

Callback method for rule tree traversing. Will be called at proper time from pynspect.rules.FloatRule.traverse method.

Parameters
  • rule (pynspect.rules.Rule) – Reference to rule.

  • kwargs (dict) – Optional callback arguments.

function(rule, args, **kwargs)[source]

Callback method for rule tree traversing. Will be called at proper time from pynspect.rules.FunctionRule.traverse method.

Parameters
  • rule (pynspect.rules.Rule) – Reference to rule.

  • args – Optional function arguments.

  • kwargs (dict) – Optional callback arguments.

integer(rule, **kwargs)[source]

Callback method for rule tree traversing. Will be called at proper time from pynspect.rules.IntegerRule.traverse method.

Parameters
  • rule (pynspect.rules.Rule) – Reference to rule.

  • kwargs (dict) – Optional callback arguments.

ipv4(rule, **kwargs)[source]

Callback method for rule tree traversing. Will be called at proper time from pynspect.rules.IPV4Rule.traverse method.

Parameters
  • rule (pynspect.rules.Rule) – Reference to rule.

  • kwargs (dict) – Optional callback arguments.

ipv6(rule, **kwargs)[source]

Callback method for rule tree traversing. Will be called at proper time from pynspect.rules.IPV6Rule.traverse method.

Parameters
  • rule (pynspect.rules.Rule) – Reference to rule.

  • kwargs (dict) – Optional callback arguments.

list(rule, **kwargs)[source]

Callback method for rule tree traversing. Will be called at proper time from pynspect.rules.ListRule.traverse method.

Parameters
  • rule (pynspect.rules.Rule) – Reference to rule.

  • kwargs (dict) – Optional callback arguments.

timedelta(rule, **kwargs)[source]

Callback method for rule tree traversing. Will be called at proper time from pynspect.rules.TimedeltaRule.traverse method.

Parameters
  • rule (pynspect.rules.Rule) – Reference to rule.

  • kwargs (dict) – Optional callback arguments.

unary_operation(rule, right, **kwargs)[source]

Callback method for rule tree traversing. Will be called at proper time from pynspect.rules.UnaryOperationRule.traverse method.

Parameters
  • rule (pynspect.rules.Rule) – Reference to rule.

  • right – right operand for operation.

  • kwargs (dict) – Optional callback arguments.

variable(rule, **kwargs)[source]

Callback method for rule tree traversing. Will be called at proper time from pynspect.rules.VariableRule.traverse method.

Parameters
  • rule (pynspect.rules.Rule) – Reference to rule.

  • kwargs (dict) – Optional callback arguments.