aboutsummaryrefslogtreecommitdiffstats
path: root/tools/xmlGenerator/PfwBaseTranslator.py
blob: 0d08565ef25aa742609a7a6129987b727c0f307b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# Copyright (c) 2015, Intel Corporation
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation and/or
# other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its contributors
# may be used to endorse or promote products derived from this software without
# specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

class PfwException(Exception):
    pass

class PfwBaseTranslator(object):
    """Abstract Pfw Translator class

    The protocol presented by this class allows the creation of
    parameter-framework settings: domains, configurations rules, etc.

    Some methods must be called within a context; e.g. when 'addElement' is
    called, the element is added to the domain that was last created through
    'createDomain'

    Derived classed should only implemented the backend methods, prefixed by an
    underscore."""

    def __init__(self):
        self._ctx_domain = ''
        self._ctx_configuration = ''
        self._ctx_sequence_aware = False
        self._ctx_command = ''

        self._domain_valid = False
        self._configuration_valid = False

    def _getContext(self):
        return {
                'domain': self._ctx_domain,
                'configuration': self._ctx_configuration,
                'sequence_aware': self._ctx_sequence_aware,
                'command': self._ctx_command}

    def _check(self, func):
        """Check and handles exceptions

        Returns False if an exception occured and was properly caught,
        True otherwise"""
        def wrapped(*args, **kwargs):
            try:
                func(*args, **kwargs)
            except PfwException as ex:
                self._handleException(ex)
                return False

            return True

        return wrapped

    def createDomain(self, name, sequence_aware=False):
        """Create a domain with a given name and optionally set its sequence
        awareness"""

        self._ctx_command = "createDomain"
        self._ctx_domain = name
        self._ctx_configuration = ''
        self._ctx_sequence_aware = sequence_aware
        self._domain_valid = True

        if not self._check(self._doCreateDomain)(name):
            self._domain_valid = False
        elif sequence_aware:
            self._check(self._doSetSequenceAware)()

    def addElement(self, path):
        """Add a configurable element to the current domain"""

        self._ctx_command = "addElement"

        if not self._domain_valid:
            return

        self._check(self._doAddElement)(path)

    def createConfiguration(self, name):
        """Create a configuration for the current domain"""

        self._ctx_command = "createConfiguration"
        self._ctx_configuration = name
        self._configuration_valid = True

        if not self._domain_valid:
            self._configuration_valid = False
            return

        if not self._check(self._doCreateConfiguration)(name):
            self._configuration_valid = False

    def setElementSequence(self, paths):
        """Set the element sequence (if applicable, e.g. if the domain is
        sequence-aware) of the current configuration"""

        self._ctx_command = "setElementSequence"

        if not self._configuration_valid:
            return

        if not self._ctx_sequence_aware:
            return

        self._check(self._doSetElementSequence)(paths)

    def setRule(self, rule):
        """Set the current configuration's applicability rule"""

        self._ctx_command = "setRule"

        if not self._configuration_valid:
            return

        self._doSetRule(rule)

    def setParameter(self, path, value):
        """Set a parameter value for the current configuration"""

        self._ctx_command = "setParameter"

        if not self._configuration_valid:
            return

        self._check(self._doSetParameter)(path, value)

    def _handleException(self, exception):
        raise exception

    def _notImplemented(self):
        raise NotImplementedError(
            "{} is an abstract class".format(self.__class__))

    # Implementation methods
    def _doCreateDomain(self, name):
        self._notImplemented()

    def _doSetSequenceAware(self):
        self._notImplemented()

    def _doAddElement(self, path):
        self._notImplemented()

    def _doCreateConfiguration(self, name):
        self._notImplemented()

    def _doSetElementSequence(self, paths):
        self._notImplemented()

    def _doSetRule(self, rule):
        self._notImplemented()

    def _doSetParameter(self, path, value):
        self._notImplemented()