ports.py
Go to the documentation of this file.
00001 # -*- Python -*-
00002 # -*- coding: utf-8 -*-
00003 
00004 '''rtsprofile
00005 
00006 Copyright (C) 2009-2010
00007     Geoffrey Biggs
00008     RT-Synthesis Research Group
00009     Intelligent Systems Research Institute,
00010     National Institute of Advanced Industrial Science and Technology (AIST),
00011     Japan
00012     All rights reserved.
00013 Licensed under the Eclipse Public License -v 1.0 (EPL)
00014 http://www.opensource.org/licenses/eclipse-1.0.txt
00015 
00016 File: ports.py
00017 
00018 Objects representing ports in a component.
00019 
00020 '''
00021 
00022 __version__ = '$Revision: $'
00023 # $Source$
00024 
00025 
00026 from rtsprofile import RTS_NS, RTS_NS_S, RTS_EXT_NS, RTS_EXT_NS_S, \
00027                        RTS_EXT_NS_YAML
00028 from rtsprofile.utils import get_direct_child_elements_xml, \
00029                              parse_properties_xml, validate_attribute
00030 
00031 
00032 ##############################################################################
00033 ## DataPort object
00034 
00035 class DataPort(object):
00036     '''Represents a data port of a component, as specified in a
00037     ConnectorProfile.
00038 
00039     '''
00040 
00041     def __init__(self, name='', comment='', visible=True):
00042         '''Constructor.
00043 
00044         @param name Name of the port.
00045         @type name str
00046         @param comment A comment about the port.
00047         @type comment str
00048         @param visible If this port is visible in graphical displays.
00049         @type visible bool
00050 
00051         '''
00052         validate_attribute(name, 'dataPort.name',
00053                            expected_type=[str, unicode], required=False)
00054         self._name = name
00055         validate_attribute(comment, 'component.ext.comment',
00056                            expected_type=[str, unicode], required=False)
00057         self._comment = comment
00058         validate_attribute(visible, 'component.ext.visible',
00059                            expected_type=bool, required=False)
00060         self._visible = visible
00061         self._properties = {}
00062 
00063     def __str__(self):
00064         result = 'Name: {0}\n'.format(self.name)
00065         if self.comment:
00066             result += '  Comment: {0}\n'.format(self.comment)
00067         result += '  Visible: {0}\n'.format(self.visible)
00068         if self.properties:
00069             result += '  Properties:\n'
00070             for p in self.properties:
00071                 result += '    {0}: {1}\n'.format(p, self.properties[p])
00072         return result[:-1] # Lop off the last new line
00073 
00074     @property
00075     def name(self):
00076         '''The name of this data port.
00077 
00078         This name is used in connector profiles to identify the port.
00079 
00080         '''
00081         return self._name
00082 
00083     @name.setter
00084     def name(self, name):
00085         validate_attribute(name, 'dataPort.name',
00086                            expected_type=[str, unicode], required=True)
00087         self._name = name
00088 
00089     @property
00090     def comment(self):
00091         '''Comment about the data port.
00092 
00093         A brief comment about the data port. May or may not be displayed in
00094         other tools. May be empty.
00095 
00096         Part of the extended profile.
00097 
00098         '''
00099         return self._comment
00100 
00101     @comment.setter
00102     def comment(self, comment):
00103         validate_attribute(comment, 'dataPort.ext.comment',
00104                            expected_type=[str, unicode], required=False)
00105         self._comment = comment
00106 
00107     @property
00108     def visible(self):
00109         '''Display the port in graphical tools.
00110 
00111         This value controls whether graphical tools will display this port or
00112         not.
00113 
00114         Part of the extended profile.
00115 
00116         '''
00117         return self._visible
00118 
00119     @visible.setter
00120     def visible(self, visible):
00121         validate_attribute(visible, 'dataPort.ext.visible',
00122                            expected_type=bool, required=False)
00123         self._visible = visible
00124 
00125     @property
00126     def properties(self):
00127         '''Miscellaneous properties.
00128 
00129         Stores key/value pair properties.
00130 
00131         Part of the extended profile.
00132 
00133         '''
00134         return self._properties
00135 
00136     @properties.setter
00137     def properties(self, properties):
00138         validate_attribute(properties, 'dataPort.ext.Properties',
00139                            expected_type=list, required=False)
00140         self._properties = properties
00141 
00142     def parse_xml_node(self, node):
00143         '''Parse an xml.dom Node object representing a data port into this
00144         object.
00145 
00146         '''
00147 
00148         self.name = node.getAttributeNS(RTS_NS, 'name')
00149         self.comment = node.getAttributeNS(RTS_EXT_NS, 'comment')
00150         if node.hasAttributeNS(RTS_EXT_NS, 'visible'):
00151             visible = node.getAttributeNS(RTS_EXT_NS, 'visible')
00152             if visible.lower() == 'true' or visible == '1':
00153                 self.visible = True
00154             else:
00155                 self.visible = False
00156         for c in get_direct_child_elements_xml(node, prefix=RTS_EXT_NS,
00157                                                local_name='Properties'):
00158             name, value = parse_properties_xml(c)
00159             self._properties[name] = value
00160         return self
00161 
00162     def parse_yaml(self, y):
00163         '''Parse a YAML specification of a data port into this object.'''
00164         self.name = y['name']
00165         if RTS_EXT_NS_YAML + 'comment' in y:
00166             self.comment = y[RTS_EXT_NS_YAML + 'comment']
00167         self.visible = False
00168         if RTS_EXT_NS_YAML + 'visible' in y:
00169             visible = y.get(RTS_EXT_NS_YAML + 'visible')
00170             if visible == True or visible == 'true' or visible == 'True':
00171                 self.visible = True
00172         if RTS_EXT_NS_YAML + 'properties' in y:
00173             for p in y.get(RTS_EXT_NS_YAML + 'properties'):
00174                 if 'value' in p:
00175                     value = p['value']
00176                 else:
00177                     value = None
00178                 self._properties[p['name']] = value
00179         return self
00180 
00181     def save_xml(self, doc, element):
00182         '''Save this data port into an xml.dom.Element object.'''
00183         element.setAttributeNS(RTS_NS, RTS_NS_S + 'name', self.name)
00184         if self.comment:
00185             element.setAttributeNS(RTS_EXT_NS, RTS_EXT_NS_S + 'comment',
00186                                    self.comment)
00187         element.setAttributeNS(RTS_EXT_NS, RTS_EXT_NS_S + 'visible',
00188                                str(self.visible).lower())
00189         for p in self.properties:
00190             new_prop_element = doc.createElementNS(RTS_EXT_NS,
00191                                                    RTS_EXT_NS_S + 'Properties')
00192             properties_to_xml(new_prop_element, p, self.properties[p])
00193             element.appendChild(new_prop_element)
00194 
00195     def to_dict(self):
00196         '''Save this data port into a dictionary.'''
00197         d = {'name': self.name,
00198                 RTS_EXT_NS_YAML + 'visible': str(self.visible).lower()}
00199         if self.comment:
00200             d[RTS_EXT_NS_YAML + 'comment'] = self.comment
00201         props = []
00202         for name in self.properties:
00203             p = {'name': name}
00204             if self.properties[name]:
00205                 p['value'] = str(self.properties[name])
00206             props.append(p)
00207         if props:
00208             d[RTS_EXT_NS_YAML + 'properties'] = props
00209         return d
00210 
00211 
00212 ##############################################################################
00213 ## ServicePort object
00214 
00215 class ServicePort(object):
00216     '''Represents a service port of a component, as specified in a
00217     ConnectorProfile.
00218 
00219     '''
00220 
00221     def __init__(self, name='', comment='', visible=True):
00222         '''Constructor.
00223 
00224         @param name Name of the port.
00225         @type name str
00226         @param comment A comment about the port.
00227         @type comment str
00228         @param visible If this port is visible in graphical displays.
00229         @type visible bool
00230 
00231         '''
00232         validate_attribute(name, 'serviceport.name',
00233                            expected_type=[str, unicode], required=False)
00234         self._name = name
00235         validate_attribute(comment, 'component.ext.comment',
00236                            expected_type=[str, unicode], required=False)
00237         self._comment = comment
00238         validate_attribute(visible, 'component.ext.visible',
00239                            expected_type=bool, required=False)
00240         self._visible = visible
00241         self._properties = {}
00242 
00243     def __str__(self):
00244         result = 'Name: {0}\n'.format(self.name)
00245         if self.comment:
00246             result += '  Comment: {0}\n'.format(self.comment)
00247         result += '  Visible: {0}\n'.format(self.visible)
00248         if self.properties:
00249             result += '  Properties:\n'
00250             for p in self.properties:
00251                 result += '    {0}: {1}\n'.format(p, self.properties[p])
00252         return result[:-1] # Lop off the last new line
00253 
00254     @property
00255     def name(self):
00256         '''The name of this service port.
00257 
00258         This name is used in connector profiles to identify the port.
00259 
00260         '''
00261         return self._name
00262 
00263     @name.setter
00264     def name(self, name):
00265         validate_attribute(name, 'serviceport.name',
00266                            expected_type=[str, unicode], required=True)
00267         self._name = name
00268 
00269     @property
00270     def comment(self):
00271         '''Comment about the service port.
00272 
00273         A brief comment about the service port. May or may not be displayed in
00274         other tools. May be empty.
00275 
00276         Part of the extended profile.
00277 
00278         '''
00279         return self._comment
00280 
00281     @comment.setter
00282     def comment(self, comment):
00283         validate_attribute(comment, 'serviceport.ext.comment',
00284                            expected_type=[str, unicode], required=False)
00285         self._comment = comment
00286 
00287     @property
00288     def visible(self):
00289         '''Display the port in graphical tools.
00290 
00291         This value controls whether graphical tools will display this port or
00292         not.
00293 
00294         Part of the extended profile.
00295 
00296         '''
00297         return self._visible
00298 
00299     @visible.setter
00300     def visible(self, visible):
00301         validate_attribute(visible, 'serviceport.ext.visible',
00302                            expected_type=bool, required=False)
00303         self._visible = visible
00304 
00305     @property
00306     def properties(self):
00307         '''Miscellaneous properties.
00308 
00309         Stores key/value pair properties.
00310 
00311         Part of the extended profile.
00312 
00313         '''
00314         return self._properties
00315 
00316     @properties.setter
00317     def properties(self, properties):
00318         validate_attribute(properties, 'serviceport.ext.Properties',
00319                            expected_type=list, required=False)
00320         self._properties = properties
00321 
00322     def parse_xml_node(self, node):
00323         '''Parse an xml.dom Node object representing a service port into this
00324         object.
00325 
00326         '''
00327 
00328         self.name = node.getAttributeNS(RTS_NS, 'name')
00329         self.comment = node.getAttributeNS(RTS_EXT_NS, 'comment')
00330         if node.hasAttributeNS(RTS_EXT_NS, 'visible'):
00331             visible = node.getAttributeNS(RTS_EXT_NS, 'visible')
00332             if visible.lower() == 'true' or visible == '1':
00333                 self.visible = True
00334             else:
00335                 self.visible = False
00336         for c in get_direct_child_elements_xml(node, prefix=RTS_EXT_NS,
00337                                                local_name='Properties'):
00338             name, value = parse_properties_xml(c)
00339             self._properties[name] = value
00340         return self
00341 
00342     def parse_yaml(self, y):
00343         '''Parse a YAML specification of a service port into this object.'''
00344         self.name = y['name']
00345         if RTS_EXT_NS_YAML + 'comment' in y:
00346             self.comment = y[RTS_EXT_NS_YAML + 'comment']
00347         self.visible = False
00348         if RTS_EXT_NS_YAML + 'visible' in y:
00349             visible = y.get(RTS_EXT_NS_YAML + 'visible')
00350             if visible == True or visible == 'true' or visible == 'True':
00351                 self.visible = True
00352         if RTS_EXT_NS_YAML + 'properties' in y:
00353             for p in y.get(RTS_EXT_NS_YAML + 'properties'):
00354                 if 'value' in p:
00355                     value = p['value']
00356                 else:
00357                     value = None
00358                 self._properties[p['name']] = value
00359         return self
00360 
00361     def save_xml(self, doc, element):
00362         '''Save this service port into an xml.dom.Element object.'''
00363         element.setAttributeNS(RTS_NS, RTS_NS_S + 'name', self.name)
00364         if self.comment:
00365             element.setAttributeNS(RTS_EXT_NS, RTS_EXT_NS_S + 'comment',
00366                                    self.comment)
00367         element.setAttributeNS(RTS_EXT_NS, RTS_EXT_NS_S + 'visible',
00368                                str(self.visible).lower())
00369         for p in self.properties:
00370             new_prop_element = doc.createElementNS(RTS_EXT_NS,
00371                                                    RTS_EXT_NS_S + 'Properties')
00372             properties_to_xml(new_prop_element, p, self.properties[p])
00373             element.appendChild(new_prop_element)
00374 
00375     def to_dict(self):
00376         '''Save this service port into a dictionary.'''
00377         d = {'name': self.name,
00378                 RTS_EXT_NS_YAML + 'visible': str(self.visible).lower()}
00379         if self.comment:
00380             d[RTS_EXT_NS_YAML + 'comment'] = self.comment
00381         props = []
00382         for name in self.properties:
00383             p = {'name': name}
00384             if self.properties[name]:
00385                 p['value'] = str(self.properties[name])
00386             props.append(p)
00387         if props:
00388             d[RTS_EXT_NS_YAML + 'properties'] = props
00389         return d
00390 
00391 
00392 # vim: tw=79
00393 


rtsprofile
Author(s): Geoffrey Biggs
autogenerated on Sun Mar 26 2017 03:39:43