grasps_parser.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 #
00003 # Copyright 2011 Shadow Robot Company Ltd.
00004 #
00005 # This program is free software: you can redistribute it and/or modify it
00006 # under the terms of the GNU General Public License as published by the Free
00007 # Software Foundation, either version 2 of the License, or (at your option)
00008 # any later version.
00009 #
00010 # This program is distributed in the hope that it will be useful, but WITHOUT
00011 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 # FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
00013 # more details.
00014 #
00015 # You should have received a copy of the GNU General Public License along
00016 # with this program.  If not, see <http://www.gnu.org/licenses/>.
00017 #
00018 import xml.etree.ElementTree as ET
00019 from sr_hand.Grasp import Grasp
00020 
00021 DEBUG = 0
00022 
00023 class GraspParser():
00024     """
00025     Parses a XML file describing a grasp.
00026     """
00027     def __init__(self):
00028         #initialize stuff
00029         self.xml_tree = ""
00030         self.grasps = {}
00031         self.xml_path = ""
00032 
00033     def parse_tree(self, xml_filename="grasps.xml"):
00034         """
00035         parses a given tree, returns a Grasp
00036 
00037         Keyword arguments:
00038         xml_filename -- the filename where the grasp is defined
00039                         if no filename is provided, then the default
00040                         value is "grasps.xml"
00041         """
00042         #parse the xml tree
00043         try:
00044             self.xml_tree = ET.parse(xml_filename)
00045         except Exception, inst:
00046             print "Unexpected error opening %s: %s" % (xml_filename, inst)
00047             return
00048 
00049         self.xml_path = xml_filename
00050 
00051         tree_root = self.xml_tree.getroot()
00052         tree_grasp = tree_root.findall("grasp")
00053 
00054         for grasp in tree_grasp:
00055             grasp_tmp = Grasp()
00056             grasp_tmp.grasp_name = grasp.attrib.get("name")
00057             if DEBUG >= 2:
00058                 print "Grasp "+ grasp_tmp.grasp_name
00059 
00060             joints = grasp.findall("joint")
00061             for j in joints:
00062                 joint_name = j.attrib.get("name")
00063                 joint_position = float(j.text)
00064                 if DEBUG >= 2:
00065                     print "  "+ joint_name + ": "+ str(joint_position)
00066 
00067                 grasp_tmp.joints_and_positions.update({joint_name:joint_position})
00068 
00069             self.grasps.update({grasp_tmp.grasp_name: grasp_tmp})
00070 
00071     def write_grasp_to_file(self, grasp, xml_filename = ""):
00072         if xml_filename == "":
00073             xml_filename = self.xml_path
00074 
00075         toWrite = grasp.convert_to_xml()
00076         objFileRead = open(xml_filename,'r')
00077         previous = objFileRead.readlines()
00078         objFileRead.close()
00079         objFileWrite = open(xml_filename,'w')
00080         for index in range (0,len(previous)-1):
00081             objFileWrite.write(previous[index])
00082         objFileWrite.write(toWrite)
00083         objFileWrite.write('</root>')
00084         objFileWrite.close()
00085 
00086         self.parse_tree(xml_filename)
00087 
00088     def refresh(self):
00089         self.parse_tree(self.xml_path)
00090 
00091 ############################
00092 #    MAIN - simple test    #
00093 ############################
00094 def main():
00095     parser = GraspParser()
00096     parser.parse_tree()
00097 
00098     return 0
00099 
00100 
00101 # start the script
00102 if __name__ == "__main__":
00103     main()


sr_hand
Author(s): Ugo Cupcic
autogenerated on Fri Aug 21 2015 12:24:55