aboutsummaryrefslogtreecommitdiffstats
path: root/resources/tools/telemetry/parser.py
blob: c6cc167066599643a85f0e72a189c5667148fe87 (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# Copyright (c) 2021 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.

"""Configuration parsing library."""

from logging import getLogger
from pathlib import Path
import sys

from yaml import safe_load, YAMLError


class Parser:
    """
    Parser class reponsible for loading configuration.
    """
    def __init__(self, configuration_file):
        """
        Config Parser init.

        :param configuration_file: Telemetry configuration file path.
        :type configuration_file: str
        """
        self.instance = None
        self.config = None
        self.suffix = Path(configuration_file).suffix[1:].capitalize()

        try:
            self.instance = globals()[self.suffix+"Loader"](configuration_file)
        except KeyError:
            raise ParserError(u"Unsupported file format!")

        self.config = FileLoader(self.instance).load()


class FileLoader:
    """
    Creates a File Loader object. This is the main object for interacting
    with configuration file.
    """
    def __init__(self, loader):
        """
        File Loader class init.

        :param loader: Loader object responsible for handling file type.
        :type loader: obj
        """
        self.loader = loader

    def load(self):
        """
        File format parser.
        """
        return self.loader.load()


class YamlLoader:
    """
    Creates a YAML Loader object. This is the main object for interacting
    with YAML file.
    """
    def __init__(self, configuration_file):
        """
        YAML Loader class init.

        :param configuration_file: YAML configuration file path.
        :type configuration_file: str
        """
        self.configuration_file = configuration_file

    def load(self):
        """
        YAML format parser.
        """
        with open(self.configuration_file, u"r") as stream:
            try:
                return safe_load(stream)
            except YAMLError as exc:
                raise ParserError(str(exc))


class ParserError(Exception):
    """
    Creates a Parser Error Exception. This exception is supposed to handle
    all the errors raised during processing.
    """
    def __init__(self, message):
        """
        Parser Error Excpetion init.

        :param message: Exception error message.
        :type message: str
        """
        super().__init__()
        self.message = message
        getLogger(__name__).error(self.message)
        sys.exit(1)