From bed5ae1d4ea091129d9dd09047b06d17b369eddb Mon Sep 17 00:00:00 2001 From: John Chilton Date: Mon, 6 Oct 2014 09:24:53 -0400 Subject: [PATCH] Eliminate Galaxy dependencies from lib/galaxy/tools/loader.py. Should allow for reuse in command-line tools that operate on Galaxy tools. Updates for targetting Python 2.6+. --- lib/galaxy/tools/loader.py | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/lib/galaxy/tools/loader.py b/lib/galaxy/tools/loader.py index 52a1d6d20f7..a17a3a5fc6e 100644 --- a/lib/galaxy/tools/loader.py +++ b/lib/galaxy/tools/loader.py @@ -1,10 +1,8 @@ -from __future__ import with_statement +from xml.etree import ElementTree, ElementInclude from copy import deepcopy import os -from galaxy.util import parse_xml - def load_tool(path): """ @@ -16,7 +14,7 @@ def load_tool(path): _import_macros(root, path) # Expand xml macros - macro_dict = _macros_of_type(root, 'xml', lambda el: list(el.getchildren())) + macro_dict = _macros_of_type(root, 'xml', lambda el: list(el)) _expand_macros([root], macro_dict) # Expand tokens @@ -42,7 +40,7 @@ def raw_tool_xml_tree(path): """ Load raw (no macro expansion) tree representation of tool represented at the specified path. """ - tree = parse_xml(path) + tree = _parse_xml(path) return tree @@ -54,7 +52,7 @@ def imported_macro_paths(root): def _import_macros(root, path): tool_dir = os.path.dirname(path) macros_el = _macros_el(root) - if macros_el: + if macros_el is not None: macro_els = _load_macros(macros_el, tool_dir) _xml_set_children(macros_el, macro_els) @@ -66,7 +64,7 @@ def _macros_el(root): def _macros_of_type(root, type, el_func): macros_el = root.find('macros') macro_dict = {} - if macros_el: + if macros_el is not None: macro_els = macros_el.findall('macro') macro_dict = dict([(macro_el.get("name"), el_func(macro_el)) \ for macro_el in macro_els \ @@ -88,7 +86,7 @@ def _expand_tokens(elements, tokens): new_value = _expand_tokens_str(value, tokens) if not (new_value is value): element.attrib[key] = new_value - _expand_tokens(list(element.getchildren()), tokens) + _expand_tokens(list(element), tokens) def _expand_tokens_str(str, tokens): @@ -129,7 +127,7 @@ def _expand_macro(element, expand_el, macros): def _expand_yield_statements(macro_def, expand_el): yield_els = [yield_el for macro_def_el in macro_def for yield_el in macro_def_el.findall('.//yield')] - expand_el_children = expand_el.getchildren() + expand_el_children = list(expand_el) macro_def_parent_map = \ dict((c, p) for macro_def_el in macro_def for p in macro_def_el.getiterator() for c in p) @@ -151,7 +149,7 @@ def _load_embedded_macros(macros_el, tool_dir): macro_els = [] # attribute typed macro - if macros_el: + if macros_el is not None: macro_els = macros_el.findall("macro") for macro in macro_els: if 'type' not in macro.attrib: @@ -163,7 +161,7 @@ def _load_embedded_macros(macros_el, tool_dir): typed_tag = ['template', 'xml', 'token'] for tag in typed_tag: macro_els = [] - if macros_el: + if macros_el is not None: macro_els = macros_el.findall(tag) for macro_el in macro_els: macro_el.attrib['type'] = tag @@ -188,7 +186,7 @@ def _load_imported_macros(macros_el, tool_dir): def _imported_macro_paths_from_el(macros_el): imported_macro_paths = [] macro_import_els = [] - if macros_el: + if macros_el is not None: macro_import_els = macros_el.findall("import") for macro_import_el in macro_import_els: raw_import_path = macro_import_el.text @@ -199,13 +197,13 @@ def _imported_macro_paths_from_el(macros_el): def _load_macro_file(path, tool_dir): - tree = parse_xml(path) + tree = _parse_xml(path) root = tree.getroot() return _load_macros(root, tool_dir) def _xml_set_children(element, new_children): - for old_child in element.getchildren(): + for old_child in element: element.remove(old_child) for i, new_child in enumerate(new_children): element.insert(i, new_child) @@ -216,7 +214,7 @@ def _xml_replace(query, targets, parent_map): parent_el = parent_map[query] matching_index = -1 #for index, el in enumerate(parent_el.iter('.')): ## Something like this for newer implementation - for index, el in enumerate(parent_el.getchildren()): + for index, el in enumerate(list(parent_el)): if el == query: matching_index = index break @@ -226,3 +224,10 @@ def _xml_replace(query, targets, parent_map): current_index += 1 parent_el.insert(current_index, deepcopy(target)) parent_el.remove(query) + + +def _parse_xml(fname): + tree = ElementTree.parse(fname) + root = tree.getroot() + ElementInclude.include(root) + return tree