From 610c3bb01d7fb8d4b0be01f22c88c9c2e2d77afe Mon Sep 17 00:00:00 2001 From: Bram van den Heuvel Date: Sun, 1 Oct 2023 16:41:00 +0200 Subject: [PATCH] Move config.py --- Dockerfile | 14 +++-- main.py | 7 +++ src/build_server.py | 2 +- config.py => src/config.py | 124 ++++++++++++++++++------------------- src/mc_wrapper.py | 2 +- src/mxclient.py | 5 +- 6 files changed, 83 insertions(+), 71 deletions(-) create mode 100644 main.py rename config.py => src/config.py (96%) diff --git a/Dockerfile b/Dockerfile index b77b5e0..8a83443 100644 --- a/Dockerfile +++ b/Dockerfile @@ -7,13 +7,17 @@ WORKDIR /usr/src/app COPY requirements.txt ./ RUN pip install --no-cache-dir -r requirements.txt -# Prepare MC server -COPY server.jar ./ -RUN java -jar server.jar --nogui +# # Prepare MC server +# COPY server.jar ./ +# RUN java -jar server.jar --nogui -COPY . . +COPY src/ ./src/ +COPY main.py ./ + +COPY LICENSE.md ./ +COPY config.yaml ./ # Buffer Python's stdout for debugging during runtime ENV PYTHONUNBUFFERED=1 -CMD ["python", "main.py", "java", "-Xmx1024M", "-Xms1024M", "-jar", "server.jar", "nogui"] +CMD ["python", "main.py"] diff --git a/main.py b/main.py new file mode 100644 index 0000000..6b24e60 --- /dev/null +++ b/main.py @@ -0,0 +1,7 @@ +import src.mc_wrapper as mc_wrapper +import src.mxclient as matrix_client + +import asyncio + +# Start the Minecraft process +asyncio.run(matrix_client.start()) diff --git a/src/build_server.py b/src/build_server.py index 9f48491..549bd8c 100644 --- a/src/build_server.py +++ b/src/build_server.py @@ -3,7 +3,7 @@ correct configuration. """ -import config +import src.config as config def write_eula(): """ diff --git a/config.py b/src/config.py similarity index 96% rename from config.py rename to src/config.py index e55b07d..dc06b6f 100644 --- a/config.py +++ b/src/config.py @@ -1,62 +1,62 @@ -""" - This module loads and parses the config.yaml file. -""" - -from typing import Any, List, Optional -import yaml - -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(['config', 'eula']) or False - -# Minecraft bridge credentials -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 = at(['matrix', 'server_address']) or 'unknown ip' - -MATRIX_ADMINS = at(['matrix', 'mc-admins']) or [] - -try: - RAM_SIZE = int(at(['config', 'ram'])) -except TypeError: - RAM_SIZE = 1024 -except ValueError: - RAM_SIZE = 1024 - -SERVER_JAR_LOCATION = at(['config', 'server_jar']) or 'server.jar' - -RUN_COMMAND = [ - 'java', - f'-Xmx{RAM_SIZE}M', - f'-Xms{RAM_SIZE}M', - '-jar', SERVER_JAR_LOCATION, - 'nogui' -] +""" + This module loads and parses the config.yaml file. +""" + +from typing import Any, List, Optional +import yaml + +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(['config', 'eula']) or False + +# Minecraft bridge credentials +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 = at(['matrix', 'server_address']) or 'unknown ip' + +MATRIX_ADMINS = at(['matrix', 'mc-admins']) or [] + +try: + RAM_SIZE = int(at(['config', 'ram'])) +except TypeError: + RAM_SIZE = 1024 +except ValueError: + RAM_SIZE = 1024 + +SERVER_JAR_LOCATION = at(['config', 'server_jar']) or 'server.jar' + +RUN_COMMAND = [ + 'java', + f'-Xmx{RAM_SIZE}M', + f'-Xms{RAM_SIZE}M', + '-jar', SERVER_JAR_LOCATION, + 'nogui' +] diff --git a/src/mc_wrapper.py b/src/mc_wrapper.py index e355c6d..241a47b 100644 --- a/src/mc_wrapper.py +++ b/src/mc_wrapper.py @@ -11,7 +11,7 @@ import asyncio import json import re from src.nbsr import NonBlockingStreamReader as NBSR -import config +import src.config as config import src.build_server as build # Write the appropriate files diff --git a/src/mxclient.py b/src/mxclient.py index 2440316..a83da3d 100644 --- a/src/mxclient.py +++ b/src/mxclient.py @@ -5,7 +5,7 @@ import re from nio import AsyncClient, MatrixRoom, RoomMessageText import src.mc_wrapper as mc_wrapper -import config +import src.config as config STARTUP_TIME = time.time() @@ -57,4 +57,5 @@ async def start(): mc_wrapper.start(client, config.MATRIX_ROOM) ) -asyncio.run(start()) \ No newline at end of file +if __name__ == '__main__': + asyncio.run(start())