ticketfrei3/kibicara/config.py
2020-07-15 16:53:01 +02:00

55 lines
1.4 KiB
Python

# Copyright (C) 2020 by Thomas Lindner <tom@dl6tom.de>
# Copyright (C) 2020 by Cathy Hu <cathy.hu@fau.de>
# Copyright (C) 2020 by Martin Rey <martin.rey@mailbox.org>
#
# SPDX-License-Identifier: 0BSD
""" Configuration file and command line argument parser.
Gives a dictionary named `config` with configuration parsed either from
`/etc/kibicara.conf` or from a file given by command line argument `-f`.
If no configuration was found at all, the defaults are used.
Example:
```
from kibicara.config import config
print(config)
```
"""
from argparse import ArgumentParser
from pytoml import load
from sys import argv
config = {
'database_connection': 'sqlite:////tmp/kibicara.sqlite',
'frontend_path': None,
'root_url': 'http://localhost:8000/',
}
""" Default configuration.
The default configuration gets overwritten by a configuration file if one exists.
"""
if argv[0].endswith('kibicara'):
parser = ArgumentParser()
parser.add_argument(
'-f',
'--config',
dest='configfile',
default='/etc/kibicara.conf',
help='path to config file',
)
parser.add_argument(
'-v', '--verbose', action="count", help="Raise verbosity level",
)
args = parser.parse_args()
try:
with open(args.configfile) as configfile:
config.update(load(configfile))
except FileNotFoundError:
# run with default config
pass