animation_parser.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 
6 
7 import xml.sax
8 
9 import converter.xar_types as xar_types
10 import converter.node.animation as node_animation
11 import converter.node.actuator_list as node_actuator_list
12 import converter.node.actuator_curve as node_actuator_curve
13 import converter.node.actuator_key as node_actuator_key
14 
15 
17  """ Generates the representation of the file designed by filename
18  in memory. Build a tree.
19 
20  :param filename: name of the file to parse
21  :returns: the tree representing the file
22  """
23  parser = xml.sax.make_parser()
24  handler = AnimationHandler()
25  parser.setContentHandler(handler)
26  parser.parse(open(filename))
27 
28  root_node = handler.get_root()
29  if root_node.format_version != "4":
30  return None
31 
32  return root_node
33 
34 
35 class AnimationHandler(xml.sax.handler.ContentHandler):
36  """ ContentHandler to parse the animation file
37  """
38 
39  def __init__(self):
40  xml.sax.handler.ContentHandler.__init__(self)
41  self._nodes = []
42  self._root = None
43 
44  def startElement(self, name, attrs):
45  new_node = None
46  if name == 'Animation':
47  new_node = node_animation.Animation(
48  xar_types.attributes(attrs))
49  elif name == 'ActuatorList':
50  new_node = node_actuator_list.ActuatorList(
51  xar_types.attributes(attrs))
52  elif name == 'ActuatorCurve':
53  new_node = node_actuator_curve.ActuatorCurve(
54  xar_types.attributes(attrs))
55  elif name == 'Key':
56  new_node = node_actuator_key.ActuatorKey(
57  xar_types.attributes(attrs))
58 
59  if new_node:
60  if not self._root:
61  self._root = new_node
62  # Attach to parent here
63  if self._nodes:
64  parent_node = self._nodes.pop()
65  parent_node.attach_attribute(name, new_node)
66  parent_node.add_child(new_node)
67  self._nodes.append(parent_node)
68 
69  self._nodes.append(new_node)
70  else:
71  raise Exception("The node %s is unknown" % name)
72 
73  def endElement(self, name):
74  self._nodes.pop()
75 
76  def get_root(self):
77  return self._root
converter.node.animation
Definition: animation.py:1
converter.animation_parser.generate_tree_from_filename
def generate_tree_from_filename(filename)
Definition: animation_parser.py:16
converter.xar_types
Definition: xar_types.py:1
converter.node.actuator_list
Definition: actuator_list.py:1
converter.animation_parser.AnimationHandler._nodes
_nodes
Definition: animation_parser.py:41
converter.animation_parser.AnimationHandler.get_root
def get_root(self)
Definition: animation_parser.py:76
converter.node.actuator_curve
Definition: actuator_curve.py:1
converter.node.actuator_key
Definition: actuator_key.py:1
converter.xar_types.attributes
Copyright (c) 2012 Aldebaran Robotics.
Definition: xar_types.py:12
converter.animation_parser.AnimationHandler.startElement
def startElement(self, name, attrs)
Definition: animation_parser.py:44
converter.animation_parser.AnimationHandler
Definition: animation_parser.py:35
converter.animation_parser.AnimationHandler._root
_root
Definition: animation_parser.py:42
converter.animation_parser.AnimationHandler.endElement
def endElement(self, name)
Definition: animation_parser.py:73
converter.animation_parser.AnimationHandler.__init__
def __init__(self)
Definition: animation_parser.py:39


naoqi_libqicore
Author(s): Aldebaran
autogenerated on Wed Sep 14 2022 02:22:41