port_connectors.py
Go to the documentation of this file.
00001 # -*- Python -*- # -*- coding: utf-8 -*-
00002 
00003 '''rtsprofile
00004 
00005 Copyright (C) 2009-2010
00006     Geoffrey Biggs
00007     RT-Synthesis Research Group
00008     Intelligent Systems Research Institute,
00009     National Institute of Advanced Industrial Science and Technology (AIST),
00010     Japan
00011     All rights reserved.
00012 Licensed under the Eclipse Public License -v 1.0 (EPL)
00013 http://www.opensource.org/licenses/eclipse-1.0.txt
00014 
00015 File: data_port_connector.py
00016 
00017 Objects representing connections between data and service ports.
00018 
00019 '''
00020 
00021 __version__ = '$Revision: $'
00022 # $Source$
00023 
00024 
00025 from rtsprofile import RTS_NS, RTS_NS_S, RTS_EXT_NS, RTS_EXT_NS_S, \
00026                        RTS_EXT_NS_YAML
00027 from rtsprofile.exceptions import InvalidDataPortConnectorNodeError, \
00028                                   InvalidServicePortConnectorNodeError
00029 from rtsprofile.targets import TargetPort
00030 from rtsprofile.utils import get_direct_child_elements_xml, \
00031                              indent_string, parse_properties_xml, \
00032                              properties_to_xml, validate_attribute
00033 
00034 
00035 ##############################################################################
00036 ## DataPortConnector object
00037 
00038 class DataPortConnector(object):
00039     '''Represents a connection between data ports.'''
00040 
00041     def __init__(self, connector_id='', name='', data_type='',
00042             interface_type='', data_flow_type='', subscription_type='',
00043             push_interval=0.0, source_data_port=TargetPort(),
00044             target_data_port=TargetPort(), comment='', visible=True):
00045         '''Constructor.
00046 
00047         @param connector_id ID of the connector.
00048         @type connector_id str
00049         @param name Name of the connector.
00050         @type name str
00051         @param data_type Data type that this connector transports.
00052         @type data_type str
00053         @param interface_type Interface type of the connected ports.
00054         @type interface_type str
00055         @param data_flow_type Type of data flow between the ports.
00056         @type data_flow_type str
00057         @param subscription_type Type of subscription between the ports.
00058         @type subscription_type str
00059         @param push_interval Rate at which data is sent between the ports.
00060         @type push_interval float
00061         @param source_data_port The source port in the connection.
00062         @type source_data_port TargetPort
00063         @param target_data_port The target port in the connection.
00064         @type target_data_port TargetPort
00065         @param comment A comment about the port connector.
00066         @type comment str
00067         @param visible If this connector is visible in graphical displays.
00068         @type visible bool
00069 
00070         '''
00071         validate_attribute(connector_id, 'dataport_connector.connectorID',
00072                            expected_type=[str, unicode], required=False)
00073         self._connector_id = connector_id
00074         validate_attribute(name, 'dataport_connector.name',
00075                            expected_type=[str, unicode], required=False)
00076         self._name = name
00077         validate_attribute(data_type, 'dataport_connector.dataType',
00078                            expected_type=[str, unicode], required=False)
00079         self._data_type = data_type
00080         validate_attribute(interface_type, 'dataport_connector.interfaceType',
00081                            expected_type=[str, unicode], required=False)
00082         self._interface_type = interface_type
00083         validate_attribute(data_flow_type, 'dataport_connector.dataflowType',
00084                            expected_type=[str, unicode], required=False)
00085         self._data_flow_type = data_flow_type
00086         validate_attribute(subscription_type,
00087                            'dataport_connector.subscriptionType',
00088                            expected_type=[str, unicode], required=False)
00089         self._subscription_type = subscription_type
00090         validate_attribute(push_interval, 'dataport_connector.pushInterval',
00091                            expected_type=[int, float], required=False)
00092         self._push_interval = push_interval
00093         validate_attribute(source_data_port,
00094                            'dataport_connector.sourceDataPort',
00095                            expected_type=TargetPort, required=False)
00096         self._source_data_port = source_data_port
00097         validate_attribute(target_data_port,
00098                            'dataport_connector.targetDataPort',
00099                            expected_type=TargetPort, required=False)
00100         self._target_data_port = target_data_port
00101         validate_attribute(comment, 'component.ext.comment',
00102                            expected_type=[str, unicode], required=False)
00103         self._comment = comment
00104         validate_attribute(visible, 'component.ext.visible',
00105                            expected_type=bool, required=False)
00106         self._visible = visible
00107         self._properties = {}
00108 
00109     def __str__(self):
00110         result = 'Name: {1}\n  Connector ID: {0}\n  Data type: {2}\n  \
00111 Interface type: {3}\n  Data flow type: {4}\n  Subscription type: {5}\n  Push \
00112 interval: {6}\n  Source data port:\n{7}\n  Target data port:\n{8}\n'.format(\
00113             self.connector_id, self.name, self.data_type, self.interface_type,
00114             self.data_flow_type, self.subscription_type, self.push_interval,
00115             indent_string(str(self.source_data_port), num_spaces=4),
00116             indent_string(str(self.target_data_port), num_spaces=4))
00117         if self.comment:
00118             result += 'Comment: {0}\n'.format(self.comment)
00119         result += 'Visible: {0}\n'.format(self.visible)
00120         if self.properties:
00121             result += 'Properties:\n'
00122             for p in self.properties:
00123                 result += '  {0}: {1}\n'.format(p, self.properties[p])
00124         return result[:-1] # Lop off the last new line
00125 
00126     @property
00127     def connector_id(self):
00128         '''The ID of the connector used to distinguish it in the RT system.'''
00129         return self._connector_id
00130 
00131     @connector_id.setter
00132     def connector_id(self, connector_id):
00133         validate_attribute(connector_id, 'dataport_connector.connectorID',
00134                            expected_type=[str, unicode], required=True)
00135         self._connector_id = connector_id
00136 
00137     @property
00138     def name(self):
00139         '''The name of the connector.'''
00140         return self._name
00141 
00142     @name.setter
00143     def name(self, name):
00144         validate_attribute(name, 'dataport_connector.name',
00145                            expected_type=[str, unicode], required=True)
00146         self._name = name
00147 
00148     @property
00149     def data_type(self):
00150         '''Data type that this connector transports.'''
00151         return self._data_type
00152 
00153     @data_type.setter
00154     def data_type(self, data_type):
00155         validate_attribute(data_type, 'dataport_connector.dataType',
00156                            expected_type=[str, unicode], required=True)
00157         self._data_type = data_type
00158 
00159     @property
00160     def interface_type(self):
00161         '''Interface type of the connection.
00162 
00163         As specified when the RT system is created. Dependent on what the RT
00164         Middleware used to execute the RT system supports.
00165 
00166         '''
00167         return self._interface_type
00168 
00169     @interface_type.setter
00170     def interface_type(self, interface_type):
00171         validate_attribute(interface_type, 'dataport_connector.interfaceType',
00172                            expected_type=[str, unicode], required=True)
00173         self._interface_type = interface_type
00174 
00175     @property
00176     def data_flow_type(self):
00177         '''Type of data flow between the ports.
00178 
00179         As specified when the RT system is created. Dependent on what the RT
00180         Middleware used to execute the RT system supports.
00181 
00182         '''
00183         return self._data_flow_type
00184 
00185     @data_flow_type.setter
00186     def data_flow_type(self, data_flow_type):
00187         validate_attribute(data_flow_type, 'dataport_connector.dataflowType',
00188                            expected_type=[str, unicode], required=True)
00189         self._data_flow_type = data_flow_type
00190 
00191     @property
00192     def subscription_type(self):
00193         '''Type of the subscription between the ports.
00194 
00195         As specified when the RT system is created. Only used when @ref
00196         data_flow_type is set to PUSH. Dependent on what the RT Middleware used
00197         to execute the RT system supports.
00198 
00199         '''
00200         return self._subscription_type
00201 
00202     @subscription_type.setter
00203     def subscription_type(self, subscription_type):
00204         validate_attribute(subscription_type,
00205                            'dataport_connector.subscriptionType',
00206                            expected_type=[str, unicode], required=False)
00207         self._subscription_type = subscription_type
00208 
00209     @property
00210     def push_interval(self):
00211         '''Rate at which data is sent between ports.
00212 
00213         As specified when the RT system is created.
00214 
00215         '''
00216         return self._push_interval
00217 
00218     @push_interval.setter
00219     def push_interval(self, push_interval):
00220         validate_attribute(push_interval, 'dataport_connector.pushInterval',
00221                            expected_type=[int, float], required=False)
00222         self._push_interval = push_interval
00223 
00224     @property
00225     def source_data_port(self):
00226         '''The source port in the connection.'''
00227         return self._source_data_port
00228 
00229     @source_data_port.setter
00230     def source_data_port(self, source_data_port):
00231         validate_attribute(source_data_port,
00232                            'dataport_connector.sourceDataPort',
00233                            expected_type=TargetPort, required=True)
00234         self._source_data_port = source_data_port
00235 
00236     @property
00237     def target_data_port(self):
00238         '''The target port in the connection.'''
00239         return self._target_data_port
00240 
00241     @target_data_port.setter
00242     def target_data_port(self, target_data_port):
00243         validate_attribute(target_data_port,
00244                            'dataport_connector.targetDataPort',
00245                            expected_type=TargetPort, required=True)
00246         self._target_data_port = target_data_port
00247 
00248     @property
00249     def comment(self):
00250         '''Comment about the connector.
00251 
00252         A brief comment about the connector. May or may not be displayed in
00253         other tools. May be empty.
00254 
00255         Part of the extended profile.
00256 
00257         '''
00258         return self._comment
00259 
00260     @comment.setter
00261     def comment(self, comment):
00262         validate_attribute(comment, 'dataport_connector.ext.comment',
00263                            expected_type=[str, unicode], required=False)
00264         self._comment = comment
00265 
00266     @property
00267     def visible(self):
00268         '''Display the connector in graphical tools.
00269 
00270         This value controls whether graphical tools will display this connector
00271         or not.
00272 
00273         Part of the extended profile.
00274 
00275         '''
00276         return self._visible
00277 
00278     @visible.setter
00279     def visible(self, visible):
00280         validate_attribute(visible, 'dataport_connector.ext.visible',
00281                            expected_type=bool, required=False)
00282         self._visible = visible
00283 
00284     @property
00285     def properties(self):
00286         '''Miscellaneous properties.
00287 
00288         Stores key/value pair properties.
00289 
00290         Part of the extended profile.
00291 
00292         '''
00293         return self._properties
00294 
00295     @properties.setter
00296     def properties(self, properties):
00297         validate_attribute(properties, 'dataport_connector.ext.Properties',
00298                            expected_type=dict, required=False)
00299         self._properties = properties
00300 
00301     def parse_xml_node(self, node):
00302         '''Parse an xml.dom Node object representing a data connector into this
00303         object.
00304 
00305         '''
00306         self.connector_id = node.getAttributeNS(RTS_NS, 'connectorId')
00307         self.name = node.getAttributeNS(RTS_NS, 'name')
00308         self.data_type = node.getAttributeNS(RTS_NS, 'dataType')
00309         self.interface_type = node.getAttributeNS(RTS_NS, 'interfaceType')
00310         self.data_flow_type = node.getAttributeNS(RTS_NS, 'dataflowType')
00311         if node.hasAttributeNS(RTS_NS, 'subscriptionType'):
00312             self.subscription_type = node.getAttributeNS(RTS_NS,
00313                                                          'subscriptionType')
00314         else:
00315             self.subscription_type = ''
00316         if node.hasAttributeNS(RTS_NS, 'pushInterval'):
00317             self.push_interval = float(node.getAttributeNS(RTS_NS,
00318                                                            'pushInterval'))
00319         else:
00320             self.push_interval = 0.0
00321         self.comment = node.getAttributeNS(RTS_EXT_NS, 'comment')
00322         if node.hasAttributeNS(RTS_EXT_NS, 'visible'):
00323             visible = node.getAttributeNS(RTS_EXT_NS, 'visible')
00324             if visible == 'true' or visible == '1':
00325                 self.visible = True
00326             else:
00327                 self.visible = False
00328 
00329         if node.getElementsByTagNameNS(RTS_NS, 'sourceDataPort').length != 1:
00330             raise InvalidDataPortConnectorNodeError
00331         self.source_data_port = TargetPort().parse_xml_node(\
00332                 node.getElementsByTagNameNS(RTS_NS, 'sourceDataPort')[0])
00333         if node.getElementsByTagNameNS(RTS_NS, 'targetDataPort').length != 1:
00334             raise InvalidDataPortConnectorNodeError
00335         self.target_data_port = TargetPort().parse_xml_node(\
00336                 node.getElementsByTagNameNS(RTS_NS, 'targetDataPort')[0])
00337         for c in get_direct_child_elements_xml(node, prefix=RTS_EXT_NS,
00338                                                local_name='Properties'):
00339             name, value = parse_properties_xml(c)
00340             self._properties[name] = value
00341         return self
00342 
00343     def parse_yaml(self, y):
00344         '''Parse a YAML specification of a data port connector into this
00345         object.
00346 
00347         '''
00348         self.connector_id = y['connectorId']
00349         self.name = y['name']
00350         self.data_type = y['dataType']
00351         self.interface_type = y['interfaceType']
00352         self.data_flow_type = y['dataflowType']
00353         if 'subscriptionType' in y:
00354             self.subscription_type = y['subscriptionType']
00355         else:
00356             self.subscription_type = ''
00357         if 'pushInterval' in y:
00358             self.push_interval = float(y['pushInterval'])
00359         else:
00360             self.push_interval = 0.0
00361         if RTS_EXT_NS_YAML + 'comment' in y:
00362             self.comment = y[RTS_EXT_NS_YAML + 'comment']
00363         else:
00364             self.comment = ''
00365         self.visible = False
00366         if RTS_EXT_NS_YAML + 'visible' in y:
00367             visible = y[RTS_EXT_NS_YAML + 'visible']
00368             if visible == 'true' or visible == '1':
00369                 self.visible = True
00370         if not 'sourceDataPort' in y:
00371             raise InvalidDataPortConnectorNodeError
00372         self.source_data_port = \
00373                 TargetPort().parse_yaml(y['sourceDataPort'])
00374         if not 'targetDataPort' in y:
00375             raise InvalidDataPortConnectorNodeError
00376         self.target_data_port = \
00377                 TargetPort().parse_yaml(y['targetDataPort'])
00378         if RTS_EXT_NS_YAML + 'properties' in y:
00379             for p in y[RTS_EXT_NS_YAML + 'properties']:
00380                 if 'value' in p:
00381                     value = p['value']
00382                 else:
00383                     value = None
00384                 self._properties[p['name']] = value
00385         return self
00386 
00387     def save_xml(self, doc, element):
00388         '''Save this data port into an xml.dom.Element object.'''
00389         element.setAttributeNS(RTS_NS, RTS_NS_S + 'connectorId',
00390                                self.connector_id)
00391         element.setAttributeNS(RTS_NS, RTS_NS_S + 'name', self.name)
00392         element.setAttributeNS(RTS_NS, RTS_NS_S + 'dataType', self.data_type)
00393         element.setAttributeNS(RTS_NS, RTS_NS_S + 'interfaceType',
00394                                self.interface_type)
00395         element.setAttributeNS(RTS_NS, RTS_NS_S + 'dataflowType',
00396                                self.data_flow_type)
00397         if self.subscription_type:
00398             element.setAttributeNS(RTS_NS, RTS_NS_S + 'subscriptionType',
00399                                    self.subscription_type)
00400         element.setAttributeNS(RTS_NS, RTS_NS_S + 'pushInterval',
00401                                str(self.push_interval))
00402         if self.comment:
00403             element.setAttributeNS(RTS_EXT_NS, RTS_EXT_NS_S + 'comment',
00404                                    self.comment)
00405         element.setAttributeNS(RTS_EXT_NS, RTS_EXT_NS_S + 'visible',
00406                                str(self.visible).lower())
00407         new_element = doc.createElementNS(RTS_NS, RTS_NS_S + 'sourceDataPort')
00408         self.source_data_port.save_xml(doc, new_element)
00409         element.appendChild(new_element)
00410         new_element = doc.createElementNS(RTS_NS, RTS_NS_S + 'targetDataPort')
00411         self.target_data_port.save_xml(doc, new_element)
00412         element.appendChild(new_element)
00413         for p in self.properties:
00414             new_prop_element = doc.createElementNS(RTS_EXT_NS,
00415                                                    RTS_EXT_NS + 'Properties')
00416             properties_to_xml(new_prop_element, p, self.properties[p])
00417             element.appendChild(new_prop_element)
00418 
00419     def to_dict(self):
00420         '''Save this data port connector into a dictionary.'''
00421         d = {'connectorId': self.connector_id,
00422                 'name': self.name,
00423                 'dataType': self.data_type,
00424                 'interfaceType': self.interface_type,
00425                 'dataflowType': self.data_flow_type,
00426                 RTS_EXT_NS_YAML + 'visible': str(self.visible).lower(),
00427                 'sourceDataPort': self.source_data_port.to_dict(),
00428                 'targetDataPort': self.target_data_port.to_dict()}
00429         if self.subscription_type:
00430             d['subscriptionType'] = self.subscription_type
00431         if self.push_interval:
00432             d['pushInterval'] = self.push_interval
00433         if self.comment:
00434             d[RTS_EXT_NS_YAML + 'comment'] = self.comment
00435         props = []
00436         for name in self.properties:
00437             p = {'name': name}
00438             if self.properties[name]:
00439                 p['value'] = str(self.properties[name])
00440             props.append(p)
00441         if props:
00442             d[RTS_EXT_NS_YAML + 'properties'] = props
00443         return d
00444 
00445 
00446 ##############################################################################
00447 ## ServicePortConnector object
00448 
00449 class ServicePortConnector(object):
00450     '''Represents a connection between service ports.'''
00451 
00452     def __init__(self, connector_id='', name='', trans_method='',
00453             source_service_port=TargetPort(),
00454             target_service_port=TargetPort(), comment='', visible=True):
00455         '''Constructor.
00456 
00457         @param connector_id ID of the connector.
00458         @type connector_id str
00459         @param name Name of the connector.
00460         @type name str
00461         @param trans_method Transport method used by the ports.
00462         @type trans_method str
00463         @param source_service_port The source port in the connection.
00464         @type source_service_port TargetPort
00465         @param target_service_port The target port in the connection.
00466         @type target_service_port TargetPort
00467         @param comment A comment about the port connector.
00468         @type comment str
00469         @param visible If this connector is visible in graphical displays.
00470         @type visible bool
00471 
00472         '''
00473         validate_attribute(connector_id, 'serviceport_connector.connectorID',
00474                            expected_type=[str, unicode], required=False)
00475         self._connector_id = connector_id
00476         validate_attribute(name, 'serviceport_connector.name',
00477                            expected_type=[str, unicode], required=False)
00478         self._name = name
00479         validate_attribute(trans_method, 'serviceport_connector.transMethod',
00480                            expected_type=[str, unicode], required=False)
00481         self._trans_method = trans_method
00482         validate_attribute(source_service_port,
00483                            'serviceport_connector.sourceServicePort',
00484                            expected_type=TargetPort, required=True)
00485         self._source_service_port = source_service_port
00486         validate_attribute(target_service_port,
00487                            'serviceport_connector.targetServicePort',
00488                            expected_type=TargetPort, required=True)
00489         self._target_service_port = target_service_port
00490         validate_attribute(comment, 'component.ext.comment',
00491                            expected_type=[str, unicode], required=False)
00492         self._comment = comment
00493         validate_attribute(visible, 'component.ext.visible',
00494                            expected_type=bool, required=False)
00495         self._visible = visible
00496         self._properties = {}
00497 
00498 
00499     def __str__(self):
00500         result = 'Name: {1}\n  Connector ID: {0}\n  Trans method: {2}\n  \
00501 Source data port:\n{3}\n  Target data port:\n{4}'.format(self.connector_id,
00502             self.name, self.trans_method,
00503             indent_string(str(self.source_service_port), num_spaces=4),
00504             indent_string(str(self.target_service_port), num_spaces=4))
00505         if self.comment:
00506             result += 'Comment: {0}\n'.format(self.comment)
00507         result += 'Visible: {0}\n'.format(self.visible)
00508         if self.properties:
00509             result += 'Properties:\n'
00510             for p in self.properties:
00511                 result += '  {0}: {1}\n'.format(p, self.properties[p])
00512         return result[:-1] # Lop off the last new line
00513 
00514     @property
00515     def connector_id(self):
00516         '''The ID of the connector used to distinguish it in the RT system.'''
00517         return self._connector_id
00518 
00519     @connector_id.setter
00520     def connector_id(self, connector_id):
00521         validate_attribute(connector_id, 'serviceport_connector.connectorID',
00522                            expected_type=[str, unicode], required=True)
00523         self._connector_id = connector_id
00524 
00525     @property
00526     def name(self):
00527         '''The name of the connector.'''
00528         return self._name
00529 
00530     @name.setter
00531     def name(self, name):
00532         validate_attribute(name, 'serviceport_connector.name',
00533                            expected_type=[str, unicode], required=True)
00534         self._name = name
00535 
00536     @property
00537     def trans_method(self):
00538         '''Transport method used by the ports.
00539 
00540         As specified when the RT system is created. Dependent on what the RT
00541         Middleware used to execute the RT system supports.
00542 
00543         '''
00544         return self._trans_method
00545 
00546     @trans_method.setter
00547     def trans_method(self, trans_method):
00548         validate_attribute(trans_method, 'serviceport_connector.transMethod',
00549                            expected_type=[str, unicode], required=False)
00550         self._trans_method = trans_method
00551 
00552     @property
00553     def source_service_port(self):
00554         '''The source port in the connection.'''
00555         return self._source_service_port
00556 
00557     @source_service_port.setter
00558     def source_service_port(self, source_service_port):
00559         validate_attribute(source_service_port,
00560                            'serviceport_connector.sourceServicePort',
00561                            expected_type=TargetPort, required=True)
00562         self._source_service_port = source_service_port
00563 
00564     @property
00565     def target_service_port(self):
00566         '''The target port in the connection.'''
00567         return self._target_service_port
00568 
00569     @target_service_port.setter
00570     def target_service_port(self, target_service_port):
00571         validate_attribute(target_service_port,
00572                            'serviceport_connector.targetServicePort',
00573                            expected_type=TargetPort, required=True)
00574         self._target_service_port = target_service_port
00575 
00576     @property
00577     def comment(self):
00578         '''Comment about the connector.
00579 
00580         A brief comment about the connector. May or may not be displayed in
00581         other tools. May be empty.
00582 
00583         Part of the extended profile.
00584 
00585         '''
00586         return self._comment
00587 
00588     @comment.setter
00589     def comment(self, comment):
00590         validate_attribute(comment, 'serviceport_connector.ext.comment',
00591                            expected_type=[str, unicode], required=False)
00592         self._comment = comment
00593 
00594     @property
00595     def visible(self):
00596         '''Display the connector in graphical tools.
00597 
00598         This value controls whether graphical tools will display this connector
00599         or not.
00600 
00601         Part of the extended profile.
00602 
00603         '''
00604         return self._visible
00605 
00606     @visible.setter
00607     def visible(self, visible):
00608         validate_attribute(visible, 'serviceport_connector.ext.visible',
00609                            expected_type=bool, required=False)
00610         self._visible = visible
00611 
00612     @property
00613     def properties(self):
00614         '''Miscellaneous properties.
00615 
00616         Stores key/value pair properties.
00617 
00618         Part of the extended profile.
00619 
00620         '''
00621         return self._properties
00622 
00623     @properties.setter
00624     def properties(self, properties):
00625         validate_attribute(properties, 'serviceport_connector.ext.Properties',
00626                            expected_type=dict, required=False)
00627         self._properties = properties
00628 
00629     def parse_xml_node(self, node):
00630         '''Parse an xml.dom Node object representing a service port connector into
00631         this object.
00632 
00633         '''
00634         self.connector_id = node.getAttributeNS(RTS_NS, 'connectorId')
00635         self.name = node.getAttributeNS(RTS_NS, 'name')
00636         if node.hasAttributeNS(RTS_NS, 'transMethod'):
00637             self.trans_method = node.getAttributeNS(RTS_NS,
00638                                                          'transMethod')
00639         else:
00640             self.trans_method = ''
00641         self.comment = node.getAttributeNS(RTS_EXT_NS, 'comment')
00642         if node.hasAttributeNS(RTS_EXT_NS, 'visible'):
00643             visible = node.getAttributeNS(RTS_EXT_NS, 'visible')
00644             if visible == 'true' or visible == '1':
00645                 self.visible = True
00646             else:
00647                 self.visible = False
00648 
00649         if node.getElementsByTagNameNS(RTS_NS, 'sourceServicePort').length != 1:
00650             raise InvalidServicePortConnectorNodeError
00651         self.source_service_port = TargetPort().parse_xml_node(\
00652                 node.getElementsByTagNameNS(RTS_NS, 'sourceServicePort')[0])
00653         if node.getElementsByTagNameNS(RTS_NS, 'targetServicePort').length != 1:
00654             raise InvalidServicePortConnectorNodeError
00655         self.target_service_port = TargetPort().parse_xml_node(\
00656                 node.getElementsByTagNameNS(RTS_NS, 'targetServicePort')[0])
00657         for c in get_direct_child_elements_xml(node, prefix=RTS_EXT_NS,
00658                                                local_name='Properties'):
00659             name, value = parse_properties_xml(c)
00660             self._properties[name] = value
00661         return self
00662 
00663     def parse_yaml(self, y):
00664         '''Parse a YAML specification of a service port connector into this
00665         object.
00666 
00667         '''
00668         self.connector_id = y['connectorId']
00669         self.name = y['name']
00670         if 'transMethod' in y:
00671             self.trans_method = y['transMethod']
00672         else:
00673             self.trans_method = ''
00674         if RTS_EXT_NS_YAML + 'comment' in y:
00675             self.comment = y[RTS_EXT_NS_YAML + 'comment']
00676         else:
00677             self.comment = ''
00678         self.visible = False
00679         if RTS_EXT_NS_YAML + 'visible' in y:
00680             visible = y[RTS_EXT_NS_YAML + 'visible']
00681             if visible == 'true' or visible == '1':
00682                 self.visible = True
00683         if 'sourceServicePort' not in y:
00684             raise InvalidServicePortConnectorNodeError
00685         self.source_service_port = \
00686                 TargetPort().parse_yaml(y['sourceServicePort'])
00687         if 'targetServicePort' not in y:
00688             raise InvalidServicePortConnectorNodeError
00689         self.target_service_port = \
00690                 TargetPort().parse_yaml(y['targetServicePort'])
00691         if RTS_EXT_NS_YAML + 'properties' in y:
00692             for p in y[RTS_EXT_NS_YAML + 'properties']:
00693                 if 'value' in p:
00694                     value = p['value']
00695                 else:
00696                     value = None
00697                 self._properties[p['name']] = value
00698         return self
00699 
00700     def save_xml(self, doc, element):
00701         '''Save this service port into an xml.dom.Element object.'''
00702         element.setAttributeNS(RTS_NS, RTS_NS_S + 'connectorId',
00703                                self.connector_id)
00704         element.setAttributeNS(RTS_NS, RTS_NS_S + 'name', self.name)
00705         if self.trans_method:
00706             element.setAttributeNS(RTS_NS, RTS_NS_S + 'transMethod',
00707                                    self.trans_method)
00708         if self.comment:
00709             element.setAttributeNS(RTS_EXT_NS, RTS_EXT_NS_S + 'comment',
00710                                    self.comment)
00711         element.setAttributeNS(RTS_EXT_NS, RTS_EXT_NS_S + 'visible',
00712                                str(self.visible).lower())
00713         new_element = doc.createElementNS(RTS_NS,
00714                                           RTS_NS_S + 'sourceServicePort')
00715         self.source_service_port.save_xml(doc, new_element)
00716         element.appendChild(new_element)
00717         new_element = doc.createElementNS(RTS_NS,
00718                                           RTS_NS_S + 'targetServicePort')
00719         self.target_service_port.save_xml(doc, new_element)
00720         element.appendChild(new_element)
00721         for p in self.properties:
00722             new_prop_element = doc.createElementNS(RTS_EXT_NS,
00723                                                    RTS_EXT_NS_S + 'Properties')
00724             properties_to_xml(new_prop_element, p, self.properties[p])
00725             element.appendChild(new_prop_element)
00726 
00727     def to_dict(self):
00728         '''Save this service port connector into a dictionary.'''
00729         d = {'connectorId': self.connector_id,
00730                 'name': self.name,
00731                 RTS_EXT_NS_YAML + 'visible': str(self.visible).lower(),
00732                 'sourceServicePort': self.source_service_port.to_dict(),
00733                 'targetServicePort': self.target_service_port.to_dict()}
00734         if self.trans_method:
00735             d['transMethod'] = self.trans_method
00736         if self.comment:
00737             d[RTS_EXT_NS_YAML + 'comment'] = self.comment
00738         props = []
00739         for name in self.properties:
00740             p = {'name': name}
00741             if self.properties[name]:
00742                 p['value'] = str(self.properties[name])
00743             props.append(p)
00744         if props:
00745             d[RTS_EXT_NS_YAML + 'properties'] = props
00746         return d
00747 
00748 
00749 # vim: tw=79
00750 


rtsprofile
Author(s): Geoffrey Biggs
autogenerated on Thu Aug 27 2015 14:59:19