component_group.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: component_group.py
17 
18 Object representing a component group.
19 
20 '''
21 
22 __version__ = '$Revision: $'
23 # $Source$
24 
25 
26 from rtsprofile import RTS_NS, RTS_NS_S
27 from rtsprofile.utils import validate_attribute
28 
29 
30 ##############################################################################
31 ## ComponentGroup object
32 
33 class ComponentGroup(object):
34  '''A group of components in the RT system.'''
35 
36  def __init__(self, group_id='', members=[]):
37  '''Constructor.
38 
39  @param group_id ID of the group.
40  @type group_id str
41  @param members Members of the group. At least one must be present.
42  @type members list
43 
44  '''
45  validate_attribute(group_id, 'component_group.groupID',
46  expected_type=[str, unicode], required=False)
47  self._group_id = group_id
48  validate_attribute(members, 'component_group.Members',
49  expected_type=list, required=False)
50  self._members = members
51 
52  def __str__(self):
53  result = 'Group ID: {0}\n'.format(self.group_id)
54  if self.members:
55  result += 'Members:\n'
56  for m in self.members:
57  result += ' {0}\n'.format(m)
58  return result[:-1] # Lop off the last new line
59 
60  @property
61  def group_id(self):
62  '''The ID used to distinguish this group in the RT system.'''
63  return self._group_id
64 
65  @group_id.setter
66  def group_id(self, group_id):
67  validate_attribute(group_id, 'component_group.groupID',
68  expected_type=[str, unicode], required=True)
69  self._group_id = group_id
70 
71  @property
72  def members(self):
73  '''A list of the components in the group.
74 
75  At least one must be present.
76 
77  '''
78  return self._members
79 
80  @members.setter
81  def members(self, members):
82  validate_attribute(members, 'component_group.Members',
83  expected_type=list, required=True)
84  self._members = members
85 
86  def parse_xml_node(self, node):
87  '''Parse an xml.dom Node object representing a component group into
88  this object.
89 
90  '''
91  self.group_id = node.getAttributeNS(RTS_NS, 'groupId')
92  self._members = []
93  for c in node.getElementsByTagNameNS(RTS_NS, 'Members'):
94  self._members.append(TargetComponent().parse_xml_node(c))
95  return self
96 
97  def parse_yaml(self, node):
98  '''Parse a YAML specification of a component group into this
99  object.
100 
101  '''
102  self.group_id = y['groupId']
103  self._members = []
104  if 'members' in y:
105  for m in y.get('members'):
106  self._members.append(TargetComponent().parse_yaml(m))
107  return self
108 
109  def save_xml(self, doc, element):
110  '''Save this component group into an xml.dom.Element object.'''
111  element.setAttributeNS(RTS_NS, RTS_NS_S + 'groupID', self.group_id)
112  for m in self.members:
113  new_element = doc.createElementNS(RTS_NS, RTS_NS_S + 'Members')
114  m.save_xml(doc, new_element)
115  element.appendChild(new_element)
116 
117  def to_dict(self):
118  '''Save this component group to a dictionary.'''
119  d = {'groupId': self.group_id}
120  members = []
121  for m in self.members:
122  members.append(m.to_dict())
123  if members:
124  d['members'] = members
125  return d
126 
127 
128 
129 # vim: tw=79
130 
def validate_attribute(attr, name, expected_type=None, required=False)
Definition: utils.py:92
def __init__(self, group_id='', members=[])


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