feat(tests): IPv6 fixes
[csit.git] / resources / tools / telemetry / parser.py
1 # Copyright (c) 2021 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 """Configuration parsing library."""
15
16 from logging import getLogger
17 from pathlib import Path
18 import sys
19
20 from yaml import safe_load, YAMLError
21
22
23 class Parser:
24     """
25     Parser class reponsible for loading configuration.
26     """
27     def __init__(self, configuration_file):
28         """
29         Config Parser init.
30
31         :param configuration_file: Telemetry configuration file path.
32         :type configuration_file: str
33         """
34         self.instance = None
35         self.config = None
36         self.suffix = Path(configuration_file).suffix[1:].capitalize()
37
38         try:
39             self.instance = globals()[self.suffix+"Loader"](configuration_file)
40         except KeyError:
41             raise ParserError(u"Unsupported file format!")
42
43         self.config = FileLoader(self.instance).load()
44
45
46 class FileLoader:
47     """
48     Creates a File Loader object. This is the main object for interacting
49     with configuration file.
50     """
51     def __init__(self, loader):
52         """
53         File Loader class init.
54
55         :param loader: Loader object responsible for handling file type.
56         :type loader: obj
57         """
58         self.loader = loader
59
60     def load(self):
61         """
62         File format parser.
63         """
64         return self.loader.load()
65
66
67 class YamlLoader:
68     """
69     Creates a YAML Loader object. This is the main object for interacting
70     with YAML file.
71     """
72     def __init__(self, configuration_file):
73         """
74         YAML Loader class init.
75
76         :param configuration_file: YAML configuration file path.
77         :type configuration_file: str
78         """
79         self.configuration_file = configuration_file
80
81     def load(self):
82         """
83         YAML format parser.
84         """
85         with open(self.configuration_file, u"r") as stream:
86             try:
87                 return safe_load(stream)
88             except YAMLError as exc:
89                 raise ParserError(str(exc))
90
91
92 class ParserError(Exception):
93     """
94     Creates a Parser Error Exception. This exception is supposed to handle
95     all the errors raised during processing.
96     """
97     def __init__(self, message):
98         """
99         Parser Error Excpetion init.
100
101         :param message: Exception error message.
102         :type message: str
103         """
104         super().__init__()
105         self.message = message
106         getLogger(__name__).error(self.message)
107         sys.exit(1)