Refactored config_loader.py and server_config_handler.py into a single file, with server config write/read operations

This commit is contained in:
VakarisZ 2021-05-24 11:05:15 +03:00
parent 43d919a3eb
commit 70b9a9f6b7
6 changed files with 45 additions and 50 deletions

View File

@ -1,21 +0,0 @@
import os
from pathlib import Path
from monkey_island.cc.environment.data_dir_generator import create_data_dir
from monkey_island.cc.server_utils.consts import (
DEFAULT_DATA_DIR,
DEFAULT_DEVELOP_SERVER_CONFIG_PATH,
DEFAULT_SERVER_CONFIG_PATH,
)
def create_default_server_config_file() -> str:
if not os.path.isfile(DEFAULT_SERVER_CONFIG_PATH):
create_data_dir(DEFAULT_DATA_DIR, False)
write_default_server_config_to_file(DEFAULT_SERVER_CONFIG_PATH)
return DEFAULT_SERVER_CONFIG_PATH
def write_default_server_config_to_file(path: str) -> None:
default_config = Path(DEFAULT_DEVELOP_SERVER_CONFIG_PATH).read_text()
Path(path).write_text(default_config)

View File

@ -0,0 +1,39 @@
import json
import os
from pathlib import Path
from monkey_island.cc.server_utils.consts import (
DEFAULT_DATA_DIR,
DEFAULT_DEVELOP_SERVER_CONFIG_PATH,
DEFAULT_LOG_LEVEL,
DEFAULT_SERVER_CONFIG_PATH,
)
def create_default_server_config_file() -> None:
if not os.path.isfile(DEFAULT_SERVER_CONFIG_PATH):
write_default_server_config_to_file(DEFAULT_SERVER_CONFIG_PATH)
def write_default_server_config_to_file(path: str) -> None:
default_config = Path(DEFAULT_DEVELOP_SERVER_CONFIG_PATH).read_text()
Path(path).write_text(default_config)
def load_server_config_from_file(server_config_path):
with open(server_config_path, "r") as f:
config_content = f.read()
config = json.loads(config_content)
add_default_values_to_config(config)
return config
def add_default_values_to_config(config):
config["data_dir"] = os.path.abspath(
os.path.expanduser(os.path.expandvars(config.get("data_dir", DEFAULT_DATA_DIR)))
)
config.setdefault("log_level", DEFAULT_LOG_LEVEL)
return config

View File

@ -14,6 +14,8 @@ def get_default_data_dir() -> str:
SERVER_CONFIG_FILENAME = "server_config.json" SERVER_CONFIG_FILENAME = "server_config.json"
DEFAULT_LOG_LEVEL = "INFO"
MONKEY_ISLAND_ABS_PATH = os.path.join(os.getcwd(), "monkey_island") MONKEY_ISLAND_ABS_PATH = os.path.join(os.getcwd(), "monkey_island")
DEFAULT_DATA_DIR = os.path.expandvars(get_default_data_dir()) DEFAULT_DATA_DIR = os.path.expandvars(get_default_data_dir())

View File

@ -1,25 +0,0 @@
import json
import os
from monkey_island.cc.server_utils.consts import DEFAULT_DATA_DIR
DEFAULT_LOG_LEVEL = "INFO"
def load_server_config_from_file(server_config_path):
with open(server_config_path, "r") as f:
config_content = f.read()
config = json.loads(config_content)
add_default_values_to_config(config)
return config
def add_default_values_to_config(config):
config["data_dir"] = os.path.abspath(
os.path.expanduser(os.path.expandvars(config.get("data_dir", DEFAULT_DATA_DIR)))
)
config.setdefault("log_level", DEFAULT_LOG_LEVEL)
return config

View File

@ -1,11 +1,11 @@
import os import os
from monkey_island import config_loader from monkey_island.cc.environment import server_config_handler
from monkey_island.cc.server_utils.consts import DEFAULT_DATA_DIR from monkey_island.cc.server_utils.consts import DEFAULT_DATA_DIR
def test_load_server_config_from_file(test_server_config, mock_home_env): def test_load_server_config_from_file(test_server_config, mock_home_env):
config = config_loader.load_server_config_from_file(test_server_config) config = server_config_handler.load_server_config_from_file(test_server_config)
assert config["data_dir"] == os.path.join(mock_home_env, ".monkey_island") assert config["data_dir"] == os.path.join(mock_home_env, ".monkey_island")
assert config["log_level"] == "NOTICE" assert config["log_level"] == "NOTICE"
@ -13,7 +13,7 @@ def test_load_server_config_from_file(test_server_config, mock_home_env):
def test_default_log_level(): def test_default_log_level():
test_config = {} test_config = {}
config = config_loader.add_default_values_to_config(test_config) config = server_config_handler.add_default_values_to_config(test_config)
assert "log_level" in config assert "log_level" in config
assert config["log_level"] == "INFO" assert config["log_level"] == "INFO"
@ -21,7 +21,7 @@ def test_default_log_level():
def test_default_data_dir(mock_home_env): def test_default_data_dir(mock_home_env):
test_config = {} test_config = {}
config = config_loader.add_default_values_to_config(test_config) config = server_config_handler.add_default_values_to_config(test_config)
assert "data_dir" in config assert "data_dir" in config
assert config["data_dir"] == DEFAULT_DATA_DIR assert config["data_dir"] == DEFAULT_DATA_DIR