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