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>
|
2020-07-09 13:11:26 +00:00
|
|
|
# Copyright (C) 2020 by Martin Rey <martin.rey@mailbox.org>
|
2020-07-01 19:21:39 +00:00
|
|
|
#
|
|
|
|
# SPDX-License-Identifier: 0BSD
|
|
|
|
|
2020-10-13 09:38:33 +00:00
|
|
|
"""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)
|
|
|
|
```
|
|
|
|
"""
|
|
|
|
|
2020-07-09 13:11:26 +00:00
|
|
|
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 = {
|
2020-07-02 12:31:53 +00:00
|
|
|
'database_connection': 'sqlite:////tmp/kibicara.sqlite',
|
2020-09-11 16:21:24 +00:00
|
|
|
'frontend_url': 'http://127.0.0.1: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
|
2020-09-11 02:05:33 +00:00
|
|
|
# production params
|
2020-09-11 16:21:24 +00:00
|
|
|
'frontend_path': None, # required, path to frontend html/css/js files
|
2020-07-25 11:34:18 +00:00
|
|
|
'production': True,
|
2020-09-11 16:21:24 +00:00
|
|
|
'behind_proxy': False,
|
|
|
|
'keyfile': None, # optional for ssl
|
|
|
|
'certfile': None, # optional for ssl
|
2020-09-11 02:05:33 +00:00
|
|
|
# dev params
|
2020-09-11 16:21:24 +00:00
|
|
|
'root_url': 'http://localhost:8000', # url of backend
|
2020-08-31 19:41:23 +00:00
|
|
|
'cors_allow_origin': 'http://127.0.0.1:4200',
|
2020-07-02 12:31:53 +00:00
|
|
|
}
|
2020-10-13 09:38:33 +00:00
|
|
|
"""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'):
|
2020-07-09 16:14:09 +00:00
|
|
|
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(
|
2020-09-04 23:39:24 +00:00
|
|
|
'-v',
|
|
|
|
'--verbose',
|
2020-10-13 08:12:35 +00:00
|
|
|
action='count',
|
|
|
|
help='Raise verbosity level',
|
2020-07-10 12:26:34 +00:00
|
|
|
)
|
2020-07-09 16:14:09 +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:
|
2020-07-09 16:14:09 +00:00
|
|
|
try:
|
|
|
|
with open(args.configfile) as configfile:
|
|
|
|
config.update(load(configfile))
|
|
|
|
except FileNotFoundError:
|
|
|
|
# run with default config
|
|
|
|
pass
|