From 8d5f898f3114d1b029963af7ed60590b56288175 Mon Sep 17 00:00:00 2001 From: mirpedrol Date: Fri, 27 Sep 2024 11:51:12 +0200 Subject: [PATCH] remove code duplication and format components info command --- nf_core/components/info.py | 43 +++++++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/nf_core/components/info.py b/nf_core/components/info.py index e4a58d1618..f3e5bf617c 100644 --- a/nf_core/components/info.py +++ b/nf_core/components/info.py @@ -229,6 +229,25 @@ def get_remote_yaml(self) -> Optional[dict]: self.remote_location = self.modules_repo.remote_url return yaml.safe_load(file_contents) + def generate_params_table(self, type) -> Table: + "Generate a rich table for inputs and outputs" + table = Table(expand=True, show_lines=True, box=box.MINIMAL_HEAVY_HEAD, padding=0) + table.add_column(f":inbox_tray: {type}") + table.add_column("Description") + if self.component_type == "modules": + table.add_column("Pattern", justify="right", style="green") + elif self.component_type == "subworkflows": + table.add_column("Structure", justify="right", style="green") + return table + + def get_channel_structure(self, structure: dict) -> str: + "Get the structure of a channel" + structure_str = "" + for key, info in structure.items(): + pattern = f" - {info['pattern']}" if info.get("pattern") else "" + structure_str += f"{key} ({info['type']}{pattern})" + return structure_str + def generate_component_info_help(self): """Take the parsed meta.yml and generate rich help. @@ -277,14 +296,9 @@ def generate_component_info_help(self): # Inputs if self.meta.get("input"): - inputs_table = Table(expand=True, show_lines=True, box=box.MINIMAL_HEAVY_HEAD, padding=0) - inputs_table.add_column(":inbox_tray: Inputs") - inputs_table.add_column("Description") - if self.component_type == "modules": - inputs_table.add_column("Pattern", justify="right", style="green") - elif self.component_type == "subworkflows": - inputs_table.add_column("Structure", justify="right", style="green") - for input in self.meta["input"]: + inputs_table = self.generate_params_table("Inputs") + for i, input in enumerate(self.meta["input"]): + inputs_table.add_row(f"[italic]input[{i}][/]", "", "") if self.component_type == "modules": for element in input: for key, info in element.items(): @@ -298,23 +312,18 @@ def generate_component_info_help(self): inputs_table.add_row( f"[orange1 on black] {key} [/][dim i]", Markdown(info["description"] if info["description"] else ""), - info.get("structure", ""), + self.get_channel_structure(info["structure"]) if info.get("structure") else "", ) renderables.append(inputs_table) # Outputs if self.meta.get("output"): - outputs_table = Table(expand=True, show_lines=True, box=box.MINIMAL_HEAVY_HEAD, padding=0) - outputs_table.add_column(":outbox_tray: Outputs") - outputs_table.add_column("Description") - if self.component_type == "modules": - inputs_table.add_column("Pattern", justify="right", style="green") - elif self.component_type == "subworkflows": - inputs_table.add_column("Structure", justify="right", style="green") + outputs_table = self.generate_params_table("Outputs") for output in self.meta["output"]: if self.component_type == "modules": for ch_name, elements in output.items(): + outputs_table.add_row(f"{ch_name}", "", "") for element in elements: for key, info in element.items(): outputs_table.add_row( @@ -327,7 +336,7 @@ def generate_component_info_help(self): outputs_table.add_row( f"[orange1 on black] {key} [/][dim i]", Markdown(info["description"] if info["description"] else ""), - info.get("structure", ""), + self.get_channel_structure(info["structure"]) if info.get("structure") else "", ) renderables.append(outputs_table)