From b82b79e2425dd966d1f55682166f7790ae45aea6 Mon Sep 17 00:00:00 2001 From: Leander Stephen D'Souza Date: Wed, 26 Nov 2025 12:58:32 +0000 Subject: [PATCH] Enable show subverb to print yamllint compliant YAML. Signed-off-by: Leander Stephen D'Souza --- colcon_mixin/subverb/show.py | 51 ++++++++++++++++++++---------------- test/spell_check.words | 2 ++ 2 files changed, 31 insertions(+), 22 deletions(-) diff --git a/colcon_mixin/subverb/show.py b/colcon_mixin/subverb/show.py index 4531b86..1c312d7 100644 --- a/colcon_mixin/subverb/show.py +++ b/colcon_mixin/subverb/show.py @@ -4,6 +4,7 @@ from colcon_core.plugin_system import satisfies_version from colcon_mixin.mixin import get_mixins from colcon_mixin.subverb import MixinSubverbExtensionPoint +import yaml def _get_mixin_name_completer(verb_key, mixins_by_verb): @@ -16,6 +17,14 @@ def mixin_name_completer(prefix, **kwargs): return mixin_name_completer +class IndentDumper(yaml.Dumper): + """Custom YAML dumper with yamllint compliant indentation.""" + + def increase_indent(self, flow=False, indentless=False): + """Override to disable indentless option.""" + return super(IndentDumper, self).increase_indent(flow, False) + + class ShowMixinSubverb(MixinSubverbExtensionPoint): """Show available mixins and their mapping.""" @@ -47,32 +56,30 @@ def main(self, *, context): # noqa: D102 context.args.verb and tuple(context.args.verb.split('.')) not in self.mixins_by_verb ): - return "Passed verb name '{context.args.verb}' has no mixins" \ - .format_map(locals()) + return f"Passed verb name '{context.args.verb}' has no mixins" + + output_data = {} for verb in sorted(self.mixins_by_verb.keys()): if context.args.verb: if context.args.verb != '.'.join(verb): continue - else: - verb_space = ' '.join(verb) - print('{verb_space}:'.format_map(locals())) + verb_key = '.'.join(verb) mixins = self.mixins_by_verb[verb] - for mixin_name in sorted(mixins.keys()): - if context.args.mixin_name: - if context.args.mixin_name != mixin_name: - continue - if context.args.mixin_name not in mixins: - return 'Passed mixin name ' \ - "'{context.args.mixin_name}' is not defined" \ - .format_map(locals()) - - else: - print('- {mixin_name}'.format_map(locals())) - mixin_value = mixins[mixin_name] - for arg_key, arg_value in mixin_value.items(): - indent = ' ' if context.args.mixin_name is None else '' - print( - '{indent}{arg_key}: {arg_value}' - .format_map(locals())) + + # Filter mixins if specific mixin name is requested + if context.args.mixin_name: + if context.args.mixin_name not in mixins: + return ( + f'Passed mixin name "{context.args.mixin_name}"' + ' is not defined' + ) + output_data[verb_key] = { + context.args.mixin_name: mixins[context.args.mixin_name] + } + else: + output_data[verb_key] = mixins + + print(yaml.dump(output_data, Dumper=IndentDumper, + sort_keys=True, explicit_start=True), end='') diff --git a/test/spell_check.words b/test/spell_check.words index 6ee44ca..d376190 100644 --- a/test/spell_check.words +++ b/test/spell_check.words @@ -7,6 +7,7 @@ blocklist colcon completers defaultdict +indentless iterdir linter mixins @@ -31,3 +32,4 @@ urllib urlopen urls yaml +yamllint