script_to_graph.py
Go to the documentation of this file.
00001 #!/usr/bin/python
00002 #################################################################
00003 ##\file
00004 #
00005 # \note
00006 #   Copyright (c) 2010 \n
00007 #   Fraunhofer Institute for Manufacturing Engineering
00008 #   and Automation (IPA) \n\n
00009 #
00010 #################################################################
00011 #
00012 # \note
00013 #   Project name: care-o-bot
00014 # \note
00015 #   ROS stack name: cob_apps
00016 # \note
00017 #   ROS package name: cob_script_server
00018 #
00019 # \author
00020 #   Author: Florian Weisshardt, email:florian.weisshardt@ipa.fhg.de
00021 # \author
00022 #   Supervised by: Florian Weisshardt, email:florian.weisshardt@ipa.fhg.de
00023 #
00024 # \date Date of creation: Aug 2010
00025 #
00026 # \brief
00027 #   Generates a visual graph (*.png) out of a script.
00028 #
00029 #################################################################
00030 #
00031 # Redistribution and use in source and binary forms, with or without
00032 # modification, are permitted provided that the following conditions are met:
00033 #
00034 #     - Redistributions of source code must retain the above copyright
00035 #       notice, this list of conditions and the following disclaimer. \n
00036 #     - Redistributions in binary form must reproduce the above copyright
00037 #       notice, this list of conditions and the following disclaimer in the
00038 #       documentation and/or other materials provided with the distribution. \n
00039 #     - Neither the name of the Fraunhofer Institute for Manufacturing
00040 #       Engineering and Automation (IPA) nor the names of its
00041 #       contributors may be used to endorse or promote products derived from
00042 #       this software without specific prior written permission. \n
00043 #
00044 # This program is free software: you can redistribute it and/or modify
00045 # it under the terms of the GNU Lesser General Public License LGPL as 
00046 # published by the Free Software Foundation, either version 3 of the 
00047 # License, or (at your option) any later version.
00048 # 
00049 # This program is distributed in the hope that it will be useful,
00050 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00051 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00052 # GNU Lesser General Public License LGPL for more details.
00053 # 
00054 # You should have received a copy of the GNU Lesser General Public 
00055 # License LGPL along with this program. 
00056 # If not, see <http://www.gnu.org/licenses/>.
00057 #
00058 #################################################################
00059 
00060 import roslib
00061 roslib.load_manifest('cob_script_server')
00062 import rospy
00063 import sys
00064 import types
00065 import string
00066 import os
00067 from simple_script_server import script
00068 
00069 # graph includes
00070 import pygraphviz as pgv
00071 
00072 
00073 if __name__ == "__main__":
00074         if (len(sys.argv) <= 1 ):
00075                 print "Error: wrong number of input arguments"
00076                 print "usage: rosrun cob_script_server script_to_graph.py <<SCRIPTFILE>> [level]"
00077                 # \todo What does "level" do?
00078                 sys.exit(1)
00079         elif (len(sys.argv) == 2):
00080                 filename = sys.argv[1]
00081                 level = 100
00082         elif (len(sys.argv) == 3):
00083                 filename = sys.argv[1]
00084                 level = int(sys.argv[2])
00085         else:
00086                 print "Error: to many arguments"
00087                 print "usage: rosrun cob_script_server script_to_graph.py <<SCRIPTFILE>> [level]"
00088                 sys.exit(1)
00089         
00090         print "Script file = ", filename
00091         print "Graph level = ", level
00092         rospy.set_param("/script_server/level",level)
00093         
00094         filename_splitted = string.split(filename, "/")
00095         #print filename_splitted
00096         scriptfile = filename_splitted[-1]
00097         if(len(filename_splitted) > 1):
00098                 filename_splitted.pop(len(filename_splitted)-1)
00099                 scriptdir = ""
00100                 for name in filename_splitted:
00101                         scriptdir += name + "/"
00102                 #print scriptdir
00103                 sys.path.insert(0,scriptdir)
00104         scriptfile_woext = string.split(scriptfile, ".")[0]
00105         #print scriptfile_woext
00106 
00107         try:
00108                 __import__(scriptfile_woext, globals(), locals(), [], -1)
00109                 scriptmodule = sys.modules[scriptfile_woext]
00110                 for classname in dir(scriptmodule):
00111                         subclass = scriptmodule.__getattribute__(classname)
00112                         if(isinstance(subclass, types.ClassType)):
00113                                 if(issubclass(subclass, script)):
00114                                         if(classname != "script"):
00115                                                 s = subclass()
00116                                                 dotcode = s.Parse()
00117                                                 print "dotcode = ",dotcode
00118                                                 graph=pgv.AGraph(dotcode)
00119                                                 graph.layout('dot')
00120                                                 basename, extension = os.path.splitext(filename)
00121                                                 if (level == 100):
00122                                                         graph.draw(basename + ".png")
00123                                                 else:
00124                                                         graph.draw(basename + "_" + str(level) + ".png")
00125         except ImportError:
00126                 print "Unable to import script file"
00127                 print "usage: rosrun cob_script_server script_to_graph.py <<SCRIPTFILE>> [level]"
00128 


cob_script_server
Author(s): Florian Weisshardt
autogenerated on Sun Oct 5 2014 22:59:42