feat: add config file handling
This commit is contained in:
parent
b5912a8b6c
commit
93f84859b4
3 changed files with 81 additions and 21 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -4,3 +4,4 @@
|
|||
coverage.xml
|
||||
htmlcov
|
||||
**__pycache__
|
||||
config.yaml
|
||||
|
|
21
config-dist.yaml
Normal file
21
config-dist.yaml
Normal file
|
@ -0,0 +1,21 @@
|
|||
hostname: localhost
|
||||
port: 4785
|
||||
# matrix-specific settings
|
||||
matrix:
|
||||
# URL of homeserver to connect
|
||||
url: https://matrix.org
|
||||
# user to connect to homserver as
|
||||
id: username
|
||||
# password for the user
|
||||
pw: password
|
||||
# keys to allow These should be random strings
|
||||
# these could be generated with something like `openssl rand -hex 24`
|
||||
# change these, you only need
|
||||
api:
|
||||
keys:
|
||||
# change these keys.
|
||||
- d6a26923e41492d89cffd9e5b6d9838a89e71ffa061edb5d # Can add a comment for what the key is used
|
||||
- 149d866b2591ceee2b254ca8b16c08fd27a7e2eed7ff01e0
|
||||
# location of logfile
|
||||
log:
|
||||
level: debug
|
|
@ -1,6 +1,25 @@
|
|||
"""Configuration for Matrix Webhook."""
|
||||
import argparse
|
||||
import os
|
||||
import sys
|
||||
import yaml
|
||||
|
||||
|
||||
def get_numeric_log_level(log_level):
|
||||
"""Return a number that will calculate to the verbosity level"""
|
||||
if log_level.lower() == "debug":
|
||||
return 4
|
||||
elif log_level.lower() == "info":
|
||||
return 3
|
||||
elif log_level.lower() == "warning":
|
||||
return 2
|
||||
elif log_level.lower() == "error":
|
||||
return 1
|
||||
elif log_level.lower() == "critical":
|
||||
return 0
|
||||
else:
|
||||
return 2
|
||||
|
||||
|
||||
parser = argparse.ArgumentParser(description=__doc__, prog="python -m matrix_webhook")
|
||||
parser.add_argument(
|
||||
|
@ -27,41 +46,60 @@ parser.add_argument(
|
|||
"-i",
|
||||
"--matrix-id",
|
||||
help="matrix user-id. Required. Environment variable: `MATRIX_ID`",
|
||||
**(
|
||||
{"default": os.environ["MATRIX_ID"]}
|
||||
if "MATRIX_ID" in os.environ
|
||||
else {"required": True}
|
||||
),
|
||||
)
|
||||
parser.add_argument(
|
||||
"-p",
|
||||
"--matrix-pw",
|
||||
help="matrix password. Required. Environment variable: `MATRIX_PW`",
|
||||
**(
|
||||
{"default": os.environ["MATRIX_PW"]}
|
||||
if "MATRIX_PW" in os.environ
|
||||
else {"required": True}
|
||||
),
|
||||
)
|
||||
parser.add_argument(
|
||||
"-k",
|
||||
"--api-keys",
|
||||
help="comma separated list of shared secrets to use this service. Required. Environment variable: `API_KEYS`",
|
||||
**(
|
||||
{"default": os.environ["API_KEYS"]}
|
||||
if "API_KEYS" in os.environ
|
||||
else {"required": True}
|
||||
),
|
||||
)
|
||||
parser.add_argument(
|
||||
"-c",
|
||||
"--config",
|
||||
help="configuration file. Default: `config.yaml`",
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
"-v", "--verbose", action="count", default=0, help="increment verbosity level"
|
||||
)
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
SERVER_ADDRESS = (args.host, args.port)
|
||||
MATRIX_URL = args.matrix_url
|
||||
MATRIX_ID = args.matrix_id
|
||||
MATRIX_PW = args.matrix_pw
|
||||
API_KEYS = args.api_keys.split(",")
|
||||
VERBOSE = args.verbose
|
||||
if args.config:
|
||||
with open(args.config) as f:
|
||||
config = yaml.safe_load(f)
|
||||
SERVER_ADDRESS = (config["hostname"], config["port"])
|
||||
MATRIX_URL = config["matrix"]["url"]
|
||||
MATRIX_ID = config["matrix"]["id"]
|
||||
MATRIX_PW = config["matrix"]["pw"]
|
||||
API_KEYS = config["api_keys"]
|
||||
LOG_FILE = config["log"]
|
||||
VERBOSE = get_numeric_log_level(config["log"]["level"])
|
||||
else:
|
||||
SERVER_ADDRESS = (args.host, args.port)
|
||||
MATRIX_URL = args.matrix_url
|
||||
LOG_FILE = args.log
|
||||
if not args.matrix_id:
|
||||
print("Missing matrix user-id. Use -i or --matrix-id or specify in config.yaml")
|
||||
sys.exit(1)
|
||||
else:
|
||||
MATRIX_ID = args.matrix_id
|
||||
|
||||
if not args.matrix_pw:
|
||||
print(
|
||||
"Missing matrix password. Use -p or --matrix-pw or specify in config.yaml"
|
||||
)
|
||||
sys.exit(1)
|
||||
else:
|
||||
MATRIX_PW = args.matrix_pw
|
||||
|
||||
if not args.api_keys:
|
||||
print("Missing api keys. Use -k or --api-keys or specify in config.yaml")
|
||||
sys.exit(1)
|
||||
else:
|
||||
API_KEYS = args.api_keys.split(",")
|
||||
VERBOSE = args.verbose
|
||||
|
|
Loading…
Reference in a new issue