summaryrefslogtreecommitdiffstats
path: root/app/utils/config_file.py
blob: 1982acc4d51363c9ddfa5568c5d2d77e8bb04136 (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
###############################################################################
# Copyright (c) 2017 Koren Lev (Cisco Systems), Yaron Yogev (Cisco Systems)   #
# and others                                                                  #
#                                                                             #
# All rights reserved. This program and the accompanying materials            #
# are made available under the terms of the Apache License, Version 2.0       #
# which accompanies this distribution, and is available at                    #
# http://www.apache.org/licenses/LICENSE-2.0                                  #
###############################################################################
import os


class ConfigFile:

    def __init__(self, file_path):
        super().__init__()
        if not os.path.isfile(file_path):
            raise ValueError("config file doesn't exist in "
                             "the system: {0}"
                             .format(file_path))
        self.config_file = file_path

    def read_config(self):
        params = {}
        try:
            with open(self.config_file) as f:
                for line in f:
                    line = line.strip()
                    if line.startswith("#") or " " not in line:
                        continue
                    index = line.index(" ")
                    key = line[: index].strip()
                    value = line[index + 1:].strip()
                    if value:
                        params[key] = value
        except Exception as e:
            raise e
        return params

    @staticmethod
    def get(file_name):
        # config file is taken from app/config by default
        # look in the current work directory to get the
        # config path
        python_path = os.environ['PYTHONPATH']
        if os.pathsep in python_path:
            python_path = python_path.split(os.pathsep)[0]
        return python_path + '/config/' + file_name