ticketfrei3/kibicara/config.py

71 lines
1.8 KiB
Python
Raw Normal View History

2020-07-01 19:21:39 +00:00
# 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>
2020-07-01 19:21:39 +00:00
#
# SPDX-License-Identifier: 0BSD
2020-07-11 10:54:07 +00:00
""" 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
2020-07-01 19:21:39 +00:00
from pytoml import load
from sys import argv
2020-07-01 19:21:39 +00:00
config = {
'database_connection': 'sqlite:////tmp/kibicara.sqlite',
'frontend_path': None,
2020-07-15 21:50:24 +00:00
'root_url': 'http://localhost:8000',
}
2020-07-11 10:54:07 +00:00
""" Default configuration.
The default configuration gets overwritten by a configuration file if one exists.
"""
2020-07-01 19:21:39 +00:00
2020-07-15 21:50:24 +00:00
args = None
2020-07-10 21:50:57 +00:00
if argv[0].endswith('kibicara'):
parser = ArgumentParser()
parser.add_argument(
'-f',
'--config',
dest='configfile',
default='/etc/kibicara.conf',
help='path to config file',
)
2020-07-10 12:26:34 +00:00
parser.add_argument(
'-v', '--verbose', action="count", help="Raise verbosity level",
)
args = parser.parse_args()
2020-07-01 19:21:39 +00:00
2020-07-15 21:50:24 +00:00
if argv[0].endswith('kibicara_mda'):
parser = ArgumentParser()
parser.add_argument(
'-f',
'--config',
dest='configfile',
default='/etc/kibicara.conf',
help='path to config file',
)
# the MDA passes the recipient address as command line argument
parser.add_argument("recipient")
args = parser.parse_args()
if args is not None:
try:
with open(args.configfile) as configfile:
config.update(load(configfile))
except FileNotFoundError:
# run with default config
pass