config.py 3.68 KB
Newer Older
1
#
2
#  engine
3
#
4
#  Playout Daemon for autoradio project
5
#
6
7
#  Copyright 2014 BFR <info@freie-radios.de>
#  Copyright (C) 2017-2018 Gottfried Gaisbauer <gottfried.gaisbauer@servus.at>
8
#
9
10
11
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; Version 3 of the License
12
#
13
14
15
16
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
17
#
18
19
20
21
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, the license can be downloaded here:
#
#  http://www.gnu.org/licenses/gpl.html
22
23
24
25
26
27
28
29

# Meta
__version__ = '0.1.1'
__license__ = "GNU General Public License (GPL) Version 3"
__version_info__ = (0, 1, 1)
__author__ = 'Michael Liebler <michael-liebler@radio-z.net>'

"""
30
Aura Config Reader
31
32
33
34
"""
import os
import sys
import socket
35
import logging
36
37
38
39
40

from configparser import ConfigParser


class ConfigReader(object):
41
42
43
44
45
    ini_path = ""
    logger = None

    def __init__(self):
        self.logger = logging.getLogger("AuraEngine")
46
47
48

    def set(self, key, value):
        """
49
        Set a property
50
        @type    key: string
51
        @param   key: The Key
52
53
54
        @type    value: mixed
        @param   value: Beliebiger Wert
        """
55
        try:
56
            self.__dict__[key] = int(value)
57
58
        except:
            self.__dict__[key] = str(value)
59

60
    # ------------------------------------------------------------------------------------------ #
61
62
    def get(self, key, default=None):
        """
63
        get a loaded property
64
65
66
67
68
        @type    key: string
        @param   key: Der Key
        @type    default: mixed
        @param   default: Beliebiger Wert
        """
69

70
71
72
73
        if key not in self.__dict__:
            if default:
                self.set(key, default)
            else:
74
                self.logger.warning("Key " + key + " not found in configfile " + self.ini_path + "!")
75
                return None
76

77
78
79
80
81
82
83
84
85
86
87
88
89
90
        if key == "loglevel":
            loglvl = self.__dict__[key]

            if loglvl == "debug":
                return logging.DEBUG
            elif loglvl == "info":
                return logging.INFO
            elif loglvl == "warning":
                return logging.WARNING
            elif loglvl == "error":
                return logging.ERROR
            else:
                return logging.CRITICAL

91
92
93
        if key == "debug":
            return self.__dict__[key].count("y")

94
95
        return self.__dict__[key]

96
97
    # ------------------------------------------------------------------------------------------ #
    def load_config(self):
98
99
100
101
        """
        Set config defaults and load settings from file
        :return:
        """
102
        self.ini_path = self.get('configpath', '/etc/aura/engine.ini')
103

104
105
        if not os.path.isfile(self.ini_path):
            self.logger.critical(self.ini_path + " not found  :(")
106
107
108
            sys.exit(1)

        # INI einlesen
109
110
        f = open(self.ini_path, 'r')
        ini_str = f.read()
111
112
        f.close()

113
114
115
116
117
118
119
120
121
122
123
124
        config_parser = ConfigParser()
        try:
            config_parser.read_string(ini_str)
        except Exception as e:
            self.logger.critical("Cannot read " + self.ini_path + "! Reason: " + str(e))
            sys.exit(0)

        for section in config_parser.sections():
            for key, value in config_parser.items(section):
                v = config_parser.get(section, key).replace('"', '').strip()
                self.set(key, v)

Gottfried Gaisbauer's avatar
Gottfried Gaisbauer committed
125
126
        self.set("install_dir", os.path.realpath(__file__ + "../../../.."))

127