Mercurial > repos > shellac > sam_consensus_v3
comparison env/lib/python3.9/site-packages/galaxy/tool_util/lint.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 """This modules contains the functions that drive the tool linting framework.""" | |
2 | |
3 import inspect | |
4 | |
5 from galaxy.tool_util.parser import get_tool_source | |
6 from galaxy.util import submodules | |
7 from galaxy.util.getargspec import getfullargspec | |
8 | |
9 | |
10 LEVEL_ALL = "all" | |
11 LEVEL_WARN = "warn" | |
12 LEVEL_ERROR = "error" | |
13 | |
14 | |
15 def lint_tool_source(tool_source, level=LEVEL_ALL, fail_level=LEVEL_WARN, extra_modules=None, skip_types=None, name=None): | |
16 extra_modules = extra_modules or [] | |
17 skip_types = skip_types or [] | |
18 lint_context = LintContext(level=level, skip_types=skip_types, object_name=name) | |
19 lint_tool_source_with(lint_context, tool_source, extra_modules) | |
20 | |
21 return not lint_context.failed(fail_level) | |
22 | |
23 | |
24 def lint_xml(tool_xml, level=LEVEL_ALL, fail_level=LEVEL_WARN, extra_modules=None, skip_types=None, name=None): | |
25 extra_modules = extra_modules or [] | |
26 skip_types = skip_types or [] | |
27 lint_context = LintContext(level=level, skip_types=skip_types, object_name=name) | |
28 lint_xml_with(lint_context, tool_xml, extra_modules) | |
29 | |
30 return not lint_context.failed(fail_level) | |
31 | |
32 | |
33 def lint_tool_source_with(lint_context, tool_source, extra_modules=None): | |
34 extra_modules = extra_modules or [] | |
35 import galaxy.tool_util.linters | |
36 tool_xml = getattr(tool_source, "xml_tree", None) | |
37 linter_modules = submodules.import_submodules(galaxy.tool_util.linters, ordered=True) | |
38 linter_modules.extend(extra_modules) | |
39 for module in linter_modules: | |
40 tool_type = tool_source.parse_tool_type() or "default" | |
41 lint_tool_types = getattr(module, "lint_tool_types", ["default"]) | |
42 if not ("*" in lint_tool_types or tool_type in lint_tool_types): | |
43 continue | |
44 | |
45 for (name, value) in inspect.getmembers(module): | |
46 if callable(value) and name.startswith("lint_"): | |
47 # Look at the first argument to the linter to decide | |
48 # if we should lint the XML description or the abstract | |
49 # tool parser object. | |
50 first_arg = getfullargspec(value).args[0] | |
51 if first_arg == "tool_xml": | |
52 if tool_xml is None: | |
53 # XML linter and non-XML tool, skip for now | |
54 continue | |
55 else: | |
56 lint_context.lint(name, value, tool_xml) | |
57 else: | |
58 lint_context.lint(name, value, tool_source) | |
59 | |
60 | |
61 def lint_xml_with(lint_context, tool_xml, extra_modules=None): | |
62 extra_modules = extra_modules or [] | |
63 tool_source = get_tool_source(xml_tree=tool_xml) | |
64 return lint_tool_source_with(lint_context, tool_source, extra_modules=extra_modules) | |
65 | |
66 | |
67 # TODO: Nothing inherently tool-y about LintContext and in fact | |
68 # it is reused for repositories in planemo. Therefore, it should probably | |
69 # be moved to galaxy.util.lint. | |
70 class LintContext: | |
71 | |
72 def __init__(self, level, skip_types=None, object_name=None): | |
73 self.skip_types = skip_types or [] | |
74 self.level = level | |
75 self.object_name = object_name | |
76 self.found_errors = False | |
77 self.found_warns = False | |
78 | |
79 def lint(self, name, lint_func, lint_target): | |
80 name = name.replace("tsts", "tests")[len("lint_"):] | |
81 if name in self.skip_types: | |
82 return | |
83 self.printed_linter_info = False | |
84 self.valid_messages = [] | |
85 self.info_messages = [] | |
86 self.warn_messages = [] | |
87 self.error_messages = [] | |
88 lint_func(lint_target, self) | |
89 # TODO: colorful emoji if in click CLI. | |
90 if self.error_messages: | |
91 status = "FAIL" | |
92 elif self.warn_messages: | |
93 status = "WARNING" | |
94 else: | |
95 status = "CHECK" | |
96 | |
97 def print_linter_info(): | |
98 if self.printed_linter_info: | |
99 return | |
100 self.printed_linter_info = True | |
101 print(f"Applying linter {name}... {status}") | |
102 | |
103 for message in self.error_messages: | |
104 self.found_errors = True | |
105 print_linter_info() | |
106 print(".. ERROR: %s" % message) | |
107 | |
108 if self.level != LEVEL_ERROR: | |
109 for message in self.warn_messages: | |
110 self.found_warns = True | |
111 print_linter_info() | |
112 print(".. WARNING: %s" % message) | |
113 | |
114 if self.level == LEVEL_ALL: | |
115 for message in self.info_messages: | |
116 print_linter_info() | |
117 print(".. INFO: %s" % message) | |
118 for message in self.valid_messages: | |
119 print_linter_info() | |
120 print(".. CHECK: %s" % message) | |
121 | |
122 def __handle_message(self, message_list, message, *args): | |
123 if args: | |
124 message = message % args | |
125 message_list.append(message) | |
126 | |
127 def valid(self, message, *args): | |
128 self.__handle_message(self.valid_messages, message, *args) | |
129 | |
130 def info(self, message, *args): | |
131 self.__handle_message(self.info_messages, message, *args) | |
132 | |
133 def error(self, message, *args): | |
134 self.__handle_message(self.error_messages, message, *args) | |
135 | |
136 def warn(self, message, *args): | |
137 self.__handle_message(self.warn_messages, message, *args) | |
138 | |
139 def failed(self, fail_level): | |
140 found_warns = self.found_warns | |
141 found_errors = self.found_errors | |
142 if fail_level == LEVEL_WARN: | |
143 lint_fail = (found_warns or found_errors) | |
144 else: | |
145 lint_fail = found_errors | |
146 return lint_fail |