예제 #1
0
 def parse_yaml(self, y):
     '''Parse a YAML specification of a condition into this object.'''
     self.sequence = int(y['sequence'])
     self.target_component = \
             TargetExecutionContext().parse_yaml(y['targetComponent'])
     if RTS_EXT_NS_YAML + 'properties' in y:
         for p in y.get(RTS_EXT_NS_YAML + 'properties'):
             if 'value' in p:
                 value = p['value']
             else:
                 value = None
             self._properties[p['name']] = value
     return self
예제 #2
0
    def parse_xml_node(self, node):
        '''Parse an xml.dom Node object representing a condition into this
        object.

        '''
        self.sequence = int(node.getAttributeNS(RTS_NS, 'sequence'))
        c = node.getElementsByTagNameNS(RTS_NS, 'TargetComponent')
        if c.length != 1:
            raise InvalidParticipantNodeError
        self.target_component = TargetExecutionContext().parse_xml_node(c[0])
        for c in get_direct_child_elements_xml(node, prefix=RTS_EXT_NS,
                                               local_name='Properties'):
            name, value = parse_properties_xml(c)
            self._properties[name] = value
        return self
예제 #3
0
class Condition(object):
    '''Specifies execution orderings and conditions for RT components in the RT
    system.

    Execution conditions can include the time to wait before executing and
    order of precedence for components. The involved RT component is specified
    using @ref TargetExecutionContext.

    '''

    def __init__(self, sequence=0, target_component=TargetExecutionContext()):
        '''Constructor.

        @param sequence Execution order of the target component.
        @type sequence int
        @param target_component The target of the condition.
        @type target_component TargetComponent
        '''
        validate_attribute(sequence, 'conditions.sequence',
                           expected_type=int, required=False)
        self._sequence = sequence
        validate_attribute(target_component, 'conditions.TargetComponent',
                           expected_type=TargetExecutionContext,
                           required=True)
        self._target_component = target_component
        self._properties = {}

    def __str__(self):
        result = 'Sequence: {0}\nTargetEC:\n{1}\n'.format(self.sequence,
                indent_string(str(self.target_component)))
        if self.properties:
            result += 'Properties:\n'
            for p in self.properties:
                result += '  {0}: {1}\n'.format(p, self.properties[p])
        return result[:-1] # Lop off the last new line

    @property
    def sequence(self):
        '''The execution order of the target components for the various
        actions.

        '''
        return self._sequence

    @sequence.setter
    def sequence(self, sequence):
        validate_attribute(sequence, 'conditions.sequence',
                           expected_type=int, required=False)
        self._sequence = sequence

    @property
    def target_component(self):
        '''Target component of the condition.'''
        return self._target_component

    @target_component.setter
    def target_component(self, target_component):
        validate_attribute(target_component, 'conditions.TargetComponent',
                           expected_type=TargetExecutionContext,
                           required=True)
        self._target_component = target_component

    @property
    def properties(self):
        '''Miscellaneous properties.

        Stores key/value pair properties.

        Part of the extended profile.

        '''
        return self._properties

    @properties.setter
    def properties(self, properties):
        validate_attribute(properties, 'conditions.ext.Properties',
                           expected_type=dict, required=False)
        self._properties = properties

    def parse_xml_node(self, node):
        '''Parse an xml.dom Node object representing a condition into this
        object.

        '''
        self.sequence = int(node.getAttributeNS(RTS_NS, 'sequence'))
        c = node.getElementsByTagNameNS(RTS_NS, 'TargetComponent')
        if c.length != 1:
            raise InvalidParticipantNodeError
        self.target_component = TargetExecutionContext().parse_xml_node(c[0])
        for c in get_direct_child_elements_xml(node, prefix=RTS_EXT_NS,
                                               local_name='Properties'):
            name, value = parse_properties_xml(c)
            self._properties[name] = value
        return self

    def parse_yaml(self, y):
        '''Parse a YAML specification of a condition into this object.'''
        self.sequence = int(y['sequence'])
        self.target_component = \
                TargetExecutionContext().parse_yaml(y['targetComponent'])
        if RTS_EXT_NS_YAML + 'properties' in y:
            for p in y.get(RTS_EXT_NS_YAML + 'properties'):
                if 'value' in p:
                    value = p['value']
                else:
                    value = None
                self._properties[p['name']] = value
        return self

    def save_xml(self, doc, element):
        '''Save this condition into an xml.dom.Element object.'''
        element.setAttributeNS(RTS_NS, RTS_NS_S + 'sequence',
                               str(self.sequence))
        new_element = doc.createElementNS(RTS_NS, RTS_NS_S + 'TargetComponent')
        self.target_component.save_xml(doc, new_element)
        element.appendChild(new_element)
        for p in self.properties:
            new_prop_element = doc.createElementNS(RTS_EXT_NS,
                                                   RTS_EXT_NS_S + 'Properties')
            properties_to_xml(new_prop_element, p, self.properties[p])
            element.appendChild(new_prop_element)

    def to_dict(self):
        '''Save this condition into a dictionary.'''
        d = {'sequence': self.sequence,
                'targetComponent': self.target_component.to_dict()}
        props = []
        for name in self.properties:
            p = {'name': name}
            if self.properties[name]:
                p['value'] = str(self.properties[name])
            props.append(p)
        if props:
            d[RTS_EXT_NS_YAML + 'properties'] = props
        return d