From 947b454faff11bbd777a63576aaa9ba87818aef8 Mon Sep 17 00:00:00 2001 From: Bram van den Heuvel Date: Sun, 1 Oct 2023 03:05:53 +0200 Subject: [PATCH] Config refactor Refactor the config away from environment variables to one YAML file that is relatively easy to edit as a Docker container volume. --- build_server.py | 14 +++++++++----- config.py | 41 +++++++++++++++++++++++++++++++++++------ config.yaml | 3 +++ main.py | 2 +- 4 files changed, 48 insertions(+), 12 deletions(-) diff --git a/build_server.py b/build_server.py index 86036a6..60a011d 100644 --- a/build_server.py +++ b/build_server.py @@ -25,7 +25,7 @@ def rewriter(file_name): @rewriter('eula.txt') def confirm_eula(line : str, line_no : int, data): """Confirm the Minecraft EULA""" - if os.getenv('EULA') is None or os.getenv('EULA').lower() != 'true': + if not config.EULA: return line else: return line.replace('eula=false', 'eula=true') @@ -36,9 +36,13 @@ def fill_in_server_settings(line : str, line_no : int, data): if line.strip().startswith('#'): return line - value = line.split('=')[0] + key = line.split('=')[0] - if value in config.SERVER_SETTINGS: - return value + '=' + str(config.SERVER_SETTINGS[value]) + '\n' + server_settings = config.at(['minecraft']) or {} + + try: + value = server_settings[key] + except IndexError: + return line else: - return line \ No newline at end of file + return key + '=' + str(value) + '\n' diff --git a/config.py b/config.py index afb124d..38f7a70 100644 --- a/config.py +++ b/config.py @@ -1,9 +1,41 @@ import os +import yaml +from typing import Any, List, Optional + +with open('config.yaml', 'r') as open_file: + SETTINGS = yaml.load(open_file) + +def at(keys : List[str]) -> Optional[Any]: + """ + Potentially get a value. If it doesn't exist, return None. + """ + return at_value(keys, SETTINGS) + +def at_value(keys : List[str], value : Any) -> Optional[Any]: + try: + head, tail = keys[0], keys[1:] + except IndexError: + return value + else: + try: + new_value = value[head] + except TypeError: + return None + except KeyError: + return None + else: + return at_value(tail, new_value) + +# EULA +EULA = at(['minecraft', 'eula']) or False # Minecraft bridge credentials -MATRIX_HOMESERVER = os.getenv('MATRIX_HOMESERVER') or "https://homeserv.er" -MATRIX_USERNAME = os.getenv('MATRIX_USERNAME') or "@bridge_username:homeserv.er" -MATRIX_PASSWORD = os.getenv('MATRIX_PASSWORD') or "bridge_password" +MATRIX_HOMESERVER = at(['matrix', 'homeserver']) or "https://matrix.example.org/" +MATRIX_USERNAME = at(['matrix', 'username']) or "@alice:example.org" +MATRIX_PASSWORD = at(['matrix', 'password']) or "bridge_password" + +# Matrix bridge room +MATRIX_ROOM = at(['matrix', 'room_id']) or "!channel_id:example.org" SERVER_IP = os.getenv('SERVER_ADDRESS') or 'unknown ip' @@ -16,9 +48,6 @@ MC_ADMINS = [ if os.getenv('MATRIX_ADMINS') is not None: MC_ADMINS = os.getenv('MATRIX_ADMINS').split(',') -# Matrix channel that the bot should talk to -MC_CHANNEL = os.getenv('MC_CHANNEL') or "!channel_id:homeserv.er" - make_bool = lambda os_value, default_value : default_value if not os_value else ( False if os_value.lower() == 'false' else True ) diff --git a/config.yaml b/config.yaml index 2325e80..d15db24 100644 --- a/config.yaml +++ b/config.yaml @@ -10,6 +10,9 @@ matrix: # Matrix room room_id: "!channel_id:example.org" + # IP address or domain where users can join the server + server_address: unknown ip + # List of Matrix users that can send commands to the bridge. # When a message starts with a slash, (/) the bridge will interpret it as a # Minecraft command and will put that as a command into the console. diff --git a/main.py b/main.py index 2fc83d5..1583264 100644 --- a/main.py +++ b/main.py @@ -13,7 +13,7 @@ STARTUP_TIME = time.time() client = AsyncClient(config.MATRIX_HOMESERVER, config.MATRIX_USERNAME) async def message_callback(room: MatrixRoom, event: RoomMessageText) -> None: - if room.machine_name != config.MC_CHANNEL: + if room.machine_name != config.MATRIX_ROOM: return if event.sender == client.user_id: return