Island: improve readability in config_setup.py by renaming methods and adding default parameter to IslandConfigOptions

This commit is contained in:
VakarisZ 2021-11-29 14:47:37 +02:00
parent 811983a8b5
commit 7e479ec3df
2 changed files with 10 additions and 8 deletions

View File

@ -11,29 +11,29 @@ logger = getLogger(__name__)
def get_server_config(island_args: IslandCmdArgs) -> IslandConfigOptions:
config = IslandConfigOptions({})
config = IslandConfigOptions()
update_config_from_file(config, PACKAGE_CONFIG_PATH)
_update_config_from_file(config, PACKAGE_CONFIG_PATH)
update_config_from_file(config, USER_CONFIG_PATH)
_update_config_from_file(config, USER_CONFIG_PATH)
if island_args.server_config_path:
path_to_config = expand_path(island_args.server_config_path)
update_config_from_file(config, path_to_config)
_update_config_from_file(config, path_to_config)
return config
def update_config_from_file(config: IslandConfigOptions, config_path: Path):
def _update_config_from_file(config: IslandConfigOptions, config_path: Path):
try:
config_from_file = load_server_config_from_file(config_path)
config_from_file = _load_server_config_from_file(config_path)
config.update(config_from_file)
logger.info(f"Server config updated from {config_path}")
except OSError:
logger.info(f"Server config not found in path {config_path}")
def load_server_config_from_file(server_config_path) -> IslandConfigOptions:
def _load_server_config_from_file(server_config_path) -> IslandConfigOptions:
with open(server_config_path, "r") as f:
config_content = f.read()
config = json.loads(config_content)

View File

@ -12,7 +12,9 @@ from monkey_island.cc.server_utils.consts import (
class IslandConfigOptions:
def __init__(self, config_contents: dict):
def __init__(self, config_contents: dict = None):
if not config_contents:
config_contents = {}
self.data_dir = expand_path(config_contents.get("data_dir", DEFAULT_DATA_DIR))
self.log_level = config_contents.get("log_level", DEFAULT_LOG_LEVEL)