Skip to content

Commit ed8211e

Browse files
committed
Add simple example to parsing apidocs
1 parent 8003684 commit ed8211e

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

src/robot/parsing/__init__.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,34 @@
4040
4141
::
4242
43-
TODO
43+
import ast
44+
import sys
45+
46+
from robot.api import get_model
47+
48+
49+
class TestNamePrinter(ast.NodeVisitor):
50+
51+
def visit_File(self, node):
52+
print(f"File '{node.source}' has following tests:")
53+
# Must call `generic_visit` to visit also child nodes.
54+
self.generic_visit(node)
55+
56+
def visit_KeywordSection(self, node):
57+
# Overriding a visitor method without calling `generic_visit`
58+
# prevents child nodes being visited. In this case it means
59+
# `visit_Name` is not called with nodes in the keyword section.
60+
pass
61+
62+
def visit_Name(self, node):
63+
print(f"- {node.name}")
64+
65+
66+
model = get_model(sys.argv[1])
67+
TestNamePrinter().visit(model)
68+
69+
70+
TODO: Add example modifying model.
4471
"""
4572

4673
from .builders import get_model, get_resource_model

0 commit comments

Comments
 (0)