grasps_parser.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 #
3 # Copyright 2011 Shadow Robot Company Ltd.
4 #
5 # This program is free software: you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by the Free
7 # Software Foundation, either version 2 of the License, or (at your option)
8 # any later version.
9 #
10 # This program is distributed in the hope that it will be useful, but WITHOUT
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 # more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with this program. If not, see <http://www.gnu.org/licenses/>.
17 #
18 import xml.etree.ElementTree as ET
19 from sr_hand.Grasp import Grasp
20 
21 DEBUG = 0
22 
23 
24 class GraspParser():
25 
26  """
27  Parses a XML file describing a grasp.
28  """
29 
30  def __init__(self):
31  # initialize stuff
32  self.xml_tree = ""
33  self.grasps = {}
34  self.xml_path = ""
35 
36  def parse_tree(self, xml_filename="grasps.xml"):
37  """
38  parses a given tree, returns a Grasp
39 
40  Keyword arguments:
41  xml_filename -- the filename where the grasp is defined
42  if no filename is provided, then the default
43  value is "grasps.xml"
44  """
45  # parse the xml tree
46  try:
47  self.xml_tree = ET.parse(xml_filename)
48  except Exception, inst:
49  print "Unexpected error opening %s: %s" % (xml_filename, inst)
50  return
51 
52  self.xml_path = xml_filename
53 
54  tree_root = self.xml_tree.getroot()
55  tree_grasp = tree_root.findall("grasp")
56 
57  for grasp in tree_grasp:
58  grasp_tmp = Grasp()
59  grasp_tmp.grasp_name = grasp.attrib.get("name")
60  if DEBUG >= 2:
61  print "Grasp " + grasp_tmp.grasp_name
62 
63  joints = grasp.findall("joint")
64  for j in joints:
65  joint_name = j.attrib.get("name")
66  joint_position = float(j.text)
67  if DEBUG >= 2:
68  print " " + joint_name + ": " + str(joint_position)
69 
70  grasp_tmp.joints_and_positions.update(
71  {joint_name: joint_position})
72 
73  self.grasps.update({grasp_tmp.grasp_name: grasp_tmp})
74 
75  def write_grasp_to_file(self, grasp, xml_filename=""):
76  if xml_filename == "":
77  xml_filename = self.xml_path
78 
79  toWrite = grasp.convert_to_xml()
80  objFileRead = open(xml_filename, 'r')
81  previous = objFileRead.readlines()
82  objFileRead.close()
83  objFileWrite = open(xml_filename, 'w')
84  for index in range(0, len(previous) - 1):
85  objFileWrite.write(previous[index])
86  objFileWrite.write(toWrite)
87  objFileWrite.write('</root>')
88  objFileWrite.close()
89 
90  self.parse_tree(xml_filename)
91 
92  def refresh(self):
93  self.parse_tree(self.xml_path)
94 
95 #
96 # MAIN - simple test #
97 #
98 
99 
100 def main():
101  parser = GraspParser()
102  parser.parse_tree()
103 
104  return 0
105 
106 
107 # start the script
108 if __name__ == "__main__":
109  main()
def write_grasp_to_file(self, grasp, xml_filename="")
def parse_tree(self, xml_filename="grasps.xml")


sr_hand
Author(s): Ugo Cupcic
autogenerated on Tue Oct 13 2020 03:55:53