Skip to content

Commit 077b4e2

Browse files
committed
noetic support, based on InigoMoreno's work. see ros-visualization#39
1 parent 1ec4886 commit 077b4e2

File tree

4 files changed

+115
-50
lines changed

4 files changed

+115
-50
lines changed

smach_viewer/scripts/smach_viewer.py

+56-31
Original file line numberDiff line numberDiff line change
@@ -42,41 +42,63 @@
4242
import pickle
4343
import pprint
4444
import copy
45-
import StringIO
45+
try:
46+
from StringIO import StringIO # for Python 2
47+
except ImportError:
48+
from io import StringIO # for Python 3
4649
import colorsys
4750
import time
4851
import base64
4952

50-
import wxversion
51-
if wxversion.checkInstalled("2.8"):
52-
wxversion.select("2.8")
53-
else:
54-
print("wxversion 2.8 is not installed, installed versions are {}".format(wxversion.getInstalled()))
53+
try:
54+
import wxversion
55+
if wxversion.checkInstalled("2.8"):
56+
wxversion.select("2.8")
57+
else:
58+
print("wxversion 2.8 is not installed, installed versions are {}".format(wxversion.getInstalled()))
59+
60+
## this import system (or ros-released) xdot
61+
# import xdot
62+
## need to import currnt package, but not to load this file
63+
# http://stackoverflow.com/questions/6031584/importing-from-builtin-library-when-module-with-same-name-exists
64+
def import_non_local(name, custom_name=None):
65+
import imp, sys
66+
67+
custom_name = custom_name or name
68+
69+
path = filter(lambda x: x != os.path.dirname(os.path.abspath(__file__)), sys.path)
70+
f, pathname, desc = imp.find_module(name, path)
71+
72+
module = imp.load_module(custom_name, f, pathname, desc)
73+
if f:
74+
f.close()
75+
76+
return module
77+
78+
smach_viewer = import_non_local('smach_viewer')
79+
from smach_viewer import xdot
80+
from smach_viewer.xdot import wxxdot
81+
from smach_viewer.xdot.xdot import TextShape
82+
except:
83+
# Guard against self import
84+
this_dir = os.path.dirname(__file__)
85+
# Use os.getcwd() to aovid weird symbolic link problems
86+
cur_dir = os.getcwd()
87+
os.chdir(this_dir)
88+
this_dir_cwd = os.getcwd()
89+
os.chdir(cur_dir)
90+
# Remove this dir from path
91+
sys.path = [a for a in sys.path if a not in [this_dir, this_dir_cwd]]
92+
#
93+
from smach_viewer.xdot import wxxdot
94+
from xdot.ui.elements import *
95+
96+
5597
import wx
5698
import wx.richtext
5799

58100
import textwrap
59101

60-
## this import system (or ros-released) xdot
61-
# import xdot
62-
## need to import currnt package, but not to load this file
63-
# http://stackoverflow.com/questions/6031584/importing-from-builtin-library-when-module-with-same-name-exists
64-
def import_non_local(name, custom_name=None):
65-
import imp, sys
66-
67-
custom_name = custom_name or name
68-
69-
path = filter(lambda x: x != os.path.dirname(os.path.abspath(__file__)), sys.path)
70-
f, pathname, desc = imp.find_module(name, path)
71-
72-
module = imp.load_module(custom_name, f, pathname, desc)
73-
if f:
74-
f.close()
75-
76-
return module
77-
78-
smach_viewer = import_non_local('smach_viewer')
79-
from smach_viewer import xdot
80102
##
81103
import smach
82104
import smach_ros
@@ -167,8 +189,11 @@ def _load_local_data(self, msg):
167189
"""Unpack the user data"""
168190
try:
169191
local_data = pickle.loads(msg.local_data)
170-
except KeyError:
171-
local_data = pickle.loads(base64.b64decode(msg.local_data.encode('utf-8')))
192+
except:
193+
if isinstance(msg.local_data, str):
194+
local_data = pickle.loads(base64.b64decode(msg.local_data))
195+
else:
196+
local_data = pickle.loads(base64.b64decode(bytes(str(msg.local_data).encode('utf-8'))))
172197
return local_data
173198

