config_set.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: config_set.py
17 
18 Objects representing a component's configuration set.
19 
20 '''
21 
22 __version__ = '$Revision: $'
23 # $Source$
24 
25 
26 from rtsprofile import RTS_NS, RTS_NS_S
27 from rtsprofile.exec_context import ExecutionContext
28 from rtsprofile.utils import indent_string, validate_attribute
29 
30 
31 ##############################################################################
32 ## ConfigurationSet object
33 
34 class ConfigurationSet(object):
35  '''Represents a configuration set.
36 
37  A configuration set is a collection of configuration parameters. An RT
38  Component can have multiple configuration sets.
39 
40  '''
41 
42  def __init__(self, id=''):
43  '''Constructor.
44 
45  @param id The configuration set ID.
46  @type id str
47 
48  '''
49  validate_attribute(id, 'configuration_set.id',
50  expected_type=[str, unicode], required=False)
51  self._id = id
52  self._config_data = []
53 
54  def __str__(self):
55  result = 'ID: {0}\n'.format(self.id)
56  if self.configuration_data:
57  result += 'Configuration data:\n'
58  for d in self.configuration_data:
59  result += '{0}\n'.format(indent_string(str(d)))
60  return result[:-1] # Lop off the last new line
61 
62  @property
63  def configuration_data(self):
64  '''The configuration parameters contained in this set.
65 
66  May be an empty list if this set has no parameters.
67 
68  '''
69  return self._config_data
70 
71  @configuration_data.setter
72  def configuration_data(self, configuration_data):
73  validate_attribute(configuration_data,
74  'configuration_set.ConfigurationData',
75  expected_type=list)
76  self._config_data = configuration_data
77 
78  @property
79  def id(self):
80  '''The configuration set ID.
81 
82  Used to distinguish this configuration set from others in the RT
83  Component.
84 
85  '''
86  return self._id
87 
88  @id.setter
89  def id(self, id):
90  validate_attribute(id, 'configuration_set.id',
91  expected_type=[str, unicode], required=True)
92  self._id = id
93 
94  def parse_xml_node(self, node):
95  '''Parse an xml.dom Node object representing a configuration set into
96  this object.
97 
98  '''
99  self.id = node.getAttributeNS(RTS_NS, 'id')
100  self._config_data = []
101  for d in node.getElementsByTagNameNS(RTS_NS, 'ConfigurationData'):
102  self._config_data.append(ConfigurationData().parse_xml_node(d))
103  return self
104 
105  def parse_yaml(self, y):
106  '''Parse a YAML specification of a configuration set into this
107  object.
108 
109  '''
110  self.id = y['id']
111  self._config_data = []
112  if 'configurationData' in y:
113  for d in y.get('configurationData'):
114  self._config_data.append(ConfigurationData().parse_yaml(d))
115  return self
116 
117  def save_xml(self, doc, element):
118  '''Save this configuration set into an xml.dom.Element object.'''
119  element.setAttributeNS(RTS_NS, RTS_NS_S + 'id', self.id)
120  for c in self._config_data:
121  new_element = doc.createElementNS(RTS_NS,
122  RTS_NS_S + 'ConfigurationData')
123  c.save_xml(doc, new_element)
124  element.appendChild(new_element)
125 
126  def to_dict(self):
127  '''Save this configuration set into a dictionary.'''
128  d = {'id': self.id}
129  data = []
130  for c in self._config_data:
131  data.append(c.to_dict())
132  if data:
133  d['configurationData'] = data
134  return d
135 
136 
137 ##############################################################################
138 ## ConfigurationData object
139 
140 class ConfigurationData(object):
141  '''Represents an individual configuration parameter and its value.'''
142 
143  def __init__(self, name='', data=''):
144  '''Constructor.
145 
146  @param name The name of the parameter.
147  @type name str
148  @param data The parameter's value, if any.
149  @type data str
150 
151  '''
152  validate_attribute(name, 'configuration_set.name',
153  expected_type=[str, unicode], required=False)
154  self._name = name
155  validate_attribute(data, 'configuration_set.data',
156  expected_type=[str, unicode], required=False)
157  self._data = data
158 
159  def __str__(self):
160  return '{0}: {1}'.format(self.name, self.data)
161 
162  @property
163  def data(self):
164  '''The value of this configuration parameter.
165 
166  May be an empty string if the parameter has no value.
167 
168  '''
169  return self._data
170 
171  @data.setter
172  def data(self, data):
173  validate_attribute(data, 'configuration_set.data',
174  expected_type=[str, unicode], required=False)
175  self._data = data
176 
177  @property
178  def name(self):
179  '''The name of this configuration parameter.
180 
181  Used as the parameter's key in the configuration set object.
182 
183  '''
184  return self._name
185 
186  @name.setter
187  def name(self, name):
188  validate_attribute(name, 'configuration_set.name',
189  expected_type=[str, unicode], required=True)
190  self._name = name
191 
192  def parse_xml_node(self, node):
193  '''Parse an xml.dom Node object representing a configuration data into
194  this object.
195 
196  '''
197  self.name = node.getAttributeNS(RTS_NS, 'name')
198  if node.hasAttributeNS(RTS_NS, 'data'):
199  self.data = node.getAttributeNS(RTS_NS, 'data')
200  else:
201  self.data = ''
202  return self
203 
204  def parse_yaml(self, y):
205  '''Parse a YAML specification of a configuration data into this
206  object.
207 
208  '''
209  self.name = y['name']
210  if 'data' in y:
211  self.data = y['data']
212  else:
213  self.data = ''
214  return self
215 
216  def save_xml(self, doc, element):
217  '''Save this configuration data into an xml.dom.Element object.'''
218  element.setAttributeNS(RTS_NS, RTS_NS_S + 'name', self.name)
219  if self.data:
220  element.setAttributeNS(RTS_NS, RTS_NS_S + 'data', self.data)
221 
222  def to_dict(self):
223  '''Save this configuration data into a dictionary.'''
224  d = {'name': self.name}
225  if self.data:
226  d['data'] = self.data
227  return d
228 
229 
230 # vim: tw=79
231 
ConfigurationSet object.
Definition: config_set.py:34
def validate_attribute(attr, name, expected_type=None, required=False)
Definition: utils.py:92
def save_xml(self, doc, element)
Definition: config_set.py:117
def indent_string(string, num_spaces=2)
Definition: utils.py:65
def __init__(self, name='', data='')
Definition: config_set.py:143
def save_xml(self, doc, element)
Definition: config_set.py:216
ConfigurationData object.
Definition: config_set.py:140


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