2019-06-14 19:02:20 +02:00
|
|
|
"""
|
|
|
|
Doorlockd -- Binary Kitchen's smart door opener
|
|
|
|
|
2019-06-14 22:02:59 +02:00
|
|
|
Copyright (c) Binary Kitchen e.V., 2018-2019
|
2019-06-14 19:02:20 +02:00
|
|
|
|
|
|
|
Author:
|
|
|
|
Ralf Ramsauer <ralf@binary-kitchen.de>
|
|
|
|
Thomas Schmid <tom@binary-kitchen.de>
|
|
|
|
|
|
|
|
This work is licensed under the terms of the GNU GPL, version 2. See
|
|
|
|
the LICENSE file in the top-level directory.
|
|
|
|
|
|
|
|
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.
|
|
|
|
"""
|
|
|
|
|
2019-06-14 22:02:59 +02:00
|
|
|
import functools
|
2019-06-14 19:02:20 +02:00
|
|
|
from configparser import ConfigParser
|
|
|
|
from os.path import join
|
|
|
|
|
2019-06-14 22:02:59 +02:00
|
|
|
|
2019-06-15 20:00:25 +02:00
|
|
|
SYSCONFDIR = './etc'
|
|
|
|
PREFIX = '.'
|
|
|
|
|
|
|
|
root_prefix = join(PREFIX, 'share', 'doorlockd')
|
|
|
|
sounds_prefix = join(root_prefix, 'sounds')
|
|
|
|
|
|
|
|
|
2019-06-14 22:02:59 +02:00
|
|
|
def check_exists(func):
|
|
|
|
@functools.wraps(func)
|
|
|
|
def decorator(*args, **kwargs):
|
|
|
|
config = args[0]
|
2022-11-16 20:37:24 +01:00
|
|
|
key = args[1]
|
|
|
|
|
|
|
|
if (len(args) == 3):
|
|
|
|
section = args[2]
|
|
|
|
else:
|
|
|
|
section = config.config_topic
|
|
|
|
|
|
|
|
if section is None:
|
|
|
|
section = config.config_topic
|
|
|
|
|
|
|
|
if not config.config.has_option(section, key):
|
2019-06-14 22:02:59 +02:00
|
|
|
return None
|
2022-11-16 20:37:24 +01:00
|
|
|
|
2019-06-14 22:02:59 +02:00
|
|
|
return func(*args, **kwargs)
|
|
|
|
return decorator
|
|
|
|
|
|
|
|
|
2019-06-14 19:02:20 +02:00
|
|
|
class Config:
|
2019-06-15 20:00:25 +02:00
|
|
|
def __init__(self, config_topic):
|
2019-06-15 19:54:37 +02:00
|
|
|
self.config_topic = config_topic
|
2019-06-14 19:02:20 +02:00
|
|
|
self.config = ConfigParser()
|
2019-06-15 20:00:25 +02:00
|
|
|
self.config.read(join(SYSCONFDIR, 'doorlockd.cfg'))
|
2019-06-14 19:02:20 +02:00
|
|
|
|
2019-06-14 22:02:59 +02:00
|
|
|
@check_exists
|
2022-11-16 20:37:24 +01:00
|
|
|
def boolean(self, key, topic = None):
|
|
|
|
if topic == None:
|
|
|
|
topic = self.config_topic
|
|
|
|
|
|
|
|
return self.config.getboolean(topic, key)
|
2019-06-14 19:02:20 +02:00
|
|
|
|
2019-06-14 22:02:59 +02:00
|
|
|
@check_exists
|
2022-11-16 20:37:24 +01:00
|
|
|
def str(self, key, topic = None):
|
|
|
|
if topic == None:
|
|
|
|
topic = self.config_topic
|
|
|
|
|
|
|
|
return self.config.get(topic, key)
|
2019-06-14 19:02:20 +02:00
|
|
|
|
2019-06-14 22:02:59 +02:00
|
|
|
@check_exists
|
2022-11-16 20:37:24 +01:00
|
|
|
def int(self, key, topic = None):
|
|
|
|
if topic == None:
|
|
|
|
topic = self.config_topic
|
|
|
|
|
|
|
|
return self.config.getint(topic, key)
|