rts_profile.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: rtsystem.py
00017 
00018 Object representing an RT system. The object constructs itself using an XML
00019 parser and an XML specification meeting the RtsProfile schema.
00020 
00021 '''
00022 
00023 __version__ = '$Revision: $'
00024 # $Source$
00025 
00026 
00027 from datetime import datetime, MINYEAR
00028 import xml.dom
00029 import xml.dom.minidom
00030 import yaml
00031 
00032 from rtsprofile import RTS_NS, RTS_NS_S, RTS_EXT_NS, RTS_EXT_NS_S, \
00033                        RTS_EXT_NS_YAML
00034 from rtsprofile.component import Component
00035 from rtsprofile.component_group import ComponentGroup
00036 from rtsprofile.exceptions import MultipleSourcesError, \
00037                                   InvalidRtsProfileNodeError, RtsProfileError
00038 from rtsprofile.message_sending import StartUp, ShutDown, Activation, \
00039                                        Deactivation, Resetting, Initialize, \
00040                                        Finalize
00041 from rtsprofile.port_connectors import DataPortConnector, ServicePortConnector
00042 from rtsprofile.utils import date_to_dict, get_direct_child_elements_xml, \
00043                              indent_string, parse_properties_xml, \
00044                              properties_to_xml, validate_attribute
00045 
00046 
00047 ##############################################################################
00048 ## RtsProfile object
00049 
00050 class RtsProfile:
00051     def __init__(self, xml_spec=None, yaml_spec=None):
00052         '''Constructor.
00053 
00054         Pass in an RTSProfile specification either in a string or a file
00055         object, and the RtsProfile will be loaded from that.
00056 
00057         If no specification is provided, the RtsProfile will be constructed
00058         from the default values. Use the properties to set the values you need.
00059 
00060         @param xml_spec A string or open file object containing the XML
00061         specification of the RTS Profile. If present, the other arguments must
00062         be None.
00063 
00064         @param yaml_spec A string or open file object containing the YAML
00065         specification of the RTS Profile. If present, the other arguments must
00066         be None.
00067 
00068         '''
00069         if xml_spec:
00070             if yaml_spec:
00071                 raise MultipleSourcesError('XML and YAML specifications both \
00072 given.')
00073             self.parse_from_xml(xml_spec)
00074         elif yaml_spec:
00075             if xml_spec:
00076                 raise MultipleSourcesError('XML and YAML specifications both \
00077 given.')
00078             self.parse_from_yaml(yaml_spec)
00079         else:
00080             self._reset()
00081 
00082     def __str__(self):
00083         result = 'ID: {0}\nAbstract: {1}\nCreation date: {2}\n\
00084 Update date: {3}\nVersion: {4}\n'.format(self.id, self.abstract,
00085                                          self.creation_date, self.update_date,
00086                                          self.version)
00087         if self.components:
00088             result += 'Components:\n'
00089             for c in self.components:
00090                 result += '{0}\n'.format(indent_string(str(c)))
00091         if self.groups:
00092             result += 'Groups:\n'
00093             for g in self.groups:
00094                 result += '{0}\n'.format(indent_string(str(g)))
00095         if self.data_port_connectors:
00096             result += 'Data port connectors:\n'
00097             for c in self.data_port_connectors:
00098                 result += '{0}\n'.format(indent_string(str(c)))
00099         if self.service_port_connectors:
00100             result += 'Service port connectors:\n'
00101             for c in self.service_port_connectors:
00102                 result += '{0}\n'.format(indent_string(str(c)))
00103         if self.startup:
00104             result += 'Startup: {0}\n'.format(self.startup)
00105         if self.shutdown:
00106             result += 'Shutdown: {0}\n'.format(self.shutdown)
00107         if self.activation:
00108             result += 'Activation: {0}\n'.format(self.activation)
00109         if self.deactivation:
00110             result += 'Deactivation: {0}\n'.format(self.deactivation)
00111         if self.resetting:
00112             result += 'Resetting: {0}\n'.format(self.resetting)
00113         if self.initializing:
00114             result += 'Initializing: {0}\n'.format(self.initializing)
00115         if self.finalizing:
00116             result += 'Finalizing: {0}\n'.format(self.finalizing)
00117         if self.comment:
00118             result += 'Comment: {0}\n'.format(self.comment)
00119         if self.version_up_log:
00120             result += 'Version up logs:\n'
00121             for vl in self.version_up_log:
00122                 result += '{0}\n'.format(indent_string(vl))
00123         if self.properties:
00124             result += 'Properties:\n'
00125             for p in self.properties:
00126                 result += '  {0}: {1}\n'.format(p, self.properties[p])
00127         return result[:-1] # Lop off the last new line
00128 
00129     ###########################################################################
00130     # Properties
00131 
00132     @property
00133     def id(self):
00134         '''ID used to distinguish the RT system.
00135 
00136         Typically in the format '[vendor name].[system name].[version]'.
00137 
00138         '''
00139         return self._id
00140 
00141     @id.setter
00142     def id(self, id):
00143         print 'setting id to ', id
00144         validate_attribute(id, 'rts_profile.id',
00145                            expected_type=[str, unicode], required=True)
00146         self._id = id
00147 
00148     @property
00149     def abstract(self):
00150         '''Description of this RT system.
00151 
00152         May be empty.
00153 
00154         '''
00155         return self._abstract
00156 
00157     @abstract.setter
00158     def abstract(self, abstract):
00159         validate_attribute(abstract, 'rts_profile.abstract',
00160                            expected_type=[str, unicode], required=True)
00161         self._abstract = abstract
00162 
00163     @property
00164     def creation_date(self):
00165         '''The date this RT system was first created.
00166 
00167         Usually set automatically by the tool that created the system.
00168 
00169         '''
00170         return self._creation_date
00171 
00172     @creation_date.setter
00173     def creation_date(self, creation_date):
00174         validate_attribute(creation_date, 'rts_profile.creationDate',
00175                            expected_type=[str, unicode], required=True)
00176         self._creation_date = creation_date
00177 
00178     @property
00179     def update_date(self):
00180         '''The date this RT system was most recently updated.
00181 
00182         Usually set automatically by the tool that created the system.
00183 
00184         '''
00185         return self._update_date
00186 
00187     @update_date.setter
00188     def update_date(self, update_date):
00189         validate_attribute(update_date, 'rts_profile.updateDate',
00190                            expected_type=[str, unicode], required=True)
00191         self._update_date = update_date
00192 
00193     @property
00194     def version(self):
00195         '''Version of the RTSProfile specification this is in.'''
00196         return self._version
00197 
00198     @version.setter
00199     def version(self, version):
00200         validate_attribute(version, 'rts_profile.version',
00201                            expected_type=[str, unicode], required=True)
00202         self._version = version
00203 
00204     @property
00205     def components(self):
00206         '''Information about the components that make up the RT system.
00207 
00208         May be an empty list if there are no components. Members are of type
00209         @ref Component.
00210 
00211         '''
00212         return self._components
00213 
00214     @components.setter
00215     def components(self, components):
00216         validate_attribute(components, 'rts_profile.Components',
00217                            expected_type=list, required=True)
00218         self._components = components
00219 
00220     @property
00221     def groups(self):
00222         '''Information about the component groups in the RT system.
00223 
00224         May be an empty list if there are no groups. Members are of type @ref
00225         ComponentGroup.
00226 
00227         '''
00228         return self._groups
00229 
00230     @groups.setter
00231     def groups(self, groups):
00232         validate_attribute(groups, 'rts_profile.Groups',
00233                            expected_type=list, required=True)
00234         self._groups = groups
00235 
00236     @property
00237     def data_port_connectors(self):
00238         '''Connections between data ports in the RT system.
00239 
00240         Members are of type @ref DataPortConnector.
00241 
00242         '''
00243         return self._data_port_connectors
00244 
00245     @data_port_connectors.setter
00246     def data_port_connectors(self, data_port_connectors):
00247         validate_attribute(data_port_connectors,
00248                            'rts_profile.DataPortConnectors',
00249                            expected_type=list, required=True)
00250         self._data_port_connectors = data_port_connectors
00251 
00252     @property
00253     def service_port_connectors(self):
00254         '''Connections between service ports in the RT system.
00255 
00256         Members are of type @ref ServicePortConnector.
00257 
00258         '''
00259         return self._service_port_connectors
00260 
00261     @service_port_connectors.setter
00262     def service_port_connectors(self, service_port_connectors):
00263         validate_attribute(service_port_connectors,
00264                            'rts_profile.ServicePortConnectors',
00265                            expected_type=list, required=True)
00266         self._service_port_connectors = service_port_connectors
00267 
00268     @property
00269     def startup(self):
00270         '''Ordering and conditions for when the RT system is started.'''
00271         return self._startup
00272 
00273     @startup.setter
00274     def startup(self, startup):
00275         validate_attribute(startup, 'rts_profile.StartUp',
00276                            expected_type=StartUp, required=False)
00277         self._startup = startup
00278 
00279     @property
00280     def shutdown(self):
00281         '''Ordering and conditions for when the RT system is shut down.'''
00282         return self._shutdown
00283 
00284     @shutdown.setter
00285     def shutdown(self, shutdown):
00286         validate_attribute(shutdown, 'rts_profile.ShutDown',
00287                            expected_type=ShutDown, required=False)
00288         self._shutdown = shutdown
00289 
00290     @property
00291     def activation(self):
00292         '''Ordering and conditions for when the RT system is activated.'''
00293         return self._activation
00294 
00295     @activation.setter
00296     def activation(self, activation):
00297         validate_attribute(activation, 'rts_profile.Activation',
00298                            expected_type=Activation, required=False)
00299         self._activation = activation
00300 
00301     @property
00302     def deactivation(self):
00303         '''Ordering and conditions for when the RT system is deactivated.'''
00304         return self._deactivation
00305 
00306     @deactivation.setter
00307     def deactivation(self, deactivation):
00308         validate_attribute(deactivation, 'rts_profile.Deactivation',
00309                            expected_type=Deactivation, required=False)
00310         self._deactivation = deactivation
00311 
00312     @property
00313     def resetting(self):
00314         '''Ordering and conditions for when the RT system is reset.'''
00315         return self._resetting
00316 
00317     @resetting.setter
00318     def resetting(self, resetting):
00319         validate_attribute(resetting, 'rts_profile.Resetting',
00320                            expected_type=Resetting, required=False)
00321         self._resetting = resetting
00322 
00323     @property
00324     def initializing(self):
00325         '''Ordering and conditions for when the RT system is initialised.'''
00326         return self._initializing
00327 
00328     @initializing.setter
00329     def initializing(self, initializing):
00330         validate_attribute(initializing, 'rts_profile.Initializing',
00331                            expected_type=Initialize, required=False)
00332         self._initializing = initializing
00333 
00334     @property
00335     def finalizing(self):
00336         '''Ordering and conditions for when the RT system is finalised.'''
00337         return self._finalizing
00338 
00339     @finalizing.setter
00340     def finalizing(self, finalizing):
00341         validate_attribute(finalizing, 'rts_profile.Finalizing',
00342                            expected_type=Finalize, required=False)
00343         self._finalizing = finalizing
00344 
00345     @property
00346     def comment(self):
00347         '''Comment about the system.
00348 
00349         A brief comment about the system. May or may not be displayed in other
00350         tools. May be empty.
00351 
00352         Part of the extended profile.
00353 
00354         '''
00355         return self._comment
00356 
00357     @comment.setter
00358     def comment(self, comment):
00359         validate_attribute(comment, 'rtsprofile.ext.comment',
00360                            expected_type=[str, unicode], required=False)
00361         self._comment = comment
00362 
00363     @property
00364     def version_up_log(self):
00365         '''Log entries for new versions.
00366 
00367         When an update to a system is made, the log entry describing changes is
00368         stored in this value.
00369 
00370         Part of the extended profile.
00371 
00372         '''
00373         return self._version_up_log
00374 
00375     @version_up_log.setter
00376     def version_up_log(self, version_up_log):
00377         validate_attribute(version_up_log, 'rtsprofile.ext.VersionUpLog',
00378                            expected_type=list, required=False)
00379         self._version_up_log = version_up_log
00380 
00381     @property
00382     def properties(self):
00383         '''Miscellaneous properties.
00384 
00385         Stores key/value pair properties.
00386 
00387         Part of the extended profile.
00388 
00389         '''
00390         return self._properties
00391 
00392     @properties.setter
00393     def properties(self, properties):
00394         validate_attribute(properties, 'rtsprofile.ext.Properties',
00395                            expected_type=dict, required=False)
00396         self._properties = properties
00397 
00398     ###########################################################################
00399     # API functions
00400 
00401     def find_comp_by_target(self, target):
00402         '''Finds a component using a TargetComponent or one of its subclasses.
00403 
00404         @param A @ref TargetComponent object or subclass of @ref
00405         TargetComponent.
00406         @return A Component object matching the target.
00407         @raises MissingComponentError
00408 
00409         '''
00410         for comp in self._components:
00411             if comp.id == target.component_id and \
00412                     comp.instance_name == target.instance_name:
00413                 return comp
00414         raise MissingComponentError
00415 
00416     def optional_data_connections(self):
00417         '''Finds all data connections in which one or more components are not
00418         required.
00419 
00420         If all the components involved in a connection are required, that
00421         connection is also required. If one or more are not required, that
00422         connection is optional.
00423 
00424         '''
00425         result = []
00426         for conn in self._data_port_connectors:
00427             source_comp = self.find_comp_by_target(conn.source_data_port)
00428             target_comp = self.find_comp_by_target(conn.target_data_port)
00429             if not source_comp.is_required or not target_comp.is_required:
00430                 result.append(conn)
00431         return result
00432 
00433     def optional_service_connections(self):
00434         '''Finds all service connections in which one or more components are
00435         not required.
00436 
00437         If all the components involved in a connection are required, that
00438         connection is also required. If one or more are not required, that
00439         connection is optional.
00440 
00441         '''
00442         result = []
00443         for conn in self._service_port_connectors:
00444             source_comp = self.find_comp_by_target(conn.source_service_port)
00445             target_comp = self.find_comp_by_target(conn.target_service_port)
00446             if not source_comp.is_required or not target_comp.is_required:
00447                 result.append(conn)
00448         return result
00449 
00450     def required_data_connections(self):
00451         '''Finds all data connections in which all components are required.
00452 
00453         If all the components involved in a connection are required, that
00454         connection is also required. If one or more are not required, that
00455         connection is optional.
00456 
00457         '''
00458         result = []
00459         for conn in self._data_port_connectors:
00460             source_comp = self.find_comp_by_target(conn.source_data_port)
00461             target_comp = self.find_comp_by_target(conn.target_data_port)
00462             if source_comp.is_required and target_comp.is_required:
00463                 result.append(conn)
00464         return result
00465 
00466     def required_service_connections(self):
00467         '''Finds all service connections in which all components are required.
00468 
00469         If all the components involved in a connection are required, that
00470         connection is also required. If one or more are not required, that
00471         connection is optional.
00472 
00473         '''
00474         result = []
00475         for conn in self._service_port_connectors:
00476             source_comp = self.find_comp_by_target(conn.source_service_port)
00477             target_comp = self.find_comp_by_target(conn.target_service_port)
00478             if source_comp.is_required and target_comp.is_required:
00479                 result.append(conn)
00480         return result
00481 
00482     ###########################################################################
00483     # XML
00484 
00485     def parse_from_xml(self, xml_spec):
00486         '''Parse a string or file containing an XML specification.'''
00487         if type(xml_spec) == str or type(xml_spec) == unicode:
00488             dom = xml.dom.minidom.parseString(xml_spec)
00489         else:
00490             dom = xml.dom.minidom.parse(xml_spec)
00491         self._parse_xml(dom)
00492         dom.unlink()
00493 
00494     def save_to_xml(self):
00495         '''Save this RtsProfile into an XML-formatted string.'''
00496         xml_obj = self._to_xml_dom()
00497         return xml_obj.toprettyxml(indent='    ')
00498 
00499     ###########################################################################
00500     # YAML
00501 
00502     def parse_from_yaml(self, yaml_spec):
00503         '''Parse a string or file containing a YAML specification.'''
00504         self._parse_yaml(yaml.safe_load(yaml_spec))
00505 
00506     def save_to_yaml(self):
00507         '''Save this RtsProfile into a YAML-formatted string.'''
00508         return self._to_yaml()
00509 
00510     ###########################################################################
00511     # Internal functions
00512 
00513     def _parse_xml(self, dom):
00514         self._reset()
00515         root = dom.documentElement
00516         # Get the attributes
00517         self.id = root.getAttributeNS(RTS_NS, 'id')
00518         self.abstract = root.getAttributeNS(RTS_NS, 'abstract')
00519         self.creation_date = root.getAttributeNS(RTS_NS, 'creationDate')
00520         self.update_date = root.getAttributeNS(RTS_NS, 'updateDate')
00521         self.version = root.getAttributeNS(RTS_NS, 'version')
00522         self.comment = root.getAttributeNS(RTS_EXT_NS, 'comment')
00523         # Parse the children
00524         for c in root.getElementsByTagNameNS(RTS_NS, 'Components'):
00525             self._components.append(Component().parse_xml_node(c))
00526         for c in root.getElementsByTagNameNS(RTS_NS, 'Groups'):
00527             self._groups.append(ComponentGroup().parse_xml_node(c))
00528         for c in root.getElementsByTagNameNS(RTS_NS, 'DataPortConnectors'):
00529             self._data_port_connectors.append(DataPortConnector().parse_xml_node(c))
00530         for c in root.getElementsByTagNameNS(RTS_NS, 'ServicePortConnectors'):
00531             self._service_port_connectors.append(ServicePortConnector().parse_xml_node(c))
00532         # These children should have zero or one
00533         c = root.getElementsByTagNameNS(RTS_NS, 'StartUp')
00534         if c.length > 0:
00535             if c.length > 1:
00536                 raise InvalidRtsProfileNodeError('StartUp')
00537             self._startup = StartUp().parse_xml_node(c[0])
00538         c = root.getElementsByTagNameNS(RTS_NS, 'ShutDown')
00539         if c.length > 0:
00540             if c.length > 1:
00541                 raise InvalidRtsProfileNodeError('ShutDown')
00542             self._shutdown = ShutDown().parse_xml_node(c[0])
00543         c = root.getElementsByTagNameNS(RTS_NS, 'Activation')
00544         if c.length > 0:
00545             if c.length > 1:
00546                 raise InvalidRtsProfileNodeError('Activation')
00547             self._activation = Activation().parse_xml_node(c[0])
00548         c = root.getElementsByTagNameNS(RTS_NS, 'Deactivation')
00549         if c.length > 0:
00550             if c.length > 1:
00551                 raise InvalidRtsProfileNodeError('Deactivation')
00552             self._deactivation = Deactivation().parse_xml_node(c[0])
00553         c = root.getElementsByTagNameNS(RTS_NS, 'Resetting')
00554         if c.length > 0:
00555             if c.length > 1:
00556                 raise InvalidRtsProfileNodeError('Resetting')
00557             self._resetting = Resetting().parse_xml_node(c[0])
00558         c = root.getElementsByTagNameNS(RTS_NS, 'Initializing')
00559         if c.length > 0:
00560             if c.length > 1:
00561                 raise InvalidRtsProfileNodeError('Initializing')
00562             self._initializing = Initializing().parse_xml_node(c[0])
00563         c = root.getElementsByTagNameNS(RTS_NS, 'Finalizing')
00564         if c.length > 0:
00565             if c.length > 1:
00566                 raise InvalidRtsProfileNodeError('Finalizing')
00567             self._finalizing = Finalizing().parse_xml_node(c[0])
00568         # Extended profile children
00569         for c in root.getElementsByTagNameNS(RTS_EXT_NS, 'VersionUpLog'):
00570             if c.nodeType == c.TEXT_NODE:
00571                 self._version_up_log.append(c.data)
00572             else:
00573                 print >>sys.stderr, 'Warning: bad VersionUpLog node type.'
00574         for c in get_direct_child_elements_xml(root, prefix=RTS_EXT_NS,
00575                                                local_name='Properties'):
00576             name, value = parse_properties_xml(c)
00577             self._properties[name] = value
00578 
00579     def _parse_yaml(self, spec):
00580         self._reset()
00581         if not 'rtsProfile' in spec:
00582             raise RtsProfileError('Missing root node.')
00583         root = spec['rtsProfile']
00584         # Get the attributes
00585         self.id = root['id']
00586         if 'abstract' in root:
00587             self.abstract = root['abstract']
00588         self.creation_date = '{year:04}-{month:02}-{day:02}T{hour:02}:\
00589 {minute:02}:{second:02}'.format(**root['creationDate'])
00590         self.update_date = '{year:04}-{month:02}-{day:02}T{hour:02}:\
00591 {minute:02}:{second:02}'.format(**root['updateDate'])
00592         self.version = root['version']
00593         if RTS_EXT_NS_YAML + 'comment' in root:
00594             self.comment = root[RTS_EXT_NS_YAML + 'comment']
00595         # Parse the children
00596         if 'components' in root:
00597             for c in root['components']:
00598                 self._components.append(Component().parse_yaml(c))
00599         if 'groups' in root:
00600             for c in root['groups']:
00601                 self._groups.append(ComponentGroup().parse_yaml(c))
00602         if 'dataPortConnectors' in root:
00603             for c in root['dataPortConnectors']:
00604                 self._data_port_connectors.append(DataPortConnector().parse_yaml(c))
00605         if 'servicePortConnectors' in root:
00606             for c in root['servicePortConnectors']:
00607                 self._service_port_connectors.append(ServicePortConnector().parse_yaml(c))
00608         # Singular children
00609         if 'startUp' in root:
00610             self._startup = StartUp().parse_yaml(root['startUp'])
00611         if 'shutDown' in root:
00612             self._shutdown = ShutDown().parse_yaml(root['shutDown'])
00613         if 'activation' in root:
00614             self._activation = Activation().parse_yaml(root['activation'])
00615         if 'deactivation' in root:
00616             self._deactivation = Deactivation().parse_yaml(root['deactivation'])
00617         if 'resetting' in root:
00618             self._resetting = Resetting().parse_yaml(root['resetting'])
00619         if 'initializing' in root:
00620             self._initializing = Initializing().parse_yaml(root['initializing'])
00621         if 'finalizing' in root:
00622             self._finalizing = Finalizing().parse_yaml(root['finalizing'])
00623         # Extended profile children
00624         if RTS_EXT_NS_YAML + 'versionUpLogs' in root:
00625             for c in root[RTS_EXT_NS_YAML + 'versionUpLogs']:
00626                 self._version_up_log.append(c)
00627         if RTS_EXT_NS_YAML + 'properties' in root:
00628             for p in root[RTS_EXT_NS_YAML + 'properties']:
00629                 if 'value' in p:
00630                     value = p['value']
00631                 else:
00632                     value = None
00633                 self._properties[p['name']] = value
00634 
00635     def _reset(self):
00636         # Clears all values in the class in preparation for parsing an
00637         # XML file.
00638         # Attributes
00639         self._id = ''
00640         self._abstract = None
00641         self._creation_date = datetime(MINYEAR, 1, 1)
00642         self._update_date = datetime(MINYEAR, 1, 1)
00643         self._version = ''
00644         # Children
00645         self._components = []
00646         self._groups = []
00647         self._data_port_connectors = []
00648         self._service_port_connectors = []
00649         self._startup = None
00650         self._shutdown = None
00651         self._activation = None
00652         self._deactivation = None
00653         self._resetting = None
00654         self._initializing = None
00655         self._finalizing = None
00656         # Extended spec
00657         self._comment = ''
00658         self._version_up_log = ''
00659         self._properties = {}
00660 
00661     def _to_dict(self):
00662         # This converts an RTSProfile object into a dictionary. The typical use
00663         # for this is to then dump that dictionary as YAML.
00664         # We need to do this because the RtsProfile object hierarchy does not
00665         # correspond directly to the YAML object hierarchy.
00666         prof = {'id': self.id,
00667                 'abstract': self.abstract,
00668                 'creationDate': date_to_dict(self.creation_date),
00669                 'updateDate': date_to_dict(self.update_date),
00670                 'version': self.version}
00671         if self.comment:
00672             prof[RTS_EXT_NS_YAML + 'comment'] = self.comment
00673 
00674         components = []
00675         for c in self.components:
00676             components.append(c.to_dict())
00677         if components:
00678             prof['components'] = components
00679         groups = []
00680         for g in self.groups:
00681             groups.append(g.to_dict())
00682         if groups:
00683             prof['groups'] = groups
00684         d_connectors = []
00685         for c in self.data_port_connectors:
00686             d_connectors.append(c.to_dict())
00687         if d_connectors:
00688             prof['dataPortConnectors'] = d_connectors
00689         s_connectors = []
00690         for c in self.service_port_connectors:
00691             s_connectors.append(c.to_dict())
00692         if s_connectors:
00693             prof['servicePortConnectors'] = s_connectors
00694 
00695         if self.startup:
00696             prof['startUp'] = self.startup.to_dict()
00697         if self.shutdown:
00698             prof['shutDown'] = self.shutdown.to_dict()
00699         if self.activation:
00700             prof['activation'] = self.activation.to_dict()
00701         if self.deactivation:
00702             prof['deactivation'] = self.deactivation.to_dict()
00703         if self.resetting:
00704             prof['resetting'] = self.resetting.to_dict()
00705         if self.initializing:
00706             prof['initializing'] = self.initializing.to_dict()
00707         if self.finalizing:
00708             prof['finalizing'] = self.finalizing.to_dict()
00709 
00710         log = []
00711         for l in self.version_up_log:
00712             log.append(l)
00713         if log:
00714             prof[RTS_EXT_NS_YAML + 'versionUpLogs'] = log
00715         props = []
00716         for name in self.properties:
00717             p = {'name': name}
00718             if self.properties[name]:
00719                 p['value'] = str(self.properties[name])
00720             props.append(p)
00721         if props:
00722             prof[RTS_EXT_NS_YAML + 'properties'] = props
00723 
00724         return {'rtsProfile': prof}
00725 
00726     def _to_xml_dom(self):
00727         impl = xml.dom.minidom.getDOMImplementation()
00728         doc = impl.createDocument(RTS_NS, RTS_NS_S + 'RtsProfile', None)
00729         doc.documentElement.setAttribute('xmlns:rts', RTS_NS)
00730         doc.documentElement.setAttribute('xmlns:rtsExt', RTS_EXT_NS)
00731         doc.documentElement.setAttribute('xmlns:xsi',
00732                 'http://www.w3.org/2001/XMLSchema-instance')
00733 
00734         doc.documentElement.setAttributeNS(RTS_NS, RTS_NS_S + 'id', self.id)
00735         doc.documentElement.setAttributeNS(RTS_NS, RTS_NS_S + 'abstract',
00736                                            self.abstract)
00737         doc.documentElement.setAttributeNS(RTS_NS, RTS_NS_S + 'creationDate',
00738                                            self.creation_date)
00739         doc.documentElement.setAttributeNS(RTS_NS, RTS_NS_S + 'updateDate',
00740                                            self.update_date)
00741         doc.documentElement.setAttributeNS(RTS_NS, RTS_NS_S + 'version',
00742                                            str(self.version))
00743         if self.comment:
00744             doc.documentElement.setAttributeNS(RTS_EXT_NS,
00745                                                RTS_EXT_NS_S + 'comment',
00746                                                self.comment)
00747         for c in self.components:
00748             new_comp_element = doc.createElementNS(RTS_NS,
00749                                                    RTS_NS_S + 'Components')
00750             c.save_xml(doc, new_comp_element)
00751             doc.documentElement.appendChild(new_comp_element)
00752         for g in self.groups:
00753             new_group_element = doc.createElementNS(RTS_NS,
00754                                                     RTS_NS_S + 'Groups')
00755             g.save_xml(doc, new_group_element)
00756             doc.documentElement.appendChild(new_group_element)
00757         for dc in self.data_port_connectors:
00758             new_conn_element = doc.createElementNS(RTS_NS,
00759                     RTS_NS_S + 'DataPortConnectors')
00760             dc.save_xml(doc, new_conn_element)
00761             doc.documentElement.appendChild(new_conn_element)
00762         for sc in self.service_port_connectors:
00763             new_conn_element = doc.createElementNS(RTS_NS,
00764                     RTS_NS_S + 'ServicePortConnectors')
00765             sc.save_xml(doc, new_conn_element)
00766             doc.documentElement.appendChild(new_conn_element)
00767         if self.startup:
00768             new_cond = doc.createElementNS(RTS_NS, RTS_NS_S + 'StartUp')
00769             self.startup.save_xml(doc, new_cond)
00770             doc.documentElement.appendChild(new_cond)
00771         if self.shutdown:
00772             new_cond = doc.createElementNS(RTS_NS, RTS_NS_S + 'ShutDown')
00773             self.shutdown.save_xml(doc, new_cond)
00774             doc.documentElement.appendChild(new_cond)
00775         if self.activation:
00776             new_cond = doc.createElementNS(RTS_NS, RTS_NS_S + 'Activation')
00777             self.activation.save_xml(doc, new_cond)
00778             doc.documentElement.appendChild(new_cond)
00779         if self.deactivation:
00780             new_cond = doc.createElementNS(RTS_NS, RTS_NS_S + 'Deactivation')
00781             self.deactivation.save_xml(doc, new_cond)
00782             doc.documentElement.appendChild(new_cond)
00783         if self.resetting:
00784             new_cond = doc.createElementNS(RTS_NS, RTS_NS_S + 'Resetting')
00785             self.resetting.save_xml(doc, new_cond)
00786             doc.documentElement.appendChild(new_cond)
00787         if self.initializing:
00788             new_cond = doc.createElementNS(RTS_NS, RTS_NS_S + 'Initializing')
00789             self.initializing.save_xml(doc, new_cond)
00790             doc.documentElement.appendChild(new_cond)
00791         if self.finalizing:
00792             new_cond = doc.createElementNS(RTS_NS, RTS_NS_S + 'Finalizing')
00793             self.finalizing.save_xml(doc, new_cond)
00794             doc.documentElement.appendChild(new_cond)
00795         for vl in self.version_up_log:
00796             new_vl_element = doc.createElementNS(RTS_EXT_NS,
00797                                                  RTS_EXT_NS_S + 'VersionUpLog')
00798             new_text_node = doc.createTextNode(vl)
00799             new_vl_element.appendChild(new_text_node)
00800             doc.documentElement.appendChild(new_vl_element)
00801         for p in self.properties:
00802             new_prop_element = doc.createElementNS(RTS_EXT_NS,
00803                                                    RTS_EXT_NS_S + 'Properties')
00804             properties_to_xml(new_prop_element, p, self.properties[p])
00805             doc.documentElement.appendChild(new_prop_element)
00806         return doc
00807 
00808     def _to_yaml(self):
00809         return yaml.safe_dump(self._to_dict())
00810 
00811 
00812 # vim: tw=79
00813 


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