X-Git-Url: https://gerrit.fd.io/r/gitweb?a=blobdiff_plain;f=csit.infra.dash%2Fapp%2Fcdash%2Futils%2Fcontrol_panel.py;fp=csit.infra.dash%2Fapp%2Fcdash%2Futils%2Fcontrol_panel.py;h=d892dfa4c1ff2879634526644d9cb298c1fd2ae7;hb=430c577e8e8a737cb46e67cbe802e038b8ffd25a;hp=0000000000000000000000000000000000000000;hpb=8002cfbc97bb0af9bc84cb2353350d9af4e5afc2;p=csit.git diff --git a/csit.infra.dash/app/cdash/utils/control_panel.py b/csit.infra.dash/app/cdash/utils/control_panel.py new file mode 100644 index 0000000000..d892dfa4c1 --- /dev/null +++ b/csit.infra.dash/app/cdash/utils/control_panel.py @@ -0,0 +1,87 @@ +# Copyright (c) 2022 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 + + +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]