Skip to content

Commit 7f78c02

Browse files
Smedsmvdbeek
authored andcommitted
command used to list available workflows
1 parent ede0085 commit 7f78c02

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
"""Module describing the planemo ``list_workflows`` command."""
2+
3+
import json
4+
5+
import click
6+
7+
from planemo import options
8+
from planemo.cli import command_function
9+
from planemo.galaxy import profiles
10+
from planemo.galaxy.api import get_workflows
11+
from planemo.io import (
12+
error,
13+
info,
14+
)
15+
16+
try:
17+
from tabulate import tabulate
18+
except ImportError:
19+
tabulate = None # type: ignore
20+
21+
22+
def format_url(url):
23+
if url == "N/A":
24+
return ""
25+
return url
26+
27+
28+
@click.command("list_workflows")
29+
@click.option(
30+
"--raw",
31+
is_flag=True,
32+
help="output will be a json structure.",
33+
default=False,
34+
)
35+
@options.galaxy_url_option()
36+
@options.galaxy_user_key_option()
37+
@options.profile_option()
38+
@command_function
39+
def cli(ctx, raw, **kwds):
40+
"""
41+
Display available workflows.
42+
"""
43+
if not raw:
44+
info("Looking for workflows...")
45+
profile = kwds.get("profile")
46+
if profile is not None:
47+
profile = profiles.ensure_profile(ctx, profile)
48+
url = profile["galaxy_url"]
49+
key = profile["galaxy_admin_key"] or profile["galaxy_user_key"]
50+
else:
51+
url = kwds.get("galaxy_url")
52+
key = kwds.get("galaxy_admin_key") or kwds.get("galaxy_user_key")
53+
54+
workflows = get_workflows(
55+
url=url,
56+
key=key,
57+
)
58+
if tabulate is not None:
59+
if raw:
60+
print(json.dumps(workflows, indent=4, sort_keys=True))
61+
return
62+
else:
63+
print(
64+
tabulate(
65+
{
66+
"Workflow ID": workflows.keys(),
67+
"Name": [workflow["name"] for _, workflow in workflows.items()],
68+
"Url": [format_url(f"{url}/{workflow['url'].strip('/')}") for _, workflow in workflows.items()],
69+
"Repo Url": [format_url(workflow["repo_url"]) for _, workflow in workflows.items()],
70+
},
71+
headers="keys",
72+
)
73+
)
74+
else:
75+
error("The tabulate package is not installed, invocations could not be listed correctly.")
76+
print(json.dumps(workflows, indent=4, sort_keys=True))
77+
if not raw:
78+
info(f"{len(workflows)} workflows found.")
79+
return

planemo/galaxy/api.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,26 @@ def get_invocations(url, key, workflow_id):
128128
}
129129

130130

131+
def get_workflows(url, key):
132+
inv_gi = gi(None, url, key)
133+
workflows = inv_gi.workflows.get_workflows()
134+
135+
def get_report_url(workflow):
136+
if "source_metadata" in workflow and workflow["source_metadata"] and "url" in workflow["source_metadata"]:
137+
return workflow["source_metadata"]["url"]
138+
return "N/A"
139+
140+
return {
141+
workflow["id"]: {
142+
"name": workflow["name"],
143+
"repo_url": get_report_url(workflow),
144+
"url": workflow["url"],
145+
"published": workflow.get("published", False),
146+
}
147+
for workflow in workflows
148+
}
149+
150+
131151
def _format_for_summary(blob, empty_message, prefix="| "):
132152
contents = "\n".join([f"{prefix}{line.strip()}" for line in StringIO(blob).readlines() if line.rstrip("\n\r")])
133153
return contents or f"{prefix}*{empty_message}*"

0 commit comments

Comments
 (0)