ports.py
Go to the documentation of this file.
1 # -*- Python -*-
2 # -*- coding: utf-8 -*-
3 
4 '''rtsprofile
5 
6 Copyright (C) 2009-2010
7  Geoffrey Biggs
8  RT-Synthesis Research Group
9  Intelligent Systems Research Institute,
10  National Institute of Advanced Industrial Science and Technology (AIST),
11  Japan
12  All rights reserved.
13 Licensed under the Eclipse Public License -v 1.0 (EPL)
14 http://www.opensource.org/licenses/eclipse-1.0.txt
15 
16 File: ports.py
17 
18 Objects representing ports in a component.
19 
20 '''
21 
22 __version__ = '$Revision: $'
23 # $Source$
24 
25 
26 from rtsprofile import RTS_NS, RTS_NS_S, RTS_EXT_NS, RTS_EXT_NS_S, \
27  RTS_EXT_NS_YAML
28 from rtsprofile.utils import get_direct_child_elements_xml, \
29  parse_properties_xml, validate_attribute
30 
31 
32 ##############################################################################
33 ## DataPort object
34 
35 class DataPort(object):
36  '''Represents a data port of a component, as specified in a
37  ConnectorProfile.
38 
39  '''
40 
41  def __init__(self, name='', comment='', visible=True):
42  '''Constructor.
43 
44  @param name Name of the port.
45  @type name str
46  @param comment A comment about the port.
47  @type comment str
48  @param visible If this port is visible in graphical displays.
49  @type visible bool
50 
51  '''
52  validate_attribute(name, 'dataPort.name',
53  expected_type=[str, unicode], required=False)
54  self._name = name
55  validate_attribute(comment, 'component.ext.comment',
56  expected_type=[str, unicode], required=False)
57  self._comment = comment
58  validate_attribute(visible, 'component.ext.visible',
59  expected_type=bool, required=False)
60  self._visible = visible
61  self._properties = {}
62 
63  def __str__(self):
64  result = 'Name: {0}\n'.format(self.name)
65  if self.comment:
66  result += ' Comment: {0}\n'.format(self.comment)
67  result += ' Visible: {0}\n'.format(self.visible)
68  if self.properties:
69  result += ' Properties:\n'
70  for p in self.properties:
71  result += ' {0}: {1}\n'.format(p, self.properties[p])
72  return result[:-1] # Lop off the last new line
73 
74  @property
75  def name(self):
76  '''The name of this data port.
77 
78  This name is used in connector profiles to identify the port.
79 
80  '''
81  return self._name
82 
83  @name.setter
84  def name(self, name):
85  validate_attribute(name, 'dataPort.name',
86  expected_type=[str, unicode], required=True)
87  self._name = name
88 
89  @property
90  def comment(self):
91  '''Comment about the data port.
92 
93  A brief comment about the data port. May or may not be displayed in
94  other tools. May be empty.
95 
96  Part of the extended profile.
97 
98  '''
99  return self._comment
100 
101  @comment.setter
102  def comment(self, comment):
103  validate_attribute(comment, 'dataPort.ext.comment',
104  expected_type=[str, unicode], required=False)
105  self._comment = comment
106 
107  @property
108  def visible(self):
109  '''Display the port in graphical tools.
110 
111  This value controls whether graphical tools will display this port or
112  not.
113 
114  Part of the extended profile.
115 
116  '''
117  return self._visible
118 
119  @visible.setter
120  def visible(self, visible):
121  validate_attribute(visible, 'dataPort.ext.visible',
122  expected_type=bool, required=False)
123  self._visible = visible
124 
125  @property
126  def properties(self):
127  '''Miscellaneous properties.
128 
129  Stores key/value pair properties.
130 
131  Part of the extended profile.
132 
133  '''
134  return self._properties
135 
136  @properties.setter
137  def properties(self, properties):
138  validate_attribute(properties, 'dataPort.ext.Properties',
139  expected_type=list, required=False)
140  self._properties = properties
141 
142  def parse_xml_node(self, node):
143  '''Parse an xml.dom Node object representing a data port into this
144  object.
145 
146  '''
147 
148  self.name = node.getAttributeNS(RTS_NS, 'name')
149  self.comment = node.getAttributeNS(RTS_EXT_NS, 'comment')
150  if node.hasAttributeNS(RTS_EXT_NS, 'visible'):
151  visible = node.getAttributeNS(RTS_EXT_NS, 'visible')
152  if visible.lower() == 'true' or visible == '1':
153  self.visible = True
154  else:
155  self.visible = False
156  for c in get_direct_child_elements_xml(node, prefix=RTS_EXT_NS,
157  local_name='Properties'):
158  name, value = parse_properties_xml(c)
159  self._properties[name] = value
160  return self
161 
162  def parse_yaml(self, y):
163  '''Parse a YAML specification of a data port into this object.'''
164  self.name = y['name']
165  if RTS_EXT_NS_YAML + 'comment' in y:
166  self.comment = y[RTS_EXT_NS_YAML + 'comment']
167  self.visible = False
168  if RTS_EXT_NS_YAML + 'visible' in y:
169  visible = y.get(RTS_EXT_NS_YAML + 'visible')
170  if visible == True or visible == 'true' or visible == 'True':
171  self.visible = True
172  if RTS_EXT_NS_YAML + 'properties' in y:
173  for p in y.get(RTS_EXT_NS_YAML + 'properties'):
174  if 'value' in p:
175  value = p['value']
176  else:
177  value = None
178  self._properties[p['name']] = value
179  return self
180 
181  def save_xml(self, doc, element):
182  '''Save this data port into an xml.dom.Element object.'''
183  element.setAttributeNS(RTS_NS, RTS_NS_S + 'name', self.name)
184  if self.comment:
185  element.setAttributeNS(RTS_EXT_NS, RTS_EXT_NS_S + 'comment',
186  self.comment)
187  element.setAttributeNS(RTS_EXT_NS, RTS_EXT_NS_S + 'visible',
188  str(self.visible).lower())
189  for p in self.properties:
190  new_prop_element = doc.createElementNS(RTS_EXT_NS,
191  RTS_EXT_NS_S + 'Properties')
192  properties_to_xml(new_prop_element, p, self.properties[p])
193  element.appendChild(new_prop_element)
194 
195  def to_dict(self):
196  '''Save this data port into a dictionary.'''
197  d = {'name': self.name,
198  RTS_EXT_NS_YAML + 'visible': str(self.visible).lower()}
199  if self.comment:
200  d[RTS_EXT_NS_YAML + 'comment'] = self.comment
201  props = []
202  for name in self.properties:
203  p = {'name': name}
204  if self.properties[name]:
205  p['value'] = str(self.properties[name])
206  props.append(p)
207  if props:
208  d[RTS_EXT_NS_YAML + 'properties'] = props
209  return d
210 
211 
212 ##############################################################################
213 ## ServicePort object
214 
215 class ServicePort(object):
216  '''Represents a service port of a component, as specified in a
217  ConnectorProfile.
218 
219  '''
220 
221  def __init__(self, name='', comment='', visible=True):
222  '''Constructor.
223 
224  @param name Name of the port.
225  @type name str
226  @param comment A comment about the port.
227  @type comment str
228  @param visible If this port is visible in graphical displays.
229  @type visible bool
230 
231  '''
232  validate_attribute(name, 'serviceport.name',
233  expected_type=[str, unicode], required=False)
234  self._name = name
235  validate_attribute(comment, 'component.ext.comment',
236  expected_type=[str, unicode], required=False)
237  self._comment = comment
238  validate_attribute(visible, 'component.ext.visible',
239  expected_type=bool, required=False)
240  self._visible = visible
241  self._properties = {}
242 
243  def __str__(self):
244  result = 'Name: {0}\n'.format(self.name)
245  if self.comment:
246  result += ' Comment: {0}\n'.format(self.comment)
247  result += ' Visible: {0}\n'.format(self.visible)
248  if self.properties:
249  result += ' Properties:\n'
250  for p in self.properties:
251  result += ' {0}: {1}\n'.format(p, self.properties[p])
252  return result[:-1] # Lop off the last new line
253 
254  @property
255  def name(self):
256  '''The name of this service port.
257 
258  This name is used in connector profiles to identify the port.
259 
260  '''
261  return self._name
262 
263  @name.setter
264  def name(self, name):
265  validate_attribute(name, 'serviceport.name',
266  expected_type=[str, unicode], required=True)
267  self._name = name
268 
269  @property
270  def comment(self):
271  '''Comment about the service port.
272 
273  A brief comment about the service port. May or may not be displayed in
274  other tools. May be empty.
275 
276  Part of the extended profile.
277 
278  '''
279  return self._comment
280 
281  @comment.setter
282  def comment(self, comment):
283  validate_attribute(comment, 'serviceport.ext.comment',
284  expected_type=[str, unicode], required=False)
285  self._comment = comment
286 
287  @property
288  def visible(self):
289  '''Display the port in graphical tools.
290 
291  This value controls whether graphical tools will display this port or
292  not.
293 
294  Part of the extended profile.
295 
296  '''
297  return self._visible
298 
299  @visible.setter
300  def visible(self, visible):
301  validate_attribute(visible, 'serviceport.ext.visible',
302  expected_type=bool, required=False)
303  self._visible = visible
304 
305  @property
306  def properties(self):
307  '''Miscellaneous properties.
308 
309  Stores key/value pair properties.
310 
311  Part of the extended profile.
312 
313  '''
314  return self._properties
315 
316  @properties.setter
317  def properties(self, properties):
318  validate_attribute(properties, 'serviceport.ext.Properties',
319  expected_type=list, required=False)
320  self._properties = properties
321 
322  def parse_xml_node(self, node):
323  '''Parse an xml.dom Node object representing a service port into this
324  object.
325 
326  '''
327 
328  self.name = node.getAttributeNS(RTS_NS, 'name')
329  self.comment = node.getAttributeNS(RTS_EXT_NS, 'comment')
330  if node.hasAttributeNS(RTS_EXT_NS, 'visible'):
331  visible = node.getAttributeNS(RTS_EXT_NS, 'visible')
332  if visible.lower() == 'true' or visible == '1':
333  self.visible = True
334  else:
335  self.visible = False
336  for c in get_direct_child_elements_xml(node, prefix=RTS_EXT_NS,
337  local_name='Properties'):
338  name, value = parse_properties_xml(c)
339  self._properties[name] = value
340  return self
341 
342  def parse_yaml(self, y):
343  '''Parse a YAML specification of a service port into this object.'''
344  self.name = y['name']
345  if RTS_EXT_NS_YAML + 'comment' in y:
346  self.comment = y[RTS_EXT_NS_YAML + 'comment']
347  self.visible = False
348  if RTS_EXT_NS_YAML + 'visible' in y:
349  visible = y.get(RTS_EXT_NS_YAML + 'visible')
350  if visible == True or visible == 'true' or visible == 'True':
351  self.visible = True
352  if RTS_EXT_NS_YAML + 'properties' in y:
353  for p in y.get(RTS_EXT_NS_YAML + 'properties'):
354  if 'value' in p:
355  value = p['value']
356  else:
357  value = None
358  self._properties[p['name']] = value
359  return self
360 
361  def save_xml(self, doc, element):
362  '''Save this service port into an xml.dom.Element object.'''
363  element.setAttributeNS(RTS_NS, RTS_NS_S + 'name', self.name)
364  if self.comment:
365  element.setAttributeNS(RTS_EXT_NS, RTS_EXT_NS_S + 'comment',
366  self.comment)
367  element.setAttributeNS(RTS_EXT_NS, RTS_EXT_NS_S + 'visible',
368  str(self.visible).lower())
369  for p in self.properties:
370  new_prop_element = doc.createElementNS(RTS_EXT_NS,
371  RTS_EXT_NS_S + 'Properties')
372  properties_to_xml(new_prop_element, p, self.properties[p])
373  element.appendChild(new_prop_element)
374 
375  def to_dict(self):
376  '''Save this service port into a dictionary.'''
377  d = {'name': self.name,
378  RTS_EXT_NS_YAML + 'visible': str(self.visible).lower()}
379  if self.comment:
380  d[RTS_EXT_NS_YAML + 'comment'] = self.comment
381  props = []
382  for name in self.properties:
383  p = {'name': name}
384  if self.properties[name]:
385  p['value'] = str(self.properties[name])
386  props.append(p)
387  if props:
388  d[RTS_EXT_NS_YAML + 'properties'] = props
389  return d
390 
391 
392 # vim: tw=79
393 
def save_xml(self, doc, element)
Definition: ports.py:361
def __init__(self, name='', comment='', visible=True)
Definition: ports.py:221
def __init__(self, name='', comment='', visible=True)
Definition: ports.py:41
def properties(self)
Definition: ports.py:126
def save_xml(self, doc, element)
Definition: ports.py:181
def parse_yaml(self, y)
Definition: ports.py:162
def parse_xml_node(self, node)
Definition: ports.py:142
def properties_to_xml(element, name, value=None)
Definition: utils.py:86
def parse_properties_xml(node)
Definition: utils.py:77
DataPort object.
Definition: ports.py:35
def validate_attribute(attr, name, expected_type=None, required=False)
Definition: utils.py:92
def get_direct_child_elements_xml(node, prefix=None, local_name=None)
Definition: utils.py:50
ServicePort object.
Definition: ports.py:215
def parse_xml_node(self, node)
Definition: ports.py:322
def parse_yaml(self, y)
Definition: ports.py:342


rtsprofile
Author(s): Geoffrey Biggs
autogenerated on Fri Jun 7 2019 21:52:35