Skip to content

Commit 5eec813

Browse files
committed
Add lots of docstrings for Sphinx
1 parent 73eabb5 commit 5eec813

File tree

5 files changed

+114
-6
lines changed

5 files changed

+114
-6
lines changed

pypuppetdbquery/__init__.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,28 @@
2020
from .parser import Parser
2121

2222

23-
def parse(s, json=True, parser_opts=None, evaluator_opts=None):
24-
parser = Parser(**(parser_opts or {}))
25-
evaluator = Evaluator(**(evaluator_opts or {}))
23+
def parse(s, json=True, lex_options=None, yacc_options=None):
24+
"""
25+
Parse a PuppetDBQuery-style query and transform it into a PuppetDB "AST"
26+
query.
27+
28+
This function is intented to be the primary entry point for this package.
29+
It wraps up all the various components of this package into an easy to
30+
consume format. The output of this function is designed to be passed
31+
directly into :mod:`pypuppetdb`.
32+
33+
For examples of the query syntax see `puppet-puppetdbquery
34+
<https://github.com/dalen/puppet-puppetdbquery>`_ and `node-puppetdbquery
35+
<https://github.com/dalen/node-puppetdbquery>`_ by `Erik Dalén
36+
<https://github.com/dalen>`_.
37+
38+
:param str s: The query to parse and transform
39+
:param bool json: Whether to JSON-encode the PuppetDB AST result
40+
:param dict lex_options: Options passed to :func:`ply.lex.lex`
41+
:param dict yacc_options: Options passed to :func:`ply.yacc.yacc`
42+
"""
43+
parser = Parser(lex_options=lex_options, yacc_options=yacc_options)
44+
evaluator = Evaluator()
2645

2746
ast = parser.parse(s)
2847
raw = evaluator.evaluate(ast)

pypuppetdbquery/ast.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1515
# See the License for the specific language governing permissions and
1616
# limitations under the License.
17+
"""
18+
Abstract Syntax Tree (AST) for the PuppetDBQuery language. These simple classes
19+
are used by the :class:`pypuppetdbquery.parser.Parser` in order to represent
20+
the parsed syntax tree.
21+
"""
1722

1823
import inspect
1924

pypuppetdbquery/evaluator.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,28 @@
2222

2323

2424
class Evaluator(object):
25+
"""
26+
Converts a :mod:`pypuppetdbquery.ast` Abstract Syntax Tree into a PuppetDB
27+
native AST query.
28+
"""
29+
30+
#: Regular expression used when converting CamelCase class names to
31+
#: underscore_separated names.
2532
DECAMEL_RE = re.compile(r'(?!^)([A-Z]+)')
2633

2734
def evaluate(self, ast, mode='nodes'):
35+
"""
36+
Process a parsed PuppetDBQuery AST and return a PuppetDB AST.
37+
38+
The resulting PuppetDB AST is a native Python list. It will need
39+
converting to JSON (using :func:`json.dumps`) before it can be used
40+
with PuppetDB.
41+
42+
:param pypuppetdbquery.ast.Query ast: Root of the AST to evaulate
43+
:param str mode: PuppetDB endpoint to target
44+
:return: PuppetDB AST
45+
:rtype: list
46+
"""
2847
return self._visit(ast, [mode])
2948

3049
def _subquery(self, from_mode, to_mode, query):

pypuppetdbquery/lexer.py

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,37 +19,71 @@
1919

2020

2121
class LexException(Exception):
22+
"""
23+
Raised for errors encountered during lexing.
24+
25+
Such errors may include unknown tokens or unexpected EOF, for example. The
26+
position of the lexer when the error was encountered (the index into the
27+
input string) is stored in the `position` attribute.
28+
"""
2229
def __init__(self, message, position):
2330
super(LexException, self).__init__(message)
2431
self.position = position
2532

2633

