Skip to content

Commit

Permalink
Fix linting (#161)
Browse files Browse the repository at this point in the history
* Add docstrings

* Fix linting

* Black/isort formatting

* Type annotate functions

* More type annotations

* Fix line break
  • Loading branch information
neoformit authored Aug 12, 2024
1 parent 95af7c7 commit 571c231
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 39 deletions.
36 changes: 19 additions & 17 deletions subdomains/scripts/parse_tools_to_produce_yml_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,47 @@
installation.
"""

import yaml
import argparse
import os
import re
import argparse

import tools_au
import yaml

parser = argparse.ArgumentParser(
description='Create installation yaml files from a Yaml file containing'
' the panel_view/Toolkit data')
parser.add_argument('--tk', action="store", dest='toolkit',
help='Path of the toolkit.yaml file')
parser.add_argument('--yml-folder', action="store", dest='dest',
help='Folder containing the yml files with tool installed'
' in the target instance')
description="Create installation yaml files from a Yaml file containing the panel_view/Toolkit data"
)
parser.add_argument("--tk", action="store", dest="toolkit", help="Path of the toolkit.yaml file")
parser.add_argument(
"--yml-folder",
action="store",
dest="dest",
help="Folder containing the yml files with tool installed" " in the target instance",
)
args = parser.parse_args()

print(args.toolkit)
print(args.dest)

with open(args.toolkit, 'r') as stream:
with open(args.toolkit) as stream:
data_loaded = yaml.safe_load(stream)

for i in range(3, len(data_loaded['items']) - 2):
section = data_loaded['items'][i]['id']
name = data_loaded['items'][i]['name']
for i in range(3, len(data_loaded["items"]) - 2):
section = data_loaded["items"][i]["id"]
name = data_loaded["items"][i]["name"]
f = open(section + ".yml", "w")
f.write("tool_panel_section_label: " + name + "\ntools:\n")
for j in data_loaded['items'][i]['items']: # for loop on toolkit sections
path = j['id']
infos = path.replace('toolshed.g2.bx.psu.edu/repos/', "")
for j in data_loaded["items"][i]["items"]: # for loop on toolkit sections
path = j["id"]
infos = path.replace("toolshed.g2.bx.psu.edu/repos/", "")
# extract the owner (group1) and the name of the tool (group2)
pattern = re.compile(r"([^\/]+)\/([^\/]+)\/")
for match in pattern.finditer(infos): # for each tool in the seection
owner = match.group(1)
tool = match.group(2)
# verify the tool is not already installed
installed = os.popen('grep -r "' + tool + '" ' + args.dest).read()
if installed == '': # if tool not installed
if installed == "": # if tool not installed
f.write("- name: " + tool + "\n")
f.write(" owner: " + owner + "\n")
f.close()
Expand Down
54 changes: 32 additions & 22 deletions subdomains/scripts/tools_au.py
Original file line number Diff line number Diff line change
@@ -1,49 +1,59 @@
"""Convert YAML files for install on AU.
Assumes that wdir contains tool yaml files.
Assumes that wdir contains tool yaml files produced by the
parse_tools_to_produce_yml_files.py script, they should look like:
<tool_section_1>.yml
<tool_section_1>.yml.lock
<tool_section_2>.yml
<tool_section_2>.yml.lock
...
See subdomains/singlecell/tool_panel for example.
"""

import yaml
from pathlib import Path

import yaml

WDIR = Path(__file__).parent
EXCLUDE_KEYS = [
'install_repository_dependencies',
'tool_panel_section_label',
"install_repository_dependencies",
"tool_panel_section_label",
]
OUTPUT_FILE = WDIR / 'usegalaxy.org.au.yml'
OUTPUT_FILE = WDIR / "usegalaxy.org.au.yml"


def parse():
data = {'tools': []}
for f in WDIR.glob('*.yml.lock'):
if not f.name.endswith('.yml.lock'):
def parse() -> None:
"""Parse tools list from YAML files."""
data: dict = {"tools": []}
for f in WDIR.glob("*.yml.lock"):
if not f.name.endswith(".yml.lock"):
continue
print(f"Extracting data from {f}...")
with open(f, 'r') as handle:
with open(f) as handle:
section_data = yaml.safe_load(handle)
section_data['tools'] = [
_transcribe_to_au(x) for x in section_data['tools']
]
section_data["tools"] = [_transcribe_to_au(x) for x in section_data["tools"]]
for key in EXCLUDE_KEYS:
section_data.pop(key)
data['tools'] += section_data['tools']
for key in ('install_resolver_dependencies', 'install_tool_dependencies'):
data["tools"] += section_data["tools"]
for key in ("install_resolver_dependencies", "install_tool_dependencies"):
data[key] = section_data[key]
write_data(data)
print(f'Data transcribed and written to {OUTPUT_FILE}')
print(f"Data transcribed and written to {OUTPUT_FILE}")


def _transcribe_to_au(tool):
tool['tool_panel_section_label'] = 'Single-Cell'
tool.pop('tool_panel_section_id')
def _transcribe_to_au(tool: dict) -> dict:
"""Convert keys to AU installation format."""
tool["tool_panel_section_label"] = "Single-Cell"
tool.pop("tool_panel_section_id")
return tool


def write_data(data):
with open(OUTPUT_FILE, 'w') as handle:
def write_data(data: dict) -> None:
"""Write data to YAML output file."""
with open(OUTPUT_FILE, "w") as handle:
yaml.dump(data, handle, default_flow_style=False)


if __name__ == '__main__':
if __name__ == "__main__":
parse()

0 comments on commit 571c231

Please sign in to comment.