xar_parser.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 
6 
7 import codecs
8 import xml.sax
9 
10 import converter.xar_types as xar_types
11 import converter.node.box as node_box
12 import converter.node.bitmap as node_bitmap
13 import converter.node.script as node_script
14 import converter.node.parameter as node_parameter
15 import converter.node.plugin_content as node_plugin_content
16 import converter.node.diagram as node_diagram
17 import converter.node.timeline as node_timeline
18 import converter.node.behavior_layer as node_behavior_layer
19 import converter.node.behavior_keyframe as node_behavior_keyframe
20 import converter.node.actuator_list as node_actuator_list
21 import converter.node.actuator_curve as node_actuator_curve
22 import converter.node.actuator_key as node_actuator_key
23 
24 
26  """ Generates the reprenstation of the file designed by filename
27  in memory. Build a tree.
28 
29  :param filename: name of the file to parse
30  :returns: the tree representing the file
31  """
32  ofile = _check_open_file(filename)
33  if not ofile:
34  return None
35  parser = xml.sax.make_parser()
36  handler = XarHandler()
37  parser.setContentHandler(handler)
38  parser.parse(open(filename))
39  return handler.get_root()
40 
41 
42 def _check_open_file(filename):
43  with codecs.open(filename, encoding='utf-8', mode='r') as ofile:
44  header = ofile.readline()
45  header = header + ofile.readline()
46  if (not ("xar_version=\"3\"" in header)):
47  return False
48  return True
49 
50 
51 class XarHandler(xml.sax.handler.ContentHandler):
52  """ ContentHandler to parse the xar file
53  """
54 
55  def __init__(self):
56  xml.sax.handler.ContentHandler.__init__(self)
57  self._nodes = []
58  self._buffer = ""
59  self._plugin_content = False
60  self._root = None
61 
62  def startElement(self, name, attrs):
63  new_node = None
64  if name == 'Box':
65  new_node = node_box.Box(xar_types.attributes(attrs))
66  elif name == 'script':
67  new_node = node_script.Script(xar_types.attributes(attrs))
68  elif name == 'Parameter':
69  new_node = node_parameter.Parameter(xar_types.attributes(attrs))
70  elif name == 'pluginContent':
71  new_node = node_plugin_content.PluginContent(
72  xar_types.attributes(attrs))
73  self._plugin_content = True
74  elif name == 'Timeline':
75  new_node = node_timeline.Timeline(xar_types.attributes(attrs))
76  elif name == 'BehaviorLayer':
77  new_node = node_behavior_layer.BehaviorLayer(
78  xar_types.attributes(attrs))
79  elif name == 'BehaviorKeyframe':
80  new_node = node_behavior_keyframe.BehaviorKeyframe(
81  xar_types.attributes(attrs))
82  elif name == 'Diagram':
83  new_node = node_diagram.Diagram(xar_types.attributes(attrs))
84  elif name == 'ActuatorList':
85  new_node = node_actuator_list.ActuatorList(
86  xar_types.attributes(attrs))
87  elif name == 'ActuatorCurve':
88  new_node = node_actuator_curve.ActuatorCurve(
89  xar_types.attributes(attrs))
90  elif name == 'Key':
91  new_node = node_actuator_key.ActuatorKey(
92  xar_types.attributes(attrs))
93 
94  if new_node:
95  if not self._root:
96  self._root = new_node
97  # Attach to parent here
98  if self._nodes:
99  parent_node = self._nodes.pop()
100  parent_node.attach_attribute(name, new_node)
101  parent_node.add_child(new_node)
102  self._nodes.append(parent_node)
103 
104  self._nodes.append(new_node)
105  else:
106  """ pluginContent beacon can embed anything, raw string as well as
107  sub nodes.
108  """
109  if self._plugin_content:
110  new_node = node_plugin_content.PluginSubNode(name, attrs)
111  parent_node = self._nodes.pop()
112  parent_node.add_subnode(new_node)
113  parent_node.add_child(new_node)
114  self._nodes.append(parent_node)
115  self._nodes.append(new_node)
116 
117  elif (self._nodes and name != 'bitmap' and name != 'content'):
118  parent_node = self._nodes.pop()
119  parent_node.attach_attribute(name,
120  xar_types.attributes(attrs))
121  self._nodes.append(parent_node)
122 
123  self._buffer = ""
124 
125  def endElement(self, name):
126  if (name == 'Box'
127  or name == 'script' or name == 'Parameter'
128  or name == 'Timeline' or name == 'BehaviorLayer'
129  or name == 'BehaviorKeyframe' or name == 'Diagram'
130  or name == 'ActuatorList' or name == 'ActuatorCurve'
131  or name == 'Key'):
132  self._nodes.pop()
133  elif name == 'pluginContent':
134  self._plugin_content = False
135  self._nodes.pop()
136  elif self._plugin_content:
137  current_subnode = self._nodes.pop()
138  if self._buffer:
139  current_subnode.add_content(self._buffer)
140  elif name == 'bitmap':
141  bitmap = node_bitmap.Bitmap(self._buffer)
142  parent_node = self._nodes.pop()
143  parent_node.attach_attribute(name, bitmap)
144  parent_node.add_child(bitmap)
145  self._nodes.append(parent_node)
146  elif name == 'content':
147  parent_node = self._nodes.pop()
148  parent_node.attach_attribute(name, self._buffer)
149  self._nodes.append(parent_node)
150 
151  self._buffer = ""
152 
153  def characters(self, content):
154  self._buffer += content
155 
156  def get_root(self):
157  return self._root
converter.node.script
Definition: script.py:1
converter.node.timeline
Definition: timeline.py:1
converter.node.box
Definition: box.py:1
converter.xar_parser.XarHandler._root
_root
Definition: xar_parser.py:60
converter.xar_parser.XarHandler.characters
def characters(self, content)
Definition: xar_parser.py:153
converter.xar_parser.XarHandler._nodes
_nodes
Definition: xar_parser.py:57
converter.node.behavior_keyframe
Definition: behavior_keyframe.py:1
converter.xar_types
Definition: xar_types.py:1
converter.node.actuator_list
Definition: actuator_list.py:1
converter.node.parameter
Definition: parameter.py:1
converter.xar_parser.XarHandler._buffer
_buffer
Definition: xar_parser.py:58
converter.xar_parser.XarHandler.endElement
def endElement(self, name)
Definition: xar_parser.py:125
converter.node.bitmap
Definition: bitmap.py:1
converter.xar_parser.XarHandler
Definition: xar_parser.py:51
converter.node.actuator_curve
Definition: actuator_curve.py:1
converter.xar_parser.generate_tree_from_filename
def generate_tree_from_filename(filename)
Definition: xar_parser.py:25
converter.xar_parser._check_open_file
def _check_open_file(filename)
Definition: xar_parser.py:42
converter.node.actuator_key
Definition: actuator_key.py:1
converter.node.behavior_layer
Definition: behavior_layer.py:1
converter.xar_parser.XarHandler.get_root
def get_root(self)
Definition: xar_parser.py:156
converter.xar_types.attributes
Copyright (c) 2012 Aldebaran Robotics.
Definition: xar_types.py:12
converter.xar_parser.XarHandler._plugin_content
_plugin_content
Definition: xar_parser.py:59
converter.node.diagram
Definition: diagram.py:1
converter.xar_parser.XarHandler.__init__
def __init__(self)
Definition: xar_parser.py:55
converter.xar_parser.XarHandler.startElement
def startElement(self, name, attrs)
Definition: xar_parser.py:62
converter.node.plugin_content
Definition: plugin_content.py:1


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