|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +import yaml |
| 4 | +from collections import defaultdict |
| 5 | +import re |
| 6 | +import os |
| 7 | +import string |
| 8 | +import sys |
| 9 | +import argparse |
| 10 | + |
| 11 | +def slugify(value): |
| 12 | + """ |
| 13 | + Normalizes string, converts to lowercase, removes non-alpha characters, |
| 14 | + and converts spaces to hyphens. |
| 15 | + """ |
| 16 | + rval = '' |
| 17 | + for c in value: |
| 18 | + rval += (c if c in string.ascii_letters + string.digits else '_').lower() |
| 19 | + return rval |
| 20 | + |
| 21 | +def strip_superflous(cat): |
| 22 | + """ |
| 23 | + Re-arranges the ephemeris returned yml format for tools to the usegalaxy-tools minimal tool yml format |
| 24 | +
|
| 25 | + i.e. Takes a list like: |
| 26 | +
|
| 27 | + - name: substitution_rates |
| 28 | + owner: devteam |
| 29 | + revisions: |
| 30 | + - d1b35bcdaacc |
| 31 | + tool_panel_section_label: Regional Variation |
| 32 | + tool_shed_url: toolshed.g2.bx.psu.edu |
| 33 | + - name: indels_3way |
| 34 | + owner: devteam |
| 35 | + revisions: |
| 36 | + - 5ad24b81dd10 |
| 37 | + tool_panel_section_label: Regional Variation |
| 38 | + tool_shed_url: toolshed.g2.bx.psu.edu |
| 39 | +
|
| 40 | + ... |
| 41 | +
|
| 42 | + and returns: |
| 43 | +
|
| 44 | + tool_panel_section_label: Regional Variation |
| 45 | + - name: substitution_rates |
| 46 | + owner: devteam |
| 47 | + - name: indels_3way |
| 48 | + owner: devteam |
| 49 | +
|
| 50 | + ... |
| 51 | + """ |
| 52 | + |
| 53 | + out = {'tool_panel_section_label': cat[0]['tool_panel_section_label']} |
| 54 | + |
| 55 | + for tool in cat: |
| 56 | + del tool['tool_panel_section_label'] |
| 57 | + del tool['tool_panel_section_id'] |
| 58 | + del tool['revisions'] |
| 59 | + if tool['tool_shed_url'] == 'toolshed.g2.bx.psu.edu': |
| 60 | + del tool['tool_shed_url'] |
| 61 | + |
| 62 | + out['tools'] = cat |
| 63 | + |
| 64 | + return out |
| 65 | + |
| 66 | + |
| 67 | +def main(): |
| 68 | + |
| 69 | + VERSION = 0.2 |
| 70 | + |
| 71 | + parser = argparse.ArgumentParser(description="Splits up a Ephemeris `get_tool_list` yml file for a Galaxy server into individual files for each Section Label.") |
| 72 | + parser.add_argument("-i", "--infile", help="The returned `get_tool_list` yml file to split.") |
| 73 | + parser.add_argument("-o", "--outdir", help="The output directory to put the split files into. Defaults to infile without the .yml.") |
| 74 | + parser.add_argument("-l", "--lockfiles", action='store_true', help="Produce lock files instead of plain yml files.") |
| 75 | + parser.add_argument("--version", action='store_true') |
| 76 | + parser.add_argument("--verbose", action='store_true') |
| 77 | + |
| 78 | + args = parser.parse_args() |
| 79 | + |
| 80 | + if args.version: |
| 81 | + print("split_tool_yml.py version: %.1f" % VERSION) |
| 82 | + return |
| 83 | + |
| 84 | + filename = args.infile |
| 85 | + |
| 86 | + a = yaml.safe_load(open(filename, 'r')) |
| 87 | + outdir = re.sub('\.yml','',filename) |
| 88 | + if args.outdir: |
| 89 | + outdir = args.outdir |
| 90 | + |
| 91 | + if args.verbose: |
| 92 | + print('Outdir: %s' % outdir) |
| 93 | + if not os.path.isdir(outdir): |
| 94 | + os.mkdir(outdir) |
| 95 | + |
| 96 | + tools = a['tools'] |
| 97 | + categories = defaultdict(list) |
| 98 | + |
| 99 | + for tool in tools: |
| 100 | + categories[tool['tool_panel_section_label']].append(tool) |
| 101 | + |
| 102 | + for cat in categories: |
| 103 | + fname = str(cat) |
| 104 | + good_fname = outdir + "/" + slugify(fname) + ".yml" |
| 105 | + if args.lockfiles: |
| 106 | + good_fname += ".lock" |
| 107 | + tool_yaml = {'tools': categories[cat]} |
| 108 | + else: |
| 109 | + tool_yaml = strip_superflous(categories[cat]) |
| 110 | + if args.verbose: |
| 111 | + print("Working on: %s" % good_fname) |
| 112 | + with open(good_fname, 'w') as outfile: |
| 113 | + yaml.dump(tool_yaml, outfile, default_flow_style=False) |
| 114 | + |
| 115 | + return |
| 116 | + |
| 117 | +if __name__ == "__main__": main() |
0 commit comments