pypuppetdbquery documentation¶
Contents:
pypuppetdbquery package¶
-
pypuppetdbquery.
parse
(s, json=True, lex_options=None, yacc_options=None)[source]¶ Parse a PuppetDBQuery-style query and transform it into a PuppetDB “AST” query.
This function is intented to be the primary entry point for this package. It wraps up all the various components of this package into an easy to consume format. The output of this function is designed to be passed directly into
pypuppetdb
.For examples of the query syntax see puppet-puppetdbquery and node-puppetdbquery by Erik Dalén.
Parameters: - s (str) – The query to parse and transform
- json (bool) – Whether to JSON-encode the PuppetDB AST result
- lex_options (dict) – Options passed to
ply.lex.lex()
- yacc_options (dict) – Options passed to
ply.yacc.yacc()
Submodules¶
pypuppetdbquery.ast module¶
Abstract Syntax Tree (AST) for the PuppetDBQuery language. These simple classes
are used by the pypuppetdbquery.parser.Parser
in order to represent
the parsed syntax tree.
-
class
pypuppetdbquery.ast.
BinaryExpression
(left, right)[source]¶ Bases:
pypuppetdbquery.ast.Node
-
class
pypuppetdbquery.ast.
Date
(value)[source]¶ Bases:
pypuppetdbquery.ast.Literal
-
class
pypuppetdbquery.ast.
Expression
[source]¶ Bases:
pypuppetdbquery.ast.Node
-
class
pypuppetdbquery.ast.
Identifier
(name)[source]¶ Bases:
pypuppetdbquery.ast.Node
-
class
pypuppetdbquery.ast.
IdentifierPath
(components)[source]¶ Bases:
pypuppetdbquery.ast.Node
-
class
pypuppetdbquery.ast.
Literal
(value)[source]¶ Bases:
pypuppetdbquery.ast.Node
-
class
pypuppetdbquery.ast.
Query
(expression)[source]¶ Bases:
pypuppetdbquery.ast.Node
-
class
pypuppetdbquery.ast.
Subquery
(endpoint, expression)[source]¶ Bases:
pypuppetdbquery.ast.Node
-
class
pypuppetdbquery.ast.
UnaryExpression
(expression)[source]¶ Bases:
pypuppetdbquery.ast.Node
pypuppetdbquery.evaluator module¶
-
class
pypuppetdbquery.evaluator.
Evaluator
[source]¶ Bases:
object
Converts a
pypuppetdbquery.ast
Abstract Syntax Tree into a PuppetDB native AST query.-
DECAMEL_RE
= re.compile('(?!^)([A-Z]+)')¶ Regular expression used when converting CamelCase class names to underscore_separated names.
-
evaluate
(ast, mode='nodes')[source]¶ Process a parsed PuppetDBQuery AST and return a PuppetDB AST.
The resulting PuppetDB AST is a native Python list. It will need converting to JSON (using
json.dumps()
) before it can be used with PuppetDB.Parameters: - ast (pypuppetdbquery.ast.Query) – Root of the AST to evaulate
- mode (str) – PuppetDB endpoint to target
Returns: PuppetDB AST
Return type: list
-
pypuppetdbquery.lexer module¶
-
exception
pypuppetdbquery.lexer.
LexException
(message, position)[source]¶ Bases:
Exception
Raised for errors encountered during lexing.
Such errors may include unknown tokens or unexpected EOF, for example. The position of the lexer when the error was encountered (the index into the input string) is stored in the position attribute.
-
class
pypuppetdbquery.lexer.
Lexer
(**kwargs)[source]¶ Bases:
object
Lexer for the PuppetDBQuery language.
This class uses
ply.lex.lex()
to implement the lexer (or tokenizer). It is used bypypuppetdbquery.parser.Parser
in order to process queries.The arguments to the constructor are passed directly to
ply.lex.lex()
.Note
Many of the docstrings in this class are used by
ply.lex
to build the lexer. These strings are not particularly useful for generating documentation from, so the built documentation for this class may not be very useful.-
input
(s)[source]¶ Reset and supply input to the lexer.
Tokens then need to be obtained using
token()
or the iterator interface provided by this class.
-
next
()[source]¶ Implementation of
iterator.next()
.Return the next item from the container. If there are no further items, raise the
StopIteration
exception.
-
t_ASTERISK
= '\\*'¶
-
t_AT
= '@'¶
-
t_DOT
= '\\.'¶
-
t_EQUALS
= '='¶
-
t_EXPORTED
= '@@'¶
-
t_GREATERTHAN
= '>'¶
-
t_GREATERTHANEQ
= '>='¶
-
t_HASH
= '[#]'¶
-
t_LBRACE
= '{'¶
-
t_LBRACK
= '\\['¶
-
t_LESSTHAN
= '<'¶
-
t_LESSTHANEQ
= '<='¶
-
t_LPAREN
= '\\('¶
-
t_MATCH
= '~'¶
-
t_NOTEQUALS
= '!='¶
-
t_NOTMATCH
= '!~'¶
-
t_RBRACE
= '}'¶
-
t_RBRACK
= '\\]'¶
-
t_RPAREN
= '\\)'¶
-
t_ignore
= ' \t\n\r\x0c\x0b'¶
-
tokens
= ('LPAREN', 'RPAREN', 'LBRACK', 'RBRACK', 'LBRACE', 'RBRACE', 'EQUALS', 'NOTEQUALS', 'MATCH', 'NOTMATCH', 'LESSTHANEQ', 'LESSTHAN', 'GREATERTHANEQ', 'GREATERTHAN', 'ASTERISK', 'HASH', 'DOT', 'NOT', 'AND', 'OR', 'BOOLEAN', 'NUMBER', 'STRING', 'FLOAT', 'EXPORTED', 'AT')¶ List of token names handled by the lexer.
-
pypuppetdbquery.parser module¶
-
exception
pypuppetdbquery.parser.
ParseException
(message, position)[source]¶ Bases:
Exception
Raised for errors encountered during parsing.
The position of the lexer when the error was encountered (the index into the input string) is stored in the position attribute.
-
class
pypuppetdbquery.parser.
Parser
(lex_options=None, yacc_options=None)[source]¶ Bases:
object
Parser for the PuppetDBQuery language.
This class uses
ply.yacc.yacc()
to implement the parser. In concert withpypuppetdbquery.lexer.Lexer
, it produces an Abstract Syntax Tree (AST) as declared inpypuppetdbquery.ast
.Parameters: - lex_options (dict) – Passed as keyword arguments to
pypuppetdbquery.lexer.Lexer
- yacc_options (dict) – Passed as keyword arguments to
ply.yacc.yacc()
Note
Many of the docstrings in this class are used by
ply.yacc
to build the parser. These strings are not particularly useful for generating documentation from, so the built documentation for this class may not be very useful.-
p_comparison_op
(p)[source]¶ - comparison_op : MATCH
- NOTMATCHEQUALSNOTEQUALSGREATERTHANGREATERTHANEQLESSTHANLESSTHANEQ
-
p_resource_expr_exported_param
(p)[source]¶ resource_expr : EXPORTED string LBRACK identifier RBRACK block_expr
-
parse
(text, debug=0)[source]¶ Parse the input string and return an AST.
Parameters: - text (str) – The query to parse
- debug (bool) – Output detailed information during the parsing process
Returns: An Abstract Syntax Tree
Return type:
-
precedence
= (('left', 'OR'), ('left', 'AND'), ('left', 'EQUALS', 'MATCH', 'LESSTHAN', 'GREATERTHAN'), ('right', 'NOT'))¶ Precedence rules in lowest to highest order
-
start
= 'query'¶ Non-terminal to use as the starting grammar symbol
- lex_options (dict) – Passed as keyword arguments to
Examples¶
Basic query for nodes using pypuppetdb
:
import pypuppetdb
import pypuppetdbquery
pdb = pypuppetdb.connect()
pdb_ast = pypuppetdbquery.parse(
'(processorcount=4 or processorcount=8) and kernel=Linux')
for node in pdb.nodes(query=pdb_ast):
print(node)
Test Suite¶
-
class
test_frontend.
TestFrontend
(methodName='runTest')[source]¶ Bases:
unittest.case.TestCase
Test cases targetting
pypuppetdbquery
, and particularlypypuppetdbquery.parse()
.
-
class
test_integration.
TestIntegration
(methodName='runTest')[source]¶ Bases:
unittest.case.TestCase
Test cases for the integrated combination of
pypuppetdbquery.lexer.Lexer
,pypuppetdbquery.parser.Parser
, andpypuppetdbquery.evaluator.Evaluator
.
-
class
test_lexer.
TestLexer
(methodName='runTest')[source]¶ Bases:
unittest.case.TestCase
Test cases for
pypuppetdbquery.lexer.Lexer
.
-
class
test_parser.
TestParster
(methodName='runTest')[source]¶ Bases:
unittest.case.TestCase
Test cases for
pypuppetdbquery.parser.Parser
.