From 70b9a9f6b73a0314ad34ef333b804410246a18dd Mon Sep 17 00:00:00 2001 From: VakarisZ Date: Mon, 24 May 2021 11:05:15 +0300 Subject: [PATCH] Refactored config_loader.py and server_config_handler.py into a single file, with server config write/read operations --- .../cc/environment/server_config_generator.py | 21 ---------- .../cc/environment/server_config_handler.py | 39 +++++++++++++++++++ .../monkey_island/cc/server_utils/consts.py | 2 + monkey/monkey_island/config_loader.py | 25 ------------ .../monkey_island/cc/environment/__init__.py | 0 .../test_server_config_handler.py} | 8 ++-- 6 files changed, 45 insertions(+), 50 deletions(-) delete mode 100644 monkey/monkey_island/cc/environment/server_config_generator.py create mode 100644 monkey/monkey_island/cc/environment/server_config_handler.py delete mode 100644 monkey/monkey_island/config_loader.py create mode 100644 monkey/tests/unit_tests/monkey_island/cc/environment/__init__.py rename monkey/tests/unit_tests/monkey_island/{test_config_loader.py => cc/environment/test_server_config_handler.py} (64%) diff --git a/monkey/monkey_island/cc/environment/server_config_generator.py b/monkey/monkey_island/cc/environment/server_config_generator.py deleted file mode 100644 index 17ee4a50c..000000000 --- a/monkey/monkey_island/cc/environment/server_config_generator.py +++ /dev/null @@ -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) diff --git a/monkey/monkey_island/cc/environment/server_config_handler.py b/monkey/monkey_island/cc/environment/server_config_handler.py new file mode 100644 index 000000000..35cc91f3e --- /dev/null +++ b/monkey/monkey_island/cc/environment/server_config_handler.py @@ -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 diff --git a/monkey/monkey_island/cc/server_utils/consts.py b/monkey/monkey_island/cc/server_utils/consts.py index 8b88b000b..a62bb98ab 100644 --- a/monkey/monkey_island/cc/server_utils/consts.py +++ b/monkey/monkey_island/cc/server_utils/consts.py @@ -14,6 +14,8 @@ def get_default_data_dir() -> str: SERVER_CONFIG_FILENAME = "server_config.json" +DEFAULT_LOG_LEVEL = "INFO" + MONKEY_ISLAND_ABS_PATH = os.path.join(os.getcwd(), "monkey_island") DEFAULT_DATA_DIR = os.path.expandvars(get_default_data_dir()) diff --git a/monkey/monkey_island/config_loader.py b/monkey/monkey_island/config_loader.py deleted file mode 100644 index aaa9185d7..000000000 --- a/monkey/monkey_island/config_loader.py +++ /dev/null @@ -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 diff --git a/monkey/tests/unit_tests/monkey_island/cc/environment/__init__.py b/monkey/tests/unit_tests/monkey_island/cc/environment/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/monkey/tests/unit_tests/monkey_island/test_config_loader.py b/monkey/tests/unit_tests/monkey_island/cc/environment/test_server_config_handler.py similarity index 64% rename from monkey/tests/unit_tests/monkey_island/test_config_loader.py rename to monkey/tests/unit_tests/monkey_island/cc/environment/test_server_config_handler.py index 20c330f6a..acd89d84f 100644 --- a/monkey/tests/unit_tests/monkey_island/test_config_loader.py +++ b/monkey/tests/unit_tests/monkey_island/cc/environment/test_server_config_handler.py @@ -1,11 +1,11 @@ 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 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["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(): 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 config["log_level"] == "INFO" @@ -21,7 +21,7 @@ def test_default_log_level(): def test_default_data_dir(mock_home_env): 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 config["data_dir"] == DEFAULT_DATA_DIR