Skip to content

Commit

Permalink
update server_properties_helper
Browse files Browse the repository at this point in the history
  • Loading branch information
Ableytner committed Mar 12, 2024
1 parent 14ad342 commit a777fbf
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 13 deletions.
41 changes: 28 additions & 13 deletions mcserverwrapper/src/server_properties_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@
DEFAULT_MAX_PLAYERS = 20
DEFAULT_ONLINE_MODE = "true"

ALL_PROPERTIES = [
"port",
"maxp",
"onli"
]

def get_properties(server_path: str) -> dict[str, Any]:
"""Return the currently stored properties"""

return parse_properties_args(server_path, None)

def parse_properties_args(server_path: str, server_property_args: dict | None) -> dict[str, Any]:
"""Parse the given server_properties_args and provide defaults for missing values"""

Expand All @@ -26,22 +37,18 @@ def parse_properties_args(server_path: str, server_property_args: dict | None) -
with open(props_path, "r", encoding="utf8") as props_file:
lines = props_file.read().splitlines()

if "port" not in server_property_args:
for line in lines:
for line in lines:
if "port" not in server_property_args:
if "server-port=" in line:
port_string = line.split("=")[1]
if port_string.isdecimal():
server_property_args["port"] = int(port_string)

if "maxp" not in server_property_args:
for line in lines:
if "maxp" not in server_property_args:
if "max-players=" in line:
maxp_string = line.split("=")[1]
if maxp_string.isdecimal():
server_property_args["maxp"] = int(maxp_string)

if "onli" not in server_property_args:
for line in lines:
if "onli" not in server_property_args:
if "online-mode=" in line:
server_property_args["onli"] = line.split("=")[1]

Expand All @@ -62,24 +69,32 @@ def save_properties(server_path: str, server_property_args: dict[str, Any]) -> N

props_path = os.path.join(server_path, "server.properties")
if not os.path.isfile(props_path):
raise FileNotFoundError("File server.properties does not exist")
# create empty file
with open(props_path, "w+", encoding="utf8"):
pass

with open(props_path, "r", encoding="utf8") as properties:
lines = properties.readlines()

# update existing props
missing_props = ALL_PROPERTIES.copy()
for index, line in enumerate(lines):
if "server-port=" in line:
lines[index] = f"server-port={server_property_args['port']}\n"
missing_props.remove("port")
if "max-players=" in line:
lines[index] = f"max-players={server_property_args['maxp']}\n"
missing_props.remove("maxp")
if "online-mode=" in line:
lines[index] = f"online-mode={server_property_args['onli']}\n"
missing_props.remove("onli")

if "server-port=" not in lines:
# add missing properties
if "port" in missing_props:
lines.append(f"server-port={server_property_args['port']}\n")
if "max-players=" not in lines:
if "maxp" in missing_props:
lines.append(f"max-players={server_property_args['maxp']}\n")
if "online-mode=" not in lines:
if "onli" in missing_props:
lines.append(f"online-mode={server_property_args['onli']}\n")

with open(props_path, "w", encoding="utf8") as properties:
Expand All @@ -92,7 +107,7 @@ def _validate_property_args(server_property_args: dict[str, Any]):
raise ValueError(f"Incorrect length of elements '{len(server_property_args)}'" + \
f" for server_property_args, expected '{PROPERTY_ARGS_COUNT}'")

required_keys = ["port", "maxp", "onli"]
required_keys = ALL_PROPERTIES.copy()
for k in required_keys:
if not k in server_property_args:
raise KeyError(f"Missing key '{k}' in server_property_property_args")
Expand Down
27 changes: 27 additions & 0 deletions mcserverwrapper/test/test_server_properties_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,27 @@

from mcserverwrapper.src import server_properties_helper as sph

def test_get_mixed_params():
"""Tests the helper with mixed params"""

props = {
"server-port": 25506,
"max-players": 18,
"some-unknown-prop": "hehe",
"online-mode": "true"
}

props_path = os.path.join(pathlib.Path(__file__).parent.resolve(), "temp")
with open(os.path.join(props_path, "server.properties"), "w+", encoding="utf8") as props_file:
props_file.write("\n".join([f"{key}={value}" for key, value in props.items()]))

result = sph.get_properties(props_path)

assert len(result) == 3
assert result["maxp"] == props["max-players"]
assert result["onli"] == props["online-mode"]
assert result["port"] == props["server-port"]

def test_parse_custom_params():
"""Tests the helper with custom params"""

Expand Down Expand Up @@ -37,6 +58,7 @@ def test_parse_default_params():
assert "maxp" not in props
assert "onli" not in props

assert len(result) == 3
assert result["port"] == sph.DEFAULT_PORT
assert result["maxp"] == sph.DEFAULT_MAX_PLAYERS
assert result["onli"] == sph.DEFAULT_ONLINE_MODE
Expand Down Expand Up @@ -65,6 +87,7 @@ def test_params_with_file():
assert props["maxp"] == 22
assert "onli" not in props

assert len(result) == 3
assert result["port"] == 25599
assert result["maxp"] == 22
assert result["onli"] == "true"
Expand All @@ -83,6 +106,7 @@ def test_parse_mixed_params_1():
assert "maxp" not in props
assert "onli" not in props

assert len(result) == 3
assert result["port"] == 25541
assert result["maxp"] == sph.DEFAULT_MAX_PLAYERS
assert result["onli"] == sph.DEFAULT_ONLINE_MODE
Expand All @@ -102,6 +126,7 @@ def test_parse_mixed_params_2():
assert props["maxp"] == 43
assert props["onli"] == "false"

assert len(result) == 3
assert result["port"] == sph.DEFAULT_PORT
assert result["maxp"] == 43
assert result["onli"] == "false"
Expand Down Expand Up @@ -135,6 +160,7 @@ def test_save_existing():
with open(os.path.join(props_path, "server.properties"), "r", encoding="utf8") as props_file:
lines = props_file.read().splitlines()

assert len(lines) == 5
assert lines[0] == "server-port=25546"
assert lines[1] == "some-other-prop=haha"
assert lines[2] == "max-players=21"
Expand Down Expand Up @@ -167,6 +193,7 @@ def test_save_new():
with open(os.path.join(props_path, "server.properties"), "r", encoding="utf8") as props_file:
lines = props_file.read().splitlines()

assert len(lines) == 5
assert lines[0] == "some-other-prop=haha"
assert lines[1] == "a-final-prop=aha"
assert lines[2] == "server-port=25546"
Expand Down

0 comments on commit a777fbf

Please sign in to comment.