comparison env/lib/python3.9/site-packages/galaxy/tool_util/verify/asserts/__init__.py @ 0:4f3585e2f14b draft default tip

"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author shellac
date Mon, 22 Mar 2021 18:12:50 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4f3585e2f14b
1 import logging
2 import sys
3
4 from galaxy.util import unicodify
5 from galaxy.util.getargspec import getfullargspec
6
7 log = logging.getLogger(__name__)
8
9 assertion_module_names = ['text', 'tabular', 'xml', 'hdf5', 'archive', 'size']
10
11 # Code for loading modules containing assertion checking functions, to
12 # create a new module of assertion functions, create the needed python
13 # source file "test/base/asserts/<MODULE_NAME>.py" and add
14 # <MODULE_NAME> to the list of assertion module names defined above.
15 assertion_modules = []
16 for assertion_module_name in assertion_module_names:
17 full_assertion_module_name = 'galaxy.tool_util.verify.asserts.' + assertion_module_name
18 try:
19 # Dynamically import module
20 __import__(full_assertion_module_name)
21 assertion_module = sys.modules[full_assertion_module_name]
22 assertion_modules.append(assertion_module)
23 except Exception:
24 log.exception('Failed to load assertion module: %s', assertion_module_name)
25
26
27 def verify_assertions(data, assertion_description_list):
28 """ This function takes a list of assertions and a string to check
29 these assertions against. """
30 for assertion_description in assertion_description_list:
31 verify_assertion(data, assertion_description)
32
33
34 def verify_assertion(data, assertion_description):
35 tag = assertion_description["tag"]
36 assert_function_name = "assert_" + tag
37 assert_function = None
38 for assertion_module in assertion_modules:
39 if hasattr(assertion_module, assert_function_name):
40 assert_function = getattr(assertion_module, assert_function_name)
41
42 if assert_function is None:
43 errmsg = "Unable to find test function associated with XML tag '%s'. Check your tool file syntax." % tag
44 raise AssertionError(errmsg)
45
46 assert_function_args = getfullargspec(assert_function).args
47 args = {}
48 for attribute, value in assertion_description["attributes"].items():
49 if attribute in assert_function_args:
50 args[attribute] = value
51
52 # Three special arguments automatically populated independently of
53 # tool XML attributes. output is passed in as the contents of the
54 # output file. verify_assertions_function is passed in as the
55 # verify_assertions function defined above, this allows
56 # recursively checking assertions on subsections of
57 # output. children is the parsed version of the child elements of
58 # the XML element describing this assertion. See
59 # assert_element_text in test/base/asserts/xml.py as an example of
60 # how to use verify_assertions_function and children in conjuction
61 # to apply assertion checking to a subset of the input. The parsed
62 # version of an elements child elements do not need to just define
63 # assertions, developers of assertion functions can also use the
64 # child elements in novel ways to define inputs the assertion
65 # checking function (for instance consider the following fictional
66 # assertion function for checking column titles of tabular output
67 # - <has_column_titles><with_name name="sequence"><with_name
68 # name="probability"></has_column_titles>.)
69 if "output" in assert_function_args:
70 # If the assert_function will have an attribute called "output"
71 # the data passed from the test to the function will be unicodified.
72 # This is because most of the assert functions are working on pure
73 # text files.
74 args["output"] = unicodify(data)
75 if "output_bytes" in assert_function_args:
76 # This will read in data as bytes and will not change it prior passing
77 # it to the assert_function
78 args["output_bytes"] = data
79
80 if "verify_assertions_function" in assert_function_args:
81 args["verify_assertions_function"] = verify_assertions
82
83 if "children" in assert_function_args:
84 args["children"] = assertion_description["children"]
85
86 # TODO: Verify all needed function arguments are specified.
87 assert_function(**args)