pynspect.compilers module

This module provides tools for compiling filtering rule trees into data structures more appropriate for actual filtering of IDEA messages.

There are following main tools in this package:

  • IDEAFilterCompiler

    Filter compiler, that ensures appropriate data types for correct variable comparison evaluation in IDEA messages.

class pynspect.compilers.ConversionRule(conversion, rule)[source]

Bases: Rule

Custom rule for delayed rule conversions. Can be used by the compiler to wrap given rule tree into rule, that can perform arbitrary conversion or manipulation with the result of traversal of that rule tree before returning the result.

traverse(traverser, **kwargs)[source]

Implementation of mandatory interface for traversing the whole rule tree. This method will call the traverse method of child rule tree and then perform arbitrary conversion of the result before returning it back. The optional kwargs are passed down to traverser callback as additional arguments and can be used to provide additional data or context.

Parameters
  • traverser (pynspect.rules.RuleTreeTraverser) – Traverser object providing appropriate interface.

  • kwargs (dict) – Additional optional keyword arguments to be passed down to traverser callback.

class pynspect.compilers.IDEAFilterCompiler[source]

Bases: BaseFilteringTreeTraverser

Rule tree traverser implementing IDEA filter compilation algorithm.

Following example demonstrates DataObjectFilter usage in conjuction with PynspectFilterParser:

>>> msg_idea = lite.Idea(test_msg)
>>> flt = DataObjectFilter()
>>> cpl = IDEAFilterCompiler()
>>> psr = PynspectFilterParser()
>>> psr.build()
>>> rule = psr.parse('ID like "e214d2d9"')
>>> rule = cpl.compile(rule)
>>> result = flt.filter(rule, test_msg)
binary_operation_comparison(rule, left, right, **kwargs)[source]

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

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

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

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

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

compile(rule)[source]

Compile given filtering rule into format appropriate for processing IDEA messages.

Parameters

rule (pynspect.rules.Rule) – filtering rule to be compiled

Returns

compiled filtering rule

Return type

pynspect.rules.Rule

constant(rule, **kwargs)[source]

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

datetime(rule, **kwargs)[source]

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

float(rule, **kwargs)[source]

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

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

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

integer(rule, **kwargs)[source]

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

ipv4(rule, **kwargs)[source]

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

ipv6(rule, **kwargs)[source]

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

list(rule, **kwargs)[source]

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

register_function_compilation(func, compilation_cbk, listclass)[source]

Register given compilation method for given function.

Parameters
  • path (str) – Function name.

  • compilation_cbk (callable) – Compilation callback to be called.

  • listclass (class) – List class to use for lists.

register_variable_compilation(path, compilation_cbk, listclass)[source]

Register given compilation method for variable on given path.

Parameters
  • path (str) – JPath for given variable.

  • compilation_cbk (callable) – Compilation callback to be called.

  • listclass (class) – List class to use for lists.

timedelta(rule, **kwargs)[source]

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

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

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

variable(rule, **kwargs)[source]

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

class pynspect.compilers.IPListRule(rules)[source]

Bases: ListRule

Custom rule for lists of IP addresses/ranges/networks, that need special handling in comparison operations.

values()[source]

Return true values of the rules in the list.

pynspect.compilers.clean_variable(var)[source]

Remove any array indices from variable name to enable indexing into COMPILATIONS_IDEA_OBJECT_CMP callback dictionary.

This dictionary contains postprocessing callback appropriate for opposing operand of comparison operation for variable on given JPath.

pynspect.compilers.compile_datetime(rule)[source]

Compiler helper method: attempt to compile constant into object representing datetime object to enable relations and thus simple comparisons using Python operators.

pynspect.compilers.compile_ip_v4(rule)[source]

Compiler helper method: attempt to compile constant into object representing IPv4 address to enable relations and thus simple comparisons using Python operators.

pynspect.compilers.compile_ip_v6(rule)[source]

Compiler helper method: attempt to compile constant into object representing IPv6 address to enable relations and thus simple comparisons using Python operators.

pynspect.compilers.compile_timedelta(rule)[source]

Compiler helper method: attempt to compile constant into object representing timedelta object to enable math operations and relations with datetime objects using Python operators.

pynspect.compilers.compile_timeoper(rule)[source]

Compiler helper method: attempt to compile constant into object representing datetime or timedelta object to enable relations and thus simple comparisons using Python operators.