• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright (c) 2015, Intel Corporation
2# All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without modification,
5# are permitted provided that the following conditions are met:
6#
7# 1. Redistributions of source code must retain the above copyright notice, this
8# list of conditions and the following disclaimer.
9#
10# 2. Redistributions in binary form must reproduce the above copyright notice,
11# this list of conditions and the following disclaimer in the documentation and/or
12# other materials provided with the distribution.
13#
14# 3. Neither the name of the copyright holder nor the names of its contributors
15# may be used to endorse or promote products derived from this software without
16# specific prior written permission.
17#
18# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
22# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29class PfwException(Exception):
30    pass
31
32class PfwBaseTranslator(object):
33    """Abstract Pfw Translator class
34
35    The protocol presented by this class allows the creation of
36    parameter-framework settings: domains, configurations rules, etc.
37
38    Some methods must be called within a context; e.g. when 'addElement' is
39    called, the element is added to the domain that was last created through
40    'createDomain'
41
42    Derived classed should only implemented the backend methods, prefixed by an
43    underscore."""
44
45    def __init__(self):
46        self._ctx_domain = ''
47        self._ctx_configuration = ''
48        self._ctx_sequence_aware = False
49        self._ctx_command = ''
50
51        self._domain_valid = False
52        self._configuration_valid = False
53
54    def _getContext(self):
55        return {
56                'domain': self._ctx_domain,
57                'configuration': self._ctx_configuration,
58                'sequence_aware': self._ctx_sequence_aware,
59                'command': self._ctx_command}
60
61    def _check(self, func):
62        """Check and handles exceptions
63
64        Returns False if an exception occured and was properly caught,
65        True otherwise"""
66        def wrapped(*args, **kwargs):
67            try:
68                func(*args, **kwargs)
69            except PfwException as ex:
70                self._handleException(ex)
71                return False
72
73            return True
74
75        return wrapped
76
77    def createDomain(self, name, sequence_aware=False):
78        """Create a domain with a given name and optionally set its sequence
79        awareness"""
80
81        self._ctx_command = "createDomain"
82        self._ctx_domain = name
83        self._ctx_configuration = ''
84        self._ctx_sequence_aware = sequence_aware
85        self._domain_valid = True
86
87        if not self._check(self._doCreateDomain)(name):
88            self._domain_valid = False
89        elif sequence_aware:
90            self._check(self._doSetSequenceAware)()
91
92    def addElement(self, path):
93        """Add a configurable element to the current domain"""
94
95        self._ctx_command = "addElement"
96
97        if not self._domain_valid:
98            return
99
100        self._check(self._doAddElement)(path)
101
102    def createConfiguration(self, name):
103        """Create a configuration for the current domain"""
104
105        self._ctx_command = "createConfiguration"
106        self._ctx_configuration = name
107        self._configuration_valid = True
108
109        if not self._domain_valid:
110            self._configuration_valid = False
111            return
112
113        if not self._check(self._doCreateConfiguration)(name):
114            self._configuration_valid = False
115
116    def setElementSequence(self, paths):
117        """Set the element sequence (if applicable, e.g. if the domain is
118        sequence-aware) of the current configuration"""
119
120        self._ctx_command = "setElementSequence"
121
122        if not self._configuration_valid:
123            return
124
125        if not self._ctx_sequence_aware:
126            return
127
128        self._check(self._doSetElementSequence)(paths)
129
130    def setRule(self, rule):
131        """Set the current configuration's applicability rule"""
132
133        self._ctx_command = "setRule"
134
135        if not self._configuration_valid:
136            return
137
138        self._doSetRule(rule)
139
140    def setParameter(self, path, value):
141        """Set a parameter value for the current configuration"""
142
143        self._ctx_command = "setParameter"
144
145        if not self._configuration_valid:
146            return
147
148        self._check(self._doSetParameter)(path, value)
149
150    def _handleException(self, exception):
151        raise exception
152
153    def _notImplemented(self):
154        raise NotImplementedError(
155            "{} is an abstract class".format(self.__class__))
156
157    # Implementation methods
158    def _doCreateDomain(self, name):
159        self._notImplemented()
160
161    def _doSetSequenceAware(self):
162        self._notImplemented()
163
164    def _doAddElement(self, path):
165        self._notImplemented()
166
167    def _doCreateConfiguration(self, name):
168        self._notImplemented()
169
170    def _doSetElementSequence(self, paths):
171        self._notImplemented()
172
173    def _doSetRule(self, rule):
174        self._notImplemented()
175
176    def _doSetParameter(self, path, value):
177        self._notImplemented()
178