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