view env/lib/python3.9/site-packages/galaxy/tool_util/linters/xml_order.py @ 0:4f3585e2f14b draft default tip

"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author shellac
date Mon, 22 Mar 2021 18:12:50 +0000
parents
children
line wrap: on
line source

"""This module contains a linting functions for tool XML block order.

For more information on the IUC standard for XML block order see -
https://github.com/galaxy-iuc/standards.
"""
# https://github.com/galaxy-iuc/standards
# https://github.com/galaxy-iuc/standards/pull/7/files
TAG_ORDER = [
    'description',
    'macros',
    'parallelism',
    'requirements',
    'code',
    'stdio',
    'version_command',
    'command',
    'environment_variables',
    'configfiles',
    'inputs',
    'outputs',
    'tests',
    'help',
    'citations',
]

DATASOURCE_TAG_ORDER = [
    'description',
    'macros',
    'command',
    'configfiles',
    'inputs',
    'request_param_translation',
    'uihints',
    'outputs',
    'options',
    'help',
    'citations',
]


# Ensure the XML blocks appear in the correct order prescribed
# by the tool author best practices.
def lint_xml_order(tool_xml, lint_ctx):
    tool_root = tool_xml.getroot()

    if tool_root.attrib.get('tool_type', '') == 'data_source':
        _validate_for_tags(tool_root, lint_ctx, DATASOURCE_TAG_ORDER)
    else:
        _validate_for_tags(tool_root, lint_ctx, TAG_ORDER)


def _validate_for_tags(root, lint_ctx, tag_ordering):
    last_tag = None
    last_key = None
    for elem in root:
        tag = elem.tag
        if tag in tag_ordering:
            key = tag_ordering.index(tag)
            if last_key:
                if last_key > key:
                    lint_ctx.warn(f"Best practice violation [{tag}] elements should come before [{last_tag}]")
            last_tag = tag
            last_key = key
        else:
            lint_ctx.info("Unknown tag [%s] encountered, this may result in a warning in the future." % tag)