C-Dash: Add search in tests
[csit.git] / csit.infra.dash / app / cdash / utils / trigger.py
1 # Copyright (c) 2024 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 processing of a trigger.
15 """
16
17 from typing import Any
18
19 from json import loads, JSONDecodeError
20
21
22 class Trigger:
23     """
24     """
25     def __init__(self, trigger) -> None:
26         """
27         """
28         self._id = trigger[0]["prop_id"].split(".")
29         self._param = self._id[1]
30         try:
31             self._id = loads(self._id[0])
32         except (JSONDecodeError, TypeError):
33             # It is a string
34             self._id = {"type": self._id[0], "index": None}
35         self._val = trigger[0]["value"]
36
37     def __str__(self) -> str:
38         return (
39             f"\nTrigger:\n"
40             f"  ID:        {self._id}\n"
41             f"  Type:      {self._id['type']}\n"
42             f"  Index:     {self._id['index']}\n"
43             f"  Parameter: {self._param}\n"
44             f"  Value:     {self._val}\n"
45         )
46
47     @property
48     def id(self) -> dict:
49         return self._id
50
51     @property
52     def type(self) -> str:
53         return self._id["type"]
54
55     @property
56     def idx(self) -> Any:
57         return self._id["index"]
58
59     @property
60     def parameter(self) -> str:
61         return self._param
62
63     @property
64     def value(self) -> Any:
65         return self._val