ticketfrei3/kibicara/config.py

85 lines
2.3 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
"""Configuration file and command line argument parser.
2020-07-11 10:54:07 +00:00
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-10-13 08:35:20 +00:00
from sys import argv
2020-09-11 22:01:30 +00:00
from nacl.secret import SecretBox
from nacl.utils import random
2020-07-01 19:21:39 +00:00
from pytoml import load
config = {
'database_connection': 'sqlite:////tmp/kibicara.sqlite',
'frontend_url': 'http://localhost:4200', # url of frontend, change in prod
2020-09-11 22:01:30 +00:00
'secret': random(SecretBox.KEY_SIZE).hex(), # generate with: openssl rand -hex 32
# production params
2020-09-11 16:21:24 +00:00
'frontend_path': None, # required, path to frontend html/css/js files
'production': True,
2020-09-11 16:21:24 +00:00
'behind_proxy': False,
'keyfile': None, # optional for ssl
'certfile': None, # optional for ssl
# dev params
2020-09-11 16:21:24 +00:00
'root_url': 'http://localhost:8000', # url of backend
'cors_allow_origin': 'http://localhost:4200',
}
"""Default configuration.
2020-07-11 10:54:07 +00:00
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',
2020-10-13 08:12:35 +00:00
action='count',
help='Raise verbosity level',
2020-07-10 12:26:34 +00:00
)
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
2020-10-13 08:12:35 +00:00
parser.add_argument('recipient')
2020-07-15 21:50:24 +00:00
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