Skip to content

Commit

Permalink
remove code duplication and format components info command
Browse files Browse the repository at this point in the history
  • Loading branch information
mirpedrol committed Sep 27, 2024
1 parent a2ffa44 commit 8d5f898
Showing 1 changed file with 26 additions and 17 deletions.
43 changes: 26 additions & 17 deletions nf_core/components/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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():
Expand All @@ -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(
Expand All @@ -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)
Expand Down

0 comments on commit 8d5f898

Please sign in to comment.