Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 import xml.etree.ElementTree as ET
00019 from 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
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
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
00093
00094 def main():
00095 parser = GraspParser()
00096 parser.parse_tree()
00097
00098 return 0
00099
00100
00101
00102 if __name__ == "__main__":
00103 main()