aboutsummaryrefslogtreecommitdiffstats
path: root/csit.infra.dash/app/cdash/utils/control_panel.py
blob: 3da44e39010aaee8ed77e66805f77ad4a418ea48 (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
# Copyright (c) 2024 Cisco and/or its affiliates.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""A module implementing the control panel data structure.
"""

from copy import deepcopy
from typing import Any

class ControlPanel:
    """A class representing the control panel.
    """

    def __init__(self, params: dict, panel: dict) -> None:
        """Initialisation of the control pannel by default values. If
        particular values are provided (parameter "panel") they are set
        afterwards.

        :param params: Default values to be set to the control panel. This
            dictionary also defines the full set of the control panel's
            parameters and their order.
        :param panel: Custom values to be set to the control panel.
        :type params: dict
        :type panel: dict
        """

        if not params:
            raise ValueError("The params must be defined.")
        self._panel = deepcopy(params)
        if panel:
            for key in panel:
                if key in self._panel:
                    self._panel[key] = panel[key]
                else:
                    raise AttributeError(
                        f"The parameter {key} is not defined in the list of "
                        f"parameters."
                    )

    @property
    def panel(self) -> dict:
        return self._panel

    @property
    def values(self) -> tuple:
        """Returns the values from the Control panel as a tuple.

        :returns: The values from the Control panel.
        :rtype: tuple
        """
        return tuple(self._panel.values())

    def set(self, kwargs: dict=dict()) -> None:
        """Set the values of the Control panel.

        :param kwargs: key - value pairs to be set.
        :type kwargs: dict
        :raises KeyError: If the key in kwargs is not present in the Control
            panel.
        """
        for key, val in kwargs.items():
            if key in self._panel:
                self._panel[key] = val
            else:
                raise KeyError(f"The key {key} is not defined.")

    def get(self, key: str) -> Any:
        """Returns the value of a key from the Control panel.

        :param key: The key which value should be returned.
        :type key: str
        :returns: The value of the key.
        :rtype: any
        :raises KeyError: If the key in kwargs is not present in the Control
            panel.
        """
        return self._panel[key]