name_map_builder.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 
6 
7 import string
8 import re
9 import os
10 import sys
11 
12 import converter.node.box as node_box
13 import converter.node.behavior_layer as node_behavior_layer
14 import converter.node.behavior_keyframe as node_behavior_keyframe
15 
16 
17 def format_name(name):
18  """ Format a name and erase not printable chars
19 
20  :param name: name to format
21  :returns: formatted name
22  """
23  if not name:
24  return "unnamed_object"
25 
26  result = ""
27  for char in name:
28  if char in string.printable:
29  result += char
30 
31  name = result
32  pattern = re.compile(r'[\W_]+')
33  return pattern.sub('_', name)
34 
35 
36 def find_io_name(box, io_id):
37  """ Find the name of the io designed by id in a box
38 
39  :param box: box with ports
40  :param io_id: id of the io
41  :returns: name of the io as a string
42  """
43  for io in box.inputs + box.outputs + box.parameters:
44  if (io.id == io_id):
45  return io.name
46  return ""
47 
48 
49 def find_input_nature(box, io_id):
50  """ Find the nature of the input designed by id in a box
51 
52  :param box: box with inputs
53  :param io_id: id of the io
54  :returns: nature of the io as a string
55  """
56  for io in box.inputs:
57  if (io.id == io_id):
58  return io.nature
59  return ""
60 
61 
63  """ Do a traversal of xar objects and rename each objects
64  according to the new format convention
65  """
66 
67  def __init__(self):
68  self._box_stack = [] # used for IOs
69  self._boxes = {}
70  self._name_set = set()
71 
72  def construct_name(self, node):
73  name = node.name + str(node.id)
74  if node.parent_path:
75  name = node.parent_path + "_" + name
76 
77  name = format_name(name) # to removed unwanted characters
78  return self.find_available_name(name)
79 
80  def construct_node_prefix(self, node):
81  prefix = ""
82  if node:
83  prefix = node.parent_path
84  # check only nodes that can have influence on prefix
85  if isinstance(node, node_box.Box):
86  if prefix:
87  prefix += "_"
88  prefix += str("B")
89  if node.id != str(-1):
90  prefix += str(node.id)
91  elif isinstance(node, node_behavior_layer.BehaviorLayer):
92  if prefix:
93  prefix += "_"
94  prefix += str(node.id)
95  elif isinstance(node, node_behavior_keyframe.BehaviorKeyframe):
96  if prefix:
97  prefix += "_"
98  prefix += str(node.id)
99 
100  return prefix
101 
102  def find_available_name(self, name):
103  if (name in self._name_set):
104  raise Exception("Conflict found on name: " + name)
105  else:
106  self._name_set.add(name)
107  return name
108 
109  def get_name_map(self):
110  """ Returns a map with names and associated objects
111 
112  :returns: a map
113  """
114  return self._boxes
115 
116  def visit(self, node):
117  """ Visit a node, and choose the good method to apply
118  """
119  if not node:
120  return
121  methname = "_visit_%s" % node.node_type.lower()
122  method = getattr(self, methname, self.visit)
123  return method(node)
124 
125  def _visit_timeline(self, node):
126  # Construct parent_path for children
127  node.parent_path = self.construct_node_prefix(node.parent_node)
128  # Timelines have the same path + basename as the owner box
129  node.node_path = node.parent_node.node_path
130  for childNode in node.children_node:
131  self.visit(childNode)
132 
133  def _visit_diagram(self, node):
134  # diagrams have the same path as their parent
135  node.parent_path = self.construct_node_prefix(node.parent_node)
136 
137  # diagrams have the same path + basename as the owner keyframe
138  # but if parent timeline isn't enable
139  timeline = node.parent_node.parent_node.parent_node
140  if timeline.enable == "0":
141  node.node_path = timeline.node_path
142  else:
143  node.node_path = node.parent_node.node_path
144 
145  for childNode in node.children_node:
146  self.visit(childNode)
147 
148  id_map = {}
149  for child in node.boxes:
150  id_map[child.id] = child.node_path
151  parent_name = ""
152  if self._box_stack:
153  parent_name = self._box_stack[len(self._box_stack) - 1].node_path
154  else:
155  sys.stderr.write("ERROR: No parent for " + node.name
156  + " abort..." + os.linesep)
157  sys.exit(2)
158  id_map[str(0)] = parent_name
159 
160  # Diagram has no name in legacy format so we fill it
161  # useless ??
162  # node.name = self.find_available_name(parent_name + "_diagram")
163 
164  for link in node.links:
165  link.emitterName = id_map[link.emitterID]
166  link.receiverName = id_map[link.receiverID]
167 
168  link.inputName = find_io_name(self._boxes[link.emitterName],
169  link.indexofinput)
170  link.outputName = find_io_name(self._boxes[link.receiverName],
171  link.indexofoutput)
172 
173  def _visit_box(self, node):
174  node.parent_path = self.construct_node_prefix(node.parent_node)
175  node.node_path = self.construct_name(node) # check collisions
176  self._boxes[node.node_path] = node
177 
178  self._box_stack.append(node)
179  for childNode in node.children_node:
180  self.visit(childNode)
181  self._box_stack.pop()
182 
183  def _visit_behaviorlayer(self, node):
184  node.parent_path = self.construct_node_prefix(node.parent_node)
185  node.node_path = self.construct_name(node) # check collisions
186  for childNode in node.children_node:
187  self.visit(childNode)
188 
189  def _visit_behaviorkeyframe(self, node):
190  node.parent_path = self.construct_node_prefix(node.parent_node)
191  node.node_path = self.construct_name(node) # check collisions
192  for childNode in node.children_node:
193  self.visit(childNode)
194 
195  def _visit_bitmap(self, node):
196  node.parent_path = self.construct_node_prefix(node.parent_node)
197  node.node_path = node.parent_path + "_Bitmap"
198  for childNode in node.children_node:
199  self.visit(childNode)
200 
201  def _visit_script(self, node):
202  # useless
203  node.parent_path = self.construct_node_prefix(node.parent_node)
204 
205  # script have the same path + basename than the owner box
206  node.node_path = node.parent_node.node_path
207  for childNode in node.children_node:
208  self.visit(childNode)
209 
210  def _visit_parameter(self, node):
211  node.parent_path = self.construct_node_prefix(node.parent_node)
212  node.node_path = node.parent_path + "_Parameter"
213 
214  def _visit_plugincontent(self, node):
215  node.parent_path = self.construct_node_prefix(node.parent_node)
216  node.node_path = node.parent_path + "_PluginContent"
217 
218  def _visit_actuatorlist(self, node):
219  node.parent_path = self.construct_node_prefix(node.parent_node)
220  # ActuatorLists have the same path + basename as owner box/timeline
221  node.node_path = node.parent_node.node_path
222  for childNode in node.children_node:
223  self.visit(childNode)
224 
225  def _visit_actuatorcurve(self, node):
226  node.parent_path = self.construct_node_prefix(node.parent_node)
227  node.node_path = node.parent_path + "_ActuatorCurve"
228  for childNode in node.children_node:
229  self.visit(childNode)
230 
231  def _visit_actuatorkey(self, node):
232  node.parent_path = self.construct_node_prefix(node.parent_node)
233  node.node_path = node.parent_path + "_ActuatorKey"
234  for childNode in node.children_node:
235  self.visit(childNode)
converter.node.box
Definition: box.py:1
converter.name_map_builder.NameMapBuilder.find_available_name
def find_available_name(self, name)
Definition: name_map_builder.py:102
converter.name_map_builder.NameMapBuilder._visit_script
def _visit_script(self, node)
Definition: name_map_builder.py:201
converter.name_map_builder.NameMapBuilder
Definition: name_map_builder.py:62
converter.name_map_builder.NameMapBuilder.get_name_map
def get_name_map(self)
Definition: name_map_builder.py:109
converter.name_map_builder.NameMapBuilder._visit_diagram
def _visit_diagram(self, node)
Definition: name_map_builder.py:133
converter.name_map_builder.find_input_nature
def find_input_nature(box, io_id)
Definition: name_map_builder.py:49
converter.name_map_builder.NameMapBuilder.construct_name
def construct_name(self, node)
Definition: name_map_builder.py:72
converter.node.behavior_keyframe
Definition: behavior_keyframe.py:1
converter.name_map_builder.NameMapBuilder._visit_timeline
def _visit_timeline(self, node)
Definition: name_map_builder.py:125
converter.name_map_builder.NameMapBuilder._box_stack
_box_stack
Definition: name_map_builder.py:68
converter.name_map_builder.NameMapBuilder._visit_plugincontent
def _visit_plugincontent(self, node)
Definition: name_map_builder.py:214
converter.name_map_builder.NameMapBuilder.visit
def visit(self, node)
Definition: name_map_builder.py:116
converter.name_map_builder.NameMapBuilder._visit_behaviorlayer
def _visit_behaviorlayer(self, node)
Definition: name_map_builder.py:183
converter.name_map_builder.NameMapBuilder._name_set
_name_set
Definition: name_map_builder.py:70
converter.name_map_builder.NameMapBuilder._visit_actuatorkey
def _visit_actuatorkey(self, node)
Definition: name_map_builder.py:231
converter.node.behavior_layer
Definition: behavior_layer.py:1
converter.name_map_builder.find_io_name
def find_io_name(box, io_id)
Definition: name_map_builder.py:36
converter.name_map_builder.NameMapBuilder._boxes
_boxes
Definition: name_map_builder.py:69
converter.name_map_builder.NameMapBuilder._visit_behaviorkeyframe
def _visit_behaviorkeyframe(self, node)
Definition: name_map_builder.py:189
converter.name_map_builder.NameMapBuilder.construct_node_prefix
def construct_node_prefix(self, node)
Definition: name_map_builder.py:80
converter.name_map_builder.NameMapBuilder._visit_actuatorcurve
def _visit_actuatorcurve(self, node)
Definition: name_map_builder.py:225
converter.name_map_builder.NameMapBuilder.__init__
def __init__(self)
Definition: name_map_builder.py:67
converter.name_map_builder.NameMapBuilder._visit_box
def _visit_box(self, node)
Definition: name_map_builder.py:173
converter.name_map_builder.NameMapBuilder._visit_parameter
def _visit_parameter(self, node)
Definition: name_map_builder.py:210
converter.name_map_builder.NameMapBuilder._visit_bitmap
def _visit_bitmap(self, node)
Definition: name_map_builder.py:195
converter.name_map_builder.NameMapBuilder._visit_actuatorlist
def _visit_actuatorlist(self, node)
Definition: name_map_builder.py:218
converter.name_map_builder.format_name
def format_name(name)
Definition: name_map_builder.py:17


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