script_to_graph.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 #
00003 # Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
00004 #
00005 # Licensed under the Apache License, Version 2.0 (the "License");
00006 # you may not use this file except in compliance with the License.
00007 # You may obtain a copy of the License at
00008 #
00009 #    http://www.apache.org/licenses/LICENSE-2.0
00010 #
00011 # Unless required by applicable law or agreed to in writing, software
00012 # distributed under the License is distributed on an "AS IS" BASIS,
00013 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014 # See the License for the specific language governing permissions and
00015 # limitations under the License.
00016 
00017 import sys
00018 import types
00019 import string
00020 import os
00021 
00022 # graph includes
00023 import pygraphviz as pgv
00024 
00025 import rospy
00026 from simple_script_server import script
00027 
00028 
00029 if __name__ == "__main__":
00030         if (len(sys.argv) <= 1 ):
00031                 print "Error: wrong number of input arguments"
00032                 print "usage: rosrun cob_script_server script_to_graph.py <<SCRIPTFILE>> [level]"
00033                 # \todo What does "level" do?
00034                 sys.exit(1)
00035         elif (len(sys.argv) == 2):
00036                 filename = sys.argv[1]
00037                 level = 100
00038         elif (len(sys.argv) == 3):
00039                 filename = sys.argv[1]
00040                 level = int(sys.argv[2])
00041         else:
00042                 print "Error: to many arguments"
00043                 print "usage: rosrun cob_script_server script_to_graph.py <<SCRIPTFILE>> [level]"
00044                 sys.exit(1)
00045 
00046         print "Script file = ", filename
00047         print "Graph level = ", level
00048         rospy.set_param("/script_server/level",level)
00049 
00050         filename_splitted = string.split(filename, "/")
00051         #print filename_splitted
00052         scriptfile = filename_splitted[-1]
00053         if(len(filename_splitted) > 1):
00054                 filename_splitted.pop(len(filename_splitted)-1)
00055                 scriptdir = ""
00056                 for name in filename_splitted:
00057                         scriptdir += name + "/"
00058                 #print scriptdir
00059                 sys.path.insert(0,scriptdir)
00060         scriptfile_woext = string.split(scriptfile, ".")[0]
00061         #print scriptfile_woext
00062 
00063         try:
00064                 __import__(scriptfile_woext, globals(), locals(), [], -1)
00065                 scriptmodule = sys.modules[scriptfile_woext]
00066                 for classname in dir(scriptmodule):
00067                         subclass = scriptmodule.__getattribute__(classname)
00068                         if(isinstance(subclass, types.ClassType)):
00069                                 if(issubclass(subclass, script)):
00070                                         if(classname != "script"):
00071                                                 s = subclass()
00072                                                 dotcode = s.Parse()
00073                                                 print "dotcode = ",dotcode
00074                                                 graph=pgv.AGraph(dotcode)
00075                                                 graph.layout('dot')
00076                                                 basename, extension = os.path.splitext(filename)
00077                                                 if (level == 100):
00078                                                         graph.draw(basename + ".png")
00079                                                 else:
00080                                                         graph.draw(basename + "_" + str(level) + ".png")
00081         except ImportError:
00082                 print "Unable to import script file"
00083                 print "usage: rosrun cob_script_server script_to_graph.py <<SCRIPTFILE>> [level]"
00084 


cob_script_server
Author(s): Florian Weisshardt
autogenerated on Sun Jun 9 2019 20:20:12