174199
def update_status(self, msg):
@@ -460,7 +485,7 @@ def set_styles(self, selected_paths, depth, max_depth, items, subgraph_shapes, c
460485
else:
461486
if child_path in items:
462487
for shape in items[child_path].shapes:
463-
if not isinstance(shape,xdot.xdot.TextShape):
488+
if not isinstance(shape,TextShape):
464489
shape.pen.color = child_color
465490
shape.pen.fillcolor = child_fillcolor
466491
shape.pen.linewidth = child_linewidth
@@ -565,7 +590,7 @@ def __init__(self):
565590
self.Bind(wx.EVT_TOOL, self.SaveDotGraph, id=wx.ID_SAVE)
566591

567592
# Create dot graph widget
568-
self.widget = xdot.wxxdot.WxDotWindow(graph_view, -1)
593+
self.widget = wxxdot.WxDotWindow(graph_view, -1)
569594

570595
gv_vbox.Add(toolbar, 0, wx.EXPAND)
571596
gv_vbox.Add(self.widget, 1, wx.EXPAND)
@@ -1071,4 +1096,4 @@ def main():
10711096
if __name__ == '__main__':
10721097
rospy.init_node('smach_viewer',anonymous=False, disable_signals=True,log_level=rospy.INFO)
10731098
sys.argv = rospy.myargv()
1074-
main()
1099+
main()

smach_viewer/setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env python
22

3-
from distutils.core import setup
3+
from setuptools import setup
44
from catkin_pkg.python_setup import generate_distutils_setup
55

66
d = generate_distutils_setup(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

smach_viewer/src/smach_viewer/xdot/wxxdot.py

+57-18
Original file line numberDiff line numberDiff line change
@@ -19,29 +19,67 @@
1919
# You should have received a copy of the GNU Lesser General Public License
2020
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2121

22-
from .xdot import *
23-
22+
try:
23+
from xdot.ui.elements import *
24+
from xdot.ui.animation import *
25+
from xdot.dot.lexer import *
26+
from xdot.dot.parser import *
27+
import subprocess
28+
except:
29+
from xdot import *
30+
31+
# Python 3 renamed the unicode type to str, the old str type has been replaced by bytes.
32+
if sys.version_info[0] >= 3:
33+
unicode = str
2434

2535
__all__ = ['WxDotWindow', 'WxDotFrame']
2636

2737
# We need to get the wx version with built-in cairo support
28-
import wxversion
29-
if wxversion.checkInstalled("2.8"):
30-
wxversion.select("2.8")
31-
else:
32-
print("wxversion 2.8 is not installed, installed versions are {}".format(wxversion.getInstalled()))
38+
try:
39+
import wxversion
40+
if wxversion.checkInstalled("2.8"):
41+
wxversion.select("2.8")
42+
else:
43+
print("wxversion 2.8 is not installed, installed versions are {}".format(wxversion.getInstalled()))
44+
except:
45+
pass # Python3
46+
3347
import wx
3448
import wx.lib.wxcairo as wxcairo
3549

3650
# This is a crazy hack to get this to work on 64-bit systems
37-
if 'wxMac' in wx.PlatformInfo:
38-
pass # Implement if necessary
39-
elif 'wxMSW' in wx.PlatformInfo:
40-
pass # Implement if necessary
41-
elif 'wxGTK' in wx.PlatformInfo:
42-
import ctypes
43-
gdkLib = wx.lib.wxcairo._findGDKLib()
44-
gdkLib.gdk_cairo_create.restype = ctypes.c_void_p
51+
try:
52+
if 'wxMac' in wx.PlatformInfo:
53+
pass # Implement if necessary
54+
elif 'wxMSW' in wx.PlatformInfo:
55+
pass # Implement if necessary
56+
elif 'wxGTK' in wx.PlatformInfo:
57+
import ctypes
58+
gdkLib = wx.lib.wxcairo._findGDKLib()
59+
gdkLib.gdk_cairo_create.restype = ctypes.c_void_p
60+
except:
61+
pass # Python3
62+
63+
class MyXDotParser(XDotParser):
64+
65+
def __init__(self, xdotcode):
66+
XDotParser.__init__(self,xdotcode)
67+
self.subgraph_shapes = {}
68+
69+
def parse_subgraph(self):
70+
shapes_before = set(self.shapes)
71+
72+
id = XDotParser.parse_subgraph(self)
73+
74+
new_shapes = set(self.shapes) - shapes_before
75+
self.subgraph_shapes[id.decode()] = [s for s in new_shapes if not any([s in ss for ss in self.subgraph_shapes.values()])]
76+
77+
return id
78+
79+
def parse(self):
80+
graph = XDotParser.parse(self)
81+
graph.subgraph_shapes = self.subgraph_shapes
82+
return graph
4583

4684
class WxDragAction(object):
4785
def __init__(self, dot_widget):
@@ -436,8 +474,9 @@ def set_filter(self, filter):
436474
self.filter = filter
437475

438476
def set_dotcode(self, dotcode, filename='<stdin>'):
439-
if isinstance(dotcode, unicode):
440-
dotcode = dotcode.encode('utf8')
477+
# Python 3 renamed the unicode type to str, the old str type has been replaced by bytes.
478+
if sys.version_info[0] < 3 and isinstance(dotcode, unicode):
479+
dotcode = dotcode.encode('utf8')
441480
p = subprocess.Popen(
442481
[self.filter, '-Txdot'],
443482
stdin=subprocess.PIPE,
@@ -484,7 +523,7 @@ def set_dotcode(self, dotcode, filename='<stdin>'):
484523
def set_xdotcode(self, xdotcode):
485524
"""Set xdot code."""
486525
#print xdotcode
487-
parser = XDotParser(xdotcode)
526+
parser = MyXDotParser(bytes(str(xdotcode).encode("utf-8")))
488527
self.graph = parser.parse()
489528
self.highlight = None
490529
#self.zoom_image(self.zoom_ratio, center=True)

0 commit comments

Comments
 (0)