C-Dash: Prepare layout for telemetry in trending
[csit.git] / csit.infra.dash / app / cdash / utils / control_panel.py
1 # Copyright (c) 2022 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
13
14 """A module implementing the control panel data structure.
15 """
16
17 from copy import deepcopy
18
19
20 class ControlPanel:
21     """A class representing the control panel.
22     """
23
24     def __init__(self, params: dict, panel: dict) -> None:
25         """Initialisation of the control pannel by default values. If
26         particular values are provided (parameter "panel") they are set
27         afterwards.
28
29         :param params: Default values to be set to the control panel. This
30             dictionary also defines the full set of the control panel's
31             parameters and their order.
32         :param panel: Custom values to be set to the control panel.
33         :type params: dict
34         :type panel: dict
35         """
36
37         if not params:
38             raise ValueError("The params must be defined.")
39         self._panel = deepcopy(params)
40         if panel:
41             for key in panel:
42                 if key in self._panel:
43                     self._panel[key] = panel[key]
44                 else:
45                     raise AttributeError(
46                         f"The parameter {key} is not defined in the list of "
47                         f"parameters."
48                     )
49
50     @property
51     def panel(self) -> dict:
52         return self._panel
53
54     @property
55     def values(self) -> tuple:
56         """Returns the values from the Control panel as a tuple.
57
58         :returns: The values from the Control panel.
59         :rtype: tuple
60         """
61         return tuple(self._panel.values())
62
63     def set(self, kwargs: dict=dict()) -> None:
64         """Set the values of the Control panel.
65
66         :param kwargs: key - value pairs to be set.
67         :type kwargs: dict
68         :raises KeyError: If the key in kwargs is not present in the Control
69             panel.
70         """
71         for key, val in kwargs.items():
72             if key in self._panel:
73                 self._panel[key] = val
74             else:
75                 raise KeyError(f"The key {key} is not defined.")
76
77     def get(self, key: str) -> any:
78         """Returns the value of a key from the Control panel.
79
80         :param key: The key which value should be returned.
81         :type key: str
82         :returns: The value of the key.
83         :rtype: any
84         :raises KeyError: If the key in kwargs is not present in the Control
85             panel.
86         """
87         return self._panel[key]