Query

Query handling: factory and builder.

class cumin.query.Query(config)[source]

Bases: cumin.backends.BaseQueryAggregator

Cumin main query class.

It has multi-query capability and allow to use a default backend, if set, without additional syntax. If a default_backend is set in the configuration, it will try to execute the query string first with the default backend and only if the query is not parsable with that backend it will try to execute it with the multi-query grammar.

When a query is executed, a ClusterShell.NodeSet.NodeSet with the FQDN of the matched hosts is returned.

Examples

>>> import cumin
>>> from cumin.query import Query
>>> config = cumin.Config()
>>> hosts = Query(config).execute(query_string)

Query constructor, initialize the registered backends.

Parameters

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

execute(query_string)[source]

Override parent class execute method to implement the multi-query capability.

Parameters

according to parent cumin.backends.BaseQueryAggregator.execute().

Returns

with the FQDNs of the matching hosts.

Return type

ClusterShell.NodeSet.NodeSet

Raises

cumin.backends.InvalidQueryError -- if unable to parse the query.

_query_default_backend(query_string)[source]

Execute the query with the default backend, according to the configuration.

Parameters

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

Returns

with the FQDNs of the matching hosts.

Return type

ClusterShell.NodeSet.NodeSet

Raises

cumin.backends.InvalidQueryError -- if unable to get the default backend from the registered backends.

_parse_token(token)[source]

Concrete implementation of parent abstract method.

Parameters

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

Raises

cumin.backends.InvalidQueryError -- on internal parsing error.

_replace_alias(token_dict)[source]

Replace any alias in the query in a recursive way, alias can reference other aliases.

Parameters

token_dict (dict) -- the dictionary of the parsed token returned by the grammar parsing.

Returns

True if a replacement was made, :py:data`False` otherwise.

Return type

bool

Raises

cumin.backends.InvalidQueryError -- if unable to replace an alias.