From 993c3c8a82590fd68c0c78961d435bebe56cea0e Mon Sep 17 00:00:00 2001 From: Javier Vegas-Regidor Date: Fri, 12 Mar 2021 13:02:24 +0100 Subject: [PATCH] Add nested alias support --- earthdiagnostics/config.py | 47 +++++++++++++++++++++++--------------- test/unit/test_config.py | 8 +++++++ 2 files changed, 36 insertions(+), 19 deletions(-) diff --git a/earthdiagnostics/config.py b/earthdiagnostics/config.py index 8d373efa..cb21d9b6 100644 --- a/earthdiagnostics/config.py +++ b/earthdiagnostics/config.py @@ -213,9 +213,11 @@ class Config(object): ) Log.debug("Preparing command list") commands = self._diags.split() - self._real_commands = list() - self._apply_aliases(commands) - Log.debug("Command list ready ") + while self._apply_aliases(commands): + pass + Log.debug("Command list ready: ") + for command in self._real_commands: + Log.debug(command) self.scratch_dir = os.path.join( self.scratch_dir, "diags", self.experiment.expid @@ -226,22 +228,29 @@ class Config(object): self.report = ReportConfig(parser) def _apply_aliases(self, commands): - for command in commands: - command = command.strip() - if command.startswith("#"): - break - if command.lower() in self._aliases: - added_commands = self._aliases[command.lower()] - Log.info( - "Changing alias {0} for {1}", - command, - " ".join(added_commands), - ) - for add_command in added_commands: - if add_command: - self._real_commands.append(add_command) - else: - self._real_commands.append(command) + applied_alias = True + while applied_alias: + real_commands = [] + applied_alias = False + for command in commands: + command = command.strip() + if command.startswith("#"): + break + if command.lower() in self._aliases: + added_commands = self._aliases[command.lower()] + Log.info( + "Changing alias {0} for {1}", + command, + " ".join(added_commands), + ) + for add_command in added_commands: + if add_command: + real_commands.append(add_command) + applied_alias = True + else: + real_commands.append(command) + commands = real_commands + self._real_commands = commands def _parse_dataconvention(self, parser): data_convention = parser.get_choice_option( diff --git a/test/unit/test_config.py b/test/unit/test_config.py index b8bc4cc0..6d2bbca8 100644 --- a/test/unit/test_config.py +++ b/test/unit/test_config.py @@ -752,6 +752,14 @@ class TestConfig(TestCase): self._parse(config) self.assertEqual(config.get_commands(), ["diag2"]) + def test_nested_alias(self): + """Test alias parsing""" + config = Config() + self.mock_parser.add_value("ALIAS", "nested", "diag3") + self.mock_parser.add_value("ALIAS", "diag2", "nested diag4") + self._parse(config) + self.assertEqual(config.get_commands(), ["diag1", "diag3", "diag4"]) + def test_auto_clean_ram_disk(self): """Test that USE_RAMDISK forces AUTO_CLEAN to true""" config = Config() -- GitLab