|
| 1 | +from xml.etree import ElementTree |
| 2 | + |
| 3 | +import click |
| 4 | + |
| 5 | +from planemo.cli import pass_context |
| 6 | +from planemo import options |
| 7 | + |
| 8 | +from galaxy.tools.loader import ( |
| 9 | + load_tool, |
| 10 | + raw_tool_xml_tree, |
| 11 | +) |
| 12 | + |
| 13 | + |
| 14 | +TAG_ORDER = [ |
| 15 | + 'description', |
| 16 | + 'macros', |
| 17 | + 'requirements', |
| 18 | + 'code', |
| 19 | + 'stdio', |
| 20 | + 'version_command', |
| 21 | + 'command', |
| 22 | + 'inputs', |
| 23 | + 'outputs', |
| 24 | + 'tests', |
| 25 | + 'help', |
| 26 | + 'citations', |
| 27 | +] |
| 28 | + |
| 29 | + |
| 30 | +@click.command('normalize') |
| 31 | +@options.required_tool_arg() |
| 32 | +@click.option( |
| 33 | + "--expand_macros", |
| 34 | + is_flag=True, |
| 35 | + help=("Expand macros while normalizing tool XML - useful to see how " |
| 36 | + "macros are evaluated.") |
| 37 | +) |
| 38 | +@click.option( |
| 39 | + "--skip_reorder", |
| 40 | + is_flag=True, |
| 41 | + help=("Planemo will reorder top-level tool blocks according to tool " |
| 42 | + "development best practices as part of this command, this flag " |
| 43 | + "will disable that behavior.") |
| 44 | +) |
| 45 | +@click.option( |
| 46 | + "--skip_reindent", |
| 47 | + is_flag=True, |
| 48 | + help=("Planemo will reindent the XML according to tool development " |
| 49 | + "best practices as part of this command, this flag will disable " |
| 50 | + "that behavior.") |
| 51 | +) |
| 52 | +@pass_context |
| 53 | +def cli(ctx, path, expand_macros=False, **kwds): |
| 54 | + """Generate normalized tool XML from input. |
| 55 | +
|
| 56 | + The top-level blocks will be reordered and whitespace fixed according to |
| 57 | + the tool development best practices outlined on the Galaxy wiki. |
| 58 | +
|
| 59 | + See also https://wiki.galaxyproject.org/Tools/BestPractices. |
| 60 | +
|
| 61 | + :: |
| 62 | +
|
| 63 | + % # Print normalized version of tool. |
| 64 | + % planemo normalize tool.xml |
| 65 | + <tool> |
| 66 | + ... |
| 67 | + % # Print a variant of tool with all macros expanded out, useful for |
| 68 | + % # debugging complex macros. |
| 69 | + % planemo normalize --expand_macros tool.xml |
| 70 | + <tool> |
| 71 | + ... |
| 72 | + """ |
| 73 | + if expand_macros: |
| 74 | + tree = load_tool(path) |
| 75 | + else: |
| 76 | + tree = raw_tool_xml_tree(path) |
| 77 | + |
| 78 | + root = tree.getroot() |
| 79 | + if not kwds.get("skip_reorder", False): |
| 80 | + last_index = len(TAG_ORDER) |
| 81 | + data = [] |
| 82 | + for elem in root: |
| 83 | + tag = elem.tag |
| 84 | + if tag in TAG_ORDER: |
| 85 | + key = TAG_ORDER.index(tag) |
| 86 | + else: |
| 87 | + key = last_index |
| 88 | + last_index += 1 |
| 89 | + data.append((key, elem)) |
| 90 | + data.sort() |
| 91 | + root[:] = [item[-1] for item in data] |
| 92 | + if not kwds.get("skip_reindent", False): |
| 93 | + _indent(root) |
| 94 | + ElementTree.dump(root) |
| 95 | + |
| 96 | + |
| 97 | +def _indent(elem, level=0): |
| 98 | + # http://stackoverflow.com/questions/749796/pretty-printing-xml-in-python |
| 99 | + i = "\n" + level * " " |
| 100 | + if len(elem): |
| 101 | + if not elem.text or not elem.text.strip(): |
| 102 | + elem.text = i + " " |
| 103 | + if not elem.tail or not elem.tail.strip(): |
| 104 | + elem.tail = i |
| 105 | + for elem in elem: |
| 106 | + _indent(elem, level + 1) |
| 107 | + if not elem.tail or not elem.tail.strip(): |
| 108 | + elem.tail = i |
| 109 | + else: |
| 110 | + if level and (not elem.tail or not elem.tail.strip()): |
| 111 | + elem.tail = i |
0 commit comments