diff --git a/CodeEntropy/config/arg_config_manager.py b/CodeEntropy/config/arg_config_manager.py index 5f62832..b8682e5 100644 --- a/CodeEntropy/config/arg_config_manager.py +++ b/CodeEntropy/config/arg_config_manager.py @@ -1,4 +1,5 @@ import argparse +import glob import logging import os @@ -77,16 +78,26 @@ def __init__(self): self.arg_map = arg_map def load_config(self, file_path): - """Load YAML configuration file.""" - if not os.path.exists(file_path): - raise FileNotFoundError(f"Configuration file '{file_path}' not found.") + """Load YAML configuration file from the given directory.""" + yaml_files = glob.glob(os.path.join(file_path, "*.yaml")) - with open(file_path, "r") as file: - config = yaml.safe_load(file) - - # If YAML content is empty, return an empty dictionary - if config is None: - config = {} + if not yaml_files: + logger.warning( + f"No YAML configuration files found in directory: {file_path}. " + "Expected a file with extension '.yaml'. " + "Proceeding with default configuration: {'run1': {}}." + ) + return {"run1": {}} + + try: + with open(yaml_files[0], "r") as file: + config = yaml.safe_load(file) + logger.info(f"Loaded configuration from: {yaml_files[0]}") + if config is None: + config = {"run1": {}} + except Exception as e: + logger.error(f"Failed to load config file: {e}") + config = {"run1": {}} return config diff --git a/CodeEntropy/run.py b/CodeEntropy/run.py index f90f56d..77ca20e 100644 --- a/CodeEntropy/run.py +++ b/CodeEntropy/run.py @@ -95,12 +95,9 @@ def run_entropy_workflow(self): try: logger = self._logging_config.setup_logging() - config = self._config_manager.load_config("config.yaml") - if config is None: - raise ValueError( - "No configuration file found, and no CLI arguments were provided." - ) + current_directory = os.getcwd() + config = self._config_manager.load_config(current_directory) parser = self._config_manager.setup_argparse() args, _ = parser.parse_known_args() args.output_file = os.path.join(self.folder, args.output_file) diff --git a/config.yaml b/config.yaml deleted file mode 100644 index f76edf8..0000000 --- a/config.yaml +++ /dev/null @@ -1,15 +0,0 @@ ---- - -run1: - top_traj_file: - selection_string: - start: - end: - step: - bin_width: - temperature: - verbose: - thread: - output_file: - force_partitioning: - water_entropy: \ No newline at end of file diff --git a/tests/test_CodeEntropy/test_arg_config_manager.py b/tests/test_CodeEntropy/test_arg_config_manager.py index 664116f..6aa15a8 100644 --- a/tests/test_CodeEntropy/test_arg_config_manager.py +++ b/tests/test_CodeEntropy/test_arg_config_manager.py @@ -68,28 +68,50 @@ def setup_file(self, mock_file): "water_entropy: False" ).return_value - @patch("builtins.open", new_callable=mock_open) - @patch("os.path.exists", return_value=True) - def test_load_config(self, mock_exists, mock_file): + @patch("builtins.open") + @patch("glob.glob", return_value=["config.yaml"]) + def test_load_config(self, mock_glob, mock_file): """ Test loading a valid configuration file. """ - arg_config = ConfigManager() + # Setup the mock file content self.setup_file(mock_file) - config = arg_config.load_config(self.config_file) + + arg_config = ConfigManager() + config = arg_config.load_config("/some/path") + self.assertIn("run1", config) self.assertEqual( config["run1"]["top_traj_file"], ["/path/to/tpr", "/path/to/trr"] ) + self.assertEqual(config["run1"]["selection_string"], "all") + self.assertEqual(config["run1"]["start"], 0) + self.assertEqual(config["run1"]["end"], -1) + self.assertEqual(config["run1"]["step"], 1) + self.assertEqual(config["run1"]["bin_width"], 30) + self.assertEqual(config["run1"]["tempra"], 298.0) + self.assertFalse(config["run1"]["verbose"]) + self.assertEqual(config["run1"]["thread"], 1) + self.assertEqual(config["run1"]["output_file"], "output_file.json") + self.assertEqual(config["run1"]["force_partitioning"], 0.5) + self.assertFalse(config["run1"]["water_entropy"]) + + @patch("glob.glob", return_value=[]) + def test_load_config_no_yaml_files(self, mock_glob): + arg_config = ConfigManager() + config = arg_config.load_config("/some/path") + self.assertEqual(config, {"run1": {}}) @patch("builtins.open", side_effect=FileNotFoundError) - def test_load_config_file_not_found(self, mock_file): + @patch("glob.glob", return_value=["config.yaml"]) + def test_load_config_file_not_found(self, mock_glob, mock_open): """ - Test loading a configuration file that does not exist. + Test loading a configuration file that exists but cannot be opened. + Should return default config instead of raising an error. """ arg_config = ConfigManager() - with self.assertRaises(FileNotFoundError): - arg_config.load_config(self.config_file) + config = arg_config.load_config("/some/path") + self.assertEqual(config, {"run1": {}}) @patch.object(ConfigManager, "load_config", return_value=None) def test_no_cli_no_yaml(self, mock_load_config): @@ -480,19 +502,17 @@ def test_edge_case_argument_values(self, mock_args): self.assertEqual(args.end, -10) @patch("builtins.open", new_callable=mock_open, read_data="--- \n") - @patch("os.path.exists", return_value=True) - def test_empty_yaml_config(self, mock_exists, mock_file): + @patch("glob.glob", return_value=["config.yaml"]) + def test_empty_yaml_config(self, mock_glob, mock_file): """ Test behavior when an empty YAML file is provided. - Should use defaults or raise an appropriate error. + Should return default config {'run1': {}}. """ - arg_config = ConfigManager() - - config = arg_config.load_config(self.config_file) + config = arg_config.load_config("/some/path") self.assertIsInstance(config, dict) - self.assertEqual(config, {}) + self.assertEqual(config, {"run1": {}}) def test_input_parameters_validation_all_valid(self): """Test that input_parameters_validation passes with all valid inputs."""