00001
00002
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: config_set.py
00017
00018 Objects representing a component's configuration set.
00019
00020 '''
00021
00022 __version__ = '$Revision: $'
00023
00024
00025
00026 from rtsprofile import RTS_NS, RTS_NS_S
00027 from rtsprofile.exec_context import ExecutionContext
00028 from rtsprofile.utils import indent_string, validate_attribute
00029
00030
00031
00032
00033
00034 class ConfigurationSet(object):
00035 '''Represents a configuration set.
00036
00037 A configuration set is a collection of configuration parameters. An RT
00038 Component can have multiple configuration sets.
00039
00040 '''
00041
00042 def __init__(self, id=''):
00043 '''Constructor.
00044
00045 @param id The configuration set ID.
00046 @type id str
00047
00048 '''
00049 validate_attribute(id, 'configuration_set.id',
00050 expected_type=[str, unicode], required=False)
00051 self._id = id
00052 self._config_data = []
00053
00054 def __str__(self):
00055 result = 'ID: {0}\n'.format(self.id)
00056 if self.configuration_data:
00057 result += 'Configuration data:\n'
00058 for d in self.configuration_data:
00059 result += '{0}\n'.format(indent_string(str(d)))
00060 return result[:-1]
00061
00062 @property
00063 def configuration_data(self):
00064 '''The configuration parameters contained in this set.
00065
00066 May be an empty list if this set has no parameters.
00067
00068 '''
00069 return self._config_data
00070
00071 @configuration_data.setter
00072 def configuration_data(self, configuration_data):
00073 validate_attribute(configuration_data,
00074 'configuration_set.ConfigurationData',
00075 expected_type=list)
00076 self._config_data = configuration_data
00077
00078 @property
00079 def id(self):
00080 '''The configuration set ID.
00081
00082 Used to distinguish this configuration set from others in the RT
00083 Component.
00084
00085 '''
00086 return self._id
00087
00088 @id.setter
00089 def id(self, id):
00090 validate_attribute(id, 'configuration_set.id',
00091 expected_type=[str, unicode], required=True)
00092 self._id = id
00093
00094 def parse_xml_node(self, node):
00095 '''Parse an xml.dom Node object representing a configuration set into
00096 this object.
00097
00098 '''
00099 self.id = node.getAttributeNS(RTS_NS, 'id')
00100 self._config_data = []
00101 for d in node.getElementsByTagNameNS(RTS_NS, 'ConfigurationData'):
00102 self._config_data.append(ConfigurationData().parse_xml_node(d))
00103 return self
00104
00105 def parse_yaml(self, y):
00106 '''Parse a YAML specification of a configuration set into this
00107 object.
00108
00109 '''
00110 self.id = y['id']
00111 self._config_data = []
00112 if 'configurationData' in y:
00113 for d in y.get('configurationData'):
00114 self._config_data.append(ConfigurationData().parse_yaml(d))
00115 return self
00116
00117 def save_xml(self, doc, element):
00118 '''Save this configuration set into an xml.dom.Element object.'''
00119 element.setAttributeNS(RTS_NS, RTS_NS_S + 'id', self.id)
00120 for c in self._config_data:
00121 new_element = doc.createElementNS(RTS_NS,
00122 RTS_NS_S + 'ConfigurationData')
00123 c.save_xml(doc, new_element)
00124 element.appendChild(new_element)
00125
00126 def to_dict(self):
00127 '''Save this configuration set into a dictionary.'''
00128 d = {'id': self.id}
00129 data = []
00130 for c in self._config_data:
00131 data.append(c.to_dict())
00132 if data:
00133 d['configurationData'] = data
00134 return d
00135
00136
00137
00138
00139
00140 class ConfigurationData(object):
00141 '''Represents an individual configuration parameter and its value.'''
00142
00143 def __init__(self, name='', data=''):
00144 '''Constructor.
00145
00146 @param name The name of the parameter.
00147 @type name str
00148 @param data The parameter's value, if any.
00149 @type data str
00150
00151 '''
00152 validate_attribute(name, 'configuration_set.name',
00153 expected_type=[str, unicode], required=False)
00154 self._name = name
00155 validate_attribute(data, 'configuration_set.data',
00156 expected_type=[str, unicode], required=False)
00157 self._data = data
00158
00159 def __str__(self):
00160 return '{0}: {1}'.format(self.name, self.data)
00161
00162 @property
00163 def data(self):
00164 '''The value of this configuration parameter.
00165
00166 May be an empty string if the parameter has no value.
00167
00168 '''
00169 return self._data
00170
00171 @data.setter
00172 def data(self, data):
00173 validate_attribute(data, 'configuration_set.data',
00174 expected_type=[str, unicode], required=False)
00175 self._data = data
00176
00177 @property
00178 def name(self):
00179 '''The name of this configuration parameter.
00180
00181 Used as the parameter's key in the configuration set object.
00182
00183 '''
00184 return self._name
00185
00186 @name.setter
00187 def name(self, name):
00188 validate_attribute(name, 'configuration_set.name',
00189 expected_type=[str, unicode], required=True)
00190 self._name = name
00191
00192 def parse_xml_node(self, node):
00193 '''Parse an xml.dom Node object representing a configuration data into
00194 this object.
00195
00196 '''
00197 self.name = node.getAttributeNS(RTS_NS, 'name')
00198 if node.hasAttributeNS(RTS_NS, 'data'):
00199 self.data = node.getAttributeNS(RTS_NS, 'data')
00200 else:
00201 self.data = ''
00202 return self
00203
00204 def parse_yaml(self, y):
00205 '''Parse a YAML specification of a configuration data into this
00206 object.
00207
00208 '''
00209 self.name = y['name']
00210 if 'data' in y:
00211 self.data = y['data']
00212 else:
00213 self.data = ''
00214 return self
00215
00216 def save_xml(self, doc, element):
00217 '''Save this configuration data into an xml.dom.Element object.'''
00218 element.setAttributeNS(RTS_NS, RTS_NS_S + 'name', self.name)
00219 if self.data:
00220 element.setAttributeNS(RTS_NS, RTS_NS_S + 'data', self.data)
00221
00222 def to_dict(self):
00223 '''Save this configuration data into a dictionary.'''
00224 d = {'name': self.name}
00225 if self.data:
00226 d['data'] = self.data
00227 return d
00228
00229
00230
00231