Skip to content

Commit 99852d1

Browse files
committed
rviz_python_tutorial: added sphinx documentation describing tutorial contents.
1 parent 26a850d commit 99852d1

File tree

7 files changed

+367
-80
lines changed

7 files changed

+367
-80
lines changed

rviz_python_tutorial/config.myviz

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
Title: RViz Python Tutorial
12
Panels:
23
[]
34
Visualization Manager:

rviz_python_tutorial/doc-src/conf.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import sys, os
2+
3+
sys.path += [ os.path.abspath( '.' )]
4+
5+
extensions = [ 'sphinx.ext.extlinks',
6+
'tutorialformatter' ]
7+
8+
# The master toctree document.
9+
master_doc = 'index'
10+
11+
# The suffix of source filenames.
12+
source_suffix = '.rst'
13+
14+
project = u'rviz_python_tutorial'
15+
16+
copyright = u'2012, Willow Garage, Inc'
17+
18+
# If true, sectionauthor and moduleauthor directives will be shown in the
19+
# output. They are ignored by default.
20+
show_authors = True
21+
22+
# The name of the Pygments (syntax highlighting) style to use.
23+
pygments_style = 'sphinx'
24+
25+
extlinks = {'srcdir': ('https://github.com/ros-visualization/visualization_tutorials/blob/groovy-devel/rviz_python_tutorial/%s', '')}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
RViz Python Tutorial
2+
====================
3+
4+
Overview
5+
--------
6+
7+
RViz is not just a visualizer application, it is also a Python
8+
library! Much of RViz's functionality can be accessed from Python
9+
code by importing the librviz Python bindings.
10+
11+
This tutorial shows a simple example of creating a visualizer
12+
(rviz::VisualizationFrame) as a child widget along with other Qt
13+
widgets, programmatically loading a config file, then connecting a
14+
slider and some Qt push buttons to change display a display property
15+
and the viewpoint.
16+
17+
The source code for this tutorial is in the rviz_python_tutorial
18+
package. You can check out the source directly or (if you use Ubuntu)
19+
you can just apt-get install the pre-compiled Debian package like so::
20+
21+
sudo apt-get install ros-groovy-visualization-tutorials
22+
23+
The running application looks like this:
24+
25+
.. image:: myviz.png
26+
27+
The Code: myviz.py
28+
------------------
29+
30+
The full text of myviz.py is here: :srcdir:`myviz.py`
31+
32+
.. tutorial-formatter:: ../myviz.py
33+
34+
Running
35+
-------
36+
37+
Just type::
38+
39+
roscd rviz_python_tutorial
40+
./myviz.py
41+
42+
myviz.py loads its config file from the current directory, so you need
43+
to run it from the directory it comes in, or adapt the script to find
44+
the file.
45+
46+
There are more classes in RViz which do not yet have Python bindings.
47+
If you find important ones are missing, please request them as
48+
"enhancement" issues on the RViz project on github.
24.1 KB
Loading
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
"""
2+
tutorialformatter
3+
===========================
4+
5+
This extension provides a directive to include a source code file
6+
in a document, but with certain comments from the file formatted
7+
as regular document text. This allows code for a tutorial to look like:
8+
9+
/// BEGIN_TUTORIAL
10+
/// This next line adds one.
11+
i = i + 1;
12+
/// Then we need to double it.
13+
i = i * 2;
14+
/// END_TUTORIAL
15+
16+
And have it formatted as
17+
18+
This next line adds one.::
19+
i = i + 1;
20+
21+
Then we need to double it.::
22+
i = i * 2;
23+
24+
The special-looking comment character sequence at the start of
25+
each text line can be anything not starting or ending with
26+
whitespace. tutorialformatter starts by scanning the file for the
27+
string BEGIN_TUTORIAL. When it finds it, it takes all the
28+
characters before BEGIN_TUTORIAL on that line, strips whitespace
29+
from the left, and uses that as the text marker. So this would
30+
also be fine:
31+
32+
#My Tutorial# BEGIN_TUTORIAL
33+
#My Tutorial# This next line adds one.
34+
i = i + 1
35+
#My Tutorial# Then we need to double it.
36+
i = i * 2
37+
#My Tutorial# END_TUTORIAL
38+
39+
.. moduleauthor:: Dave Hershberger <[email protected]>
40+
"""
41+
42+
__version__ = '0.1.0'
43+
44+
import os
45+
from docutils.parsers import rst
46+
from docutils.parsers.rst.directives import flag, unchanged
47+
from docutils.statemachine import string2lines
48+
from pygments.lexers import get_lexer_for_filename
49+
50+
class TutorialFormatterDirective(rst.Directive):
51+
has_content = False
52+
final_argument_whitespace = True
53+
required_arguments = 1
54+
55+
option_spec = dict(shell=flag, prompt=flag, nostderr=flag,
56+
in_srcdir=flag, extraargs=unchanged,
57+
until=unchanged)
58+
59+
def run(self):
60+
filename = self.arguments[0]
61+
text_tag = None
62+
tag_len = 0
63+
64+
filepath = self.state.document.settings.env.srcdir
65+
absfilename = os.path.join( filepath, filename )
66+
if absfilename.endswith('.h'):
67+
language = 'c++'
68+
elif absfilename.endswith('CMakeLists.txt'):
69+
language = 'cmake'
70+
else:
71+
try:
72+
language = get_lexer_for_filename( absfilename ).name.lower()
73+
if language == 'text only':
74+
language = 'none'
75+
except:
76+
language = 'none'
77+
code_prefix = '\n.. code-block:: ' + language + '\n\n'
78+
code_suffix = '\n'
79+
80+
print "tutorial-formatter running on", absfilename
81+
file_ = open( absfilename, 'r' )
82+
text_to_process = ""
83+
current_block = ""
84+
in_code = False
85+
in_text = False
86+
in_tutorial = False
87+
for line in file_:
88+
if not in_tutorial:
89+
begin_pos = line.find( 'BEGIN_TUTORIAL' )
90+
if begin_pos != -1:
91+
text_tag = line[:begin_pos].lstrip()
92+
tag_len = len( text_tag )
93+
in_tutorial = True
94+
continue
95+
if line.find( 'END_TUTORIAL' ) != -1:
96+
break
97+
stripped = line.lstrip()
98+
if stripped.startswith( text_tag.strip() ):
99+
if in_code:
100+
text_to_process += code_prefix + current_block + code_suffix
101+
current_block = ""
102+
in_code = False
103+
in_text = True
104+
addition = stripped[tag_len:]
105+
if addition == '' or addition[-1] != '\n':
106+
addition += '\n'
107+
current_block += addition
108+
else:
109+
if in_text:
110+
text_to_process += current_block
111+
current_block = ""
112+
in_text = False
113+
in_code = True # Code to show begins right after tagged text
114+
if in_code:
115+
current_block += ' ' + line
116+
if in_code:
117+
text_to_process += code_prefix + current_block + code_suffix
118+
elif in_text:
119+
text_to_process += current_block
120+
121+
# Debug writes...
122+
#print 'text_to_process ='
123+
#print text_to_process
124+
#print '= text_to_process'
125+
126+
lines = string2lines( text_to_process )
127+
self.state_machine.insert_input( lines, absfilename )
128+
129+
return []
130+
131+
def setup(app):
132+
app.add_directive('tutorial-formatter', TutorialFormatterDirective)

rviz_python_tutorial/myviz

Lines changed: 0 additions & 80 deletions
This file was deleted.

0 commit comments

Comments
 (0)