component_group.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: component_group.py
00017 
00018 Object representing a component group.
00019 
00020 '''
00021 
00022 __version__ = '$Revision: $'
00023 # $Source$
00024 
00025 
00026 from rtsprofile import RTS_NS, RTS_NS_S
00027 from rtsprofile.utils import validate_attribute
00028 
00029 
00030 ##############################################################################
00031 ## ComponentGroup object
00032 
00033 class ComponentGroup(object):
00034     '''A group of components in the RT system.'''
00035 
00036     def __init__(self, group_id='', members=[]):
00037         '''Constructor.
00038 
00039         @param group_id ID of the group.
00040         @type group_id str
00041         @param members Members of the group. At least one must be present.
00042         @type members list
00043 
00044         '''
00045         validate_attribute(group_id, 'component_group.groupID',
00046                            expected_type=[str, unicode], required=False)
00047         self._group_id = group_id
00048         validate_attribute(members, 'component_group.Members',
00049                            expected_type=list, required=False)
00050         self._members = members
00051 
00052     def __str__(self):
00053         result = 'Group ID: {0}\n'.format(self.group_id)
00054         if self.members:
00055             result += 'Members:\n'
00056             for m in self.members:
00057                 result += '  {0}\n'.format(m)
00058         return result[:-1] # Lop off the last new line
00059 
00060     @property
00061     def group_id(self):
00062         '''The ID used to distinguish this group in the RT system.'''
00063         return self._group_id
00064 
00065     @group_id.setter
00066     def group_id(self, group_id):
00067         validate_attribute(group_id, 'component_group.groupID',
00068                            expected_type=[str, unicode], required=True)
00069         self._group_id = group_id
00070 
00071     @property
00072     def members(self):
00073         '''A list of the components in the group.
00074 
00075         At least one must be present.
00076 
00077         '''
00078         return self._members
00079 
00080     @members.setter
00081     def members(self, members):
00082         validate_attribute(members, 'component_group.Members',
00083                            expected_type=list, required=True)
00084         self._members = members
00085 
00086     def parse_xml_node(self, node):
00087         '''Parse an xml.dom Node object representing a component group into
00088         this object.
00089 
00090         '''
00091         self.group_id = node.getAttributeNS(RTS_NS, 'groupId')
00092         self._members = []
00093         for c in node.getElementsByTagNameNS(RTS_NS, 'Members'):
00094             self._members.append(TargetComponent().parse_xml_node(c))
00095         return self
00096 
00097     def parse_yaml(self, node):
00098         '''Parse a YAML specification of a component group into this
00099         object.
00100 
00101         '''
00102         self.group_id = y['groupId']
00103         self._members = []
00104         if 'members' in y:
00105             for m in y.get('members'):
00106                 self._members.append(TargetComponent().parse_yaml(m))
00107         return self
00108 
00109     def save_xml(self, doc, element):
00110         '''Save this component group into an xml.dom.Element object.'''
00111         element.setAttributeNS(RTS_NS, RTS_NS_S + 'groupID', self.group_id)
00112         for m in self.members:
00113             new_element = doc.createElementNS(RTS_NS, RTS_NS_S + 'Members')
00114             m.save_xml(doc, new_element)
00115             element.appendChild(new_element)
00116 
00117     def to_dict(self):
00118         '''Save this component group to a dictionary.'''
00119         d = {'groupId': self.group_id}
00120         members = []
00121         for m in self.members:
00122             members.append(m.to_dict())
00123         if members:
00124             d['members'] = members
00125         return d
00126 
00127 
00128 
00129 # vim: tw=79
00130 


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