Backends

Abstract backend.

exception cumin.backends.InvalidQueryError[source]

Bases: cumin.CuminError

Custom exception class for invalid queries.

class cumin.backends.BaseQuery(config)[source]

Bases: object

abstract Query abstract class.

All backends query classes must inherit, directly or indirectly, from this one.

Query constructor.

Parameters

config (dict) -- a dictionary with the parsed configuration file.

grammar = NoMatch

derived classes must define their own pyparsing grammar and set this class attribute accordingly.

Type

pyparsing.ParserElement

execute(query_string)[source]

Build and execute the query, return the NodeSet of FQDN hostnames that matches.

Parameters

query_string (str) -- the query string to be parsed and executed.

Returns

with the FQDNs of the matching hosts.

Return type

ClusterShell.NodeSet.NodeSet

abstract _execute()[source]

Execute the already parsed query and return the NodeSet of FQDN hostnames that matches.

Returns

with the FQDNs of the matching hosts.

Return type

ClusterShell.NodeSet.NodeSet

abstract _parse_token(token)[source]

Recursively interpret the tokens returned by the grammar parsing.

Parameters

token (pyparsing.ParseResults) -- a single token returned by the grammar parsing.

_build(query_string)[source]

Parse the query string according to the grammar and build the query for later execution.

Parameters

query_string (str) -- the query string to be parsed.

class cumin.backends.BaseQueryAggregator(config)[source]

Bases: cumin.backends.BaseQuery

abstract Query aggregator abstract class.

Add to cumin.backends.BaseQuery the capability of aggregating query subgroups and sub tokens into a unified result using common boolean operators for sets: and, or, and not and xor. The class has a stack-like structure that must be populated by the derived classes while building the query. On execution the stack is traversed and the results are aggreagated together based on subgroups and boolean operators.

Query aggregator constructor, initialize the stack.

Parameters

according to parent cumin.backends.BaseQuery.__init__().

_build(query_string)[source]

Override parent method to reset the stack and log it.

Parameters

according to parent cumin.backends.BaseQuery._build().

_execute()[source]

Concrete implementation of parent abstract method.

Parameters

according to parent cumin.backends.BaseQuery._execute().

_open_subgroup()[source]

Handle subgroup opening.

_close_subgroup()[source]

Handle subgroup closing.

abstract _parse_token(token)[source]

Re-define abstract method from parent abstract class.

Parameters

according to parent cumin.backends.BaseQuery._parse_token().

static _get_stack_element()[source]

Return an empty stack element.

Returns

the dictionary with an empty stack element.

Return type

dict

_loop_stack(hosts, stack_element)[source]

Loop the stack generated while parsing the query and aggregate the results.

Parameters
  • hosts (ClusterShell.NodeSet.NodeSet) -- the hosts to be updated with the current stack element results. This object is updated in place by reference.

  • stack_element (dict) -- the stack element to iterate.

_aggregate_hosts(hosts, element_hosts, bool_operator)[source]

Aggregate hosts according to their boolean operator.

Parameters
  • hosts (ClusterShell.NodeSet.NodeSet) -- the hosts to update with the results in element_hosts according to the bool_operator. This object is updated in place by reference.

  • element_hosts (ClusterShell.NodeSet.NodeSet) -- the additional hosts to aggregate to the results based on the bool_operator.

  • bool_operator (str, None) -- the boolean operator to apply while aggregating the two NodeSet. It must be None when adding the first hosts.

Available backends