Skip to content

Commit

Permalink
implement listing of linters
Browse files Browse the repository at this point in the history
using `__subclasses__`
  • Loading branch information
bernt-matthias committed Jan 17, 2024
1 parent ccced2f commit 3a98a3a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 18 deletions.
21 changes: 7 additions & 14 deletions lib/galaxy/tool_util/lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,13 @@ def name(cls) -> str:
"""
return cls.__name__

@classmethod
def list_listers(cls) -> List[str]:
"""
list the names of all linter derived from Linter
"""
return [s.__name__ for s in cls.__subclasses__()]


class LintMessage:
"""
Expand Down Expand Up @@ -375,17 +382,3 @@ def lint_xml_with(lint_context, tool_xml, extra_modules=None) -> LintContext:
extra_modules = extra_modules or []
tool_source = get_tool_source(xml_tree=tool_xml)
return lint_tool_source_with(lint_context, tool_source, extra_modules=extra_modules)


def list_linters(extra_modules: Optional[List[str]] = None) -> List[str]:
extra_modules = extra_modules or []
linter_modules = submodules.import_submodules(galaxy.tool_util.linters)
linter_modules.extend(extra_modules)
linters = list()
for module in linter_modules:
for name, value in inspect.getmembers(module):
if callable(value) and name.startswith("lint_"):
linters.append(name[5:])
elif inspect.isclass(value) and issubclass(value, Linter) and not inspect.isabstract(value):
linters.append(name)
return linters
10 changes: 6 additions & 4 deletions test/unit/tool_util/test_tool_linters.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
lint_tool_source_with_modules,
lint_xml_with,
LintContext,
list_linters,
Linter,
XMLLintMessageLine,
XMLLintMessageXPath,
)
Expand Down Expand Up @@ -2074,8 +2074,10 @@ def test_xml_comments_are_ignored(lint_ctx: LintContext):


def test_list_linters():
linters = list_linters()
assert len(linters) >= 129
linter_names = Linter.list_listers()
# make sure to add/remove a test for new/removed linters if this number changes
assert len(linter_names) == 129
assert "Linter" not in linter_names
# make sure that linters from all modules are available
for prefix in [
"Citations",
Expand All @@ -2090,4 +2092,4 @@ def test_list_linters():
"XMLOrder",
"XSD",
]:
assert len([x for x in linters if x.startswith(prefix)])
assert len([x for x in linter_names if x.startswith(prefix)])

0 comments on commit 3a98a3a

Please sign in to comment.