2734
class Lexer(object):
2835
"""
29-
Lexer for the PuppetDB query language.
36+
Lexer for the PuppetDBQuery language.
37+
38+
This class uses :func:`ply.lex.lex` to implement the lexer (or tokenizer).
39+
It is used by :class:`pypuppetdbquery.parser.Parser` in order to process
40+
queries.
41+
42+
The arguments to the constructor are passed directly to
43+
:func:`ply.lex.lex`.
44+
45+
.. note:: Many of the docstrings in this class are used by
46+
:mod:`ply.lex` to build the lexer. These strings are not particularly
47+
useful for generating documentation from, so the built documentation for
48+
this class may not be very useful.
3049
"""
3150
def __init__(self, **kwargs):
3251
super(Lexer, self).__init__()
3352
self.lexer = lex.lex(object=self, **kwargs)
3453

3554
def input(self, s):
55+
"""
56+
Reset and supply input to the lexer.
57+
58+
Tokens then need to be obtained using :meth:`token` or the iterator
59+
interface provided by this class.
60+
"""
3661
self.lexer.input(s)
3762

3863
def token(self):
64+
"""
65+
Obtain one token from the input.
66+
"""
3967
return self.lexer.token()
4068

4169
def __iter__(self):
4270
return self
4371

4472
def next(self):
73+
"""
74+
Implementation of :meth:`iterator.next`.
75+
76+
Return the next item from the container. If there are no further items,
77+
raise the :exc:`StopIteration` exception.
78+
"""
4579
t = self.token()
4680
if t is None:
4781
raise StopIteration()
4882
return t
4983

5084
__next__ = next
5185

52-
# List of token names.
86+
#: List of token names handled by the lexer.
5387
tokens = (
5488
'LPAREN',
5589
'RPAREN',

pypuppetdbquery/parser.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,34 @@
2222

2323

2424
class ParseException(Exception):
25+
"""
26+
Raised for errors encountered during parsing.
27+
28+
The position of the lexer when the error was encountered (the index into
29+
the input string) is stored in the `position` attribute.
30+
"""
2531
def __init__(self, message, position):
2632
super(ParseException, self).__init__(message)
2733
self.position = position
2834

2935

3036
class Parser(object):
3137
"""
32-
Parser for the PuppetDB query language.
38+
Parser for the PuppetDBQuery language.
39+
40+
This class uses :func:`ply.yacc.yacc` to implement the parser. In concert
41+
with :class:`pypuppetdbquery.lexer.Lexer`, it produces an Abstract Syntax
42+
Tree (AST) as declared in :mod:`pypuppetdbquery.ast`.
43+
44+
:param dict lex_options: Passed as keyword arguments to
45+
:class:`pypuppetdbquery.lexer.Lexer`
46+
:param dict yacc_options: Passed as keyword arguments to
47+
:func:`ply.yacc.yacc`
48+
49+
.. note:: Many of the docstrings in this class are used by :mod:`ply.yacc`
50+
to build the parser. These strings are not particularly useful for
51+
generating documentation from, so the built documentation for this class
52+
may not be very useful.
3353
"""
3454
def __init__(self, lex_options=None, yacc_options=None):
3555
super(Parser, self).__init__()
@@ -48,8 +68,18 @@ def __init__(self, lex_options=None, yacc_options=None):
4868
self.parser = yacc.yacc(module=self, **yacc_options)
4969

5070
def parse(self, text, debug=0):
71+
"""
72+
Parse the input string and return an AST.
73+
74+
:param str text: The query to parse
75+
:param bool debug: Output detailed information during the parsing
76+
process
77+
:return: An Abstract Syntax Tree
78+
:rtype: pypuppetdbquery.ast.Query
79+
"""
5180
return self.parser.parse(input=text, lexer=self.lexer, debug=debug)
5281

82+
#: Non-terminal to use as the starting grammar symbol
5383
start = 'query'
5484

5585
def p_query(self, p):
@@ -189,6 +219,7 @@ def p_empty(self, p):
189219
'empty :'
190220
pass
191221

222+
#: Precedence rules in lowest to highest order
192223
precedence = (
193224
('left', 'OR'),
194225
('left', 'AND'),

0 commit comments

Comments
 (0)