Skip to content

Commit b82b79e

Browse files
Enable show subverb to print yamllint compliant YAML.
Signed-off-by: Leander Stephen D'Souza <leanderdsouza1234@gmail.com>
1 parent ebedd27 commit b82b79e

2 files changed

Lines changed: 31 additions & 22 deletions

File tree

colcon_mixin/subverb/show.py

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from colcon_core.plugin_system import satisfies_version
55
from colcon_mixin.mixin import get_mixins
66
from colcon_mixin.subverb import MixinSubverbExtensionPoint
7+
import yaml
78

89

910
def _get_mixin_name_completer(verb_key, mixins_by_verb):
@@ -16,6 +17,14 @@ def mixin_name_completer(prefix, **kwargs):
1617
return mixin_name_completer
1718

1819

20+
class IndentDumper(yaml.Dumper):
21+
"""Custom YAML dumper with yamllint compliant indentation."""
22+
23+
def increase_indent(self, flow=False, indentless=False):
24+
"""Override to disable indentless option."""
25+
return super(IndentDumper, self).increase_indent(flow, False)
26+
27+
1928
class ShowMixinSubverb(MixinSubverbExtensionPoint):
2029
"""Show available mixins and their mapping."""
2130

@@ -47,32 +56,30 @@ def main(self, *, context): # noqa: D102
4756
context.args.verb and
4857
tuple(context.args.verb.split('.')) not in self.mixins_by_verb
4958
):
50-
return "Passed verb name '{context.args.verb}' has no mixins" \
51-
.format_map(locals())
59+
return f"Passed verb name '{context.args.verb}' has no mixins"
60+
61+
output_data = {}
5262

5363
for verb in sorted(self.mixins_by_verb.keys()):
5464
if context.args.verb:
5565
if context.args.verb != '.'.join(verb):
5666
continue
57-
else:
58-
verb_space = ' '.join(verb)
59-
print('{verb_space}:'.format_map(locals()))
6067

68+
verb_key = '.'.join(verb)
6169
mixins = self.mixins_by_verb[verb]
62-
for mixin_name in sorted(mixins.keys()):
63-
if context.args.mixin_name:
64-
if context.args.mixin_name != mixin_name:
65-
continue
66-
if context.args.mixin_name not in mixins:
67-
return 'Passed mixin name ' \
68-
"'{context.args.mixin_name}' is not defined" \
69-
.format_map(locals())
70-
71-
else:
72-
print('- {mixin_name}'.format_map(locals()))
73-
mixin_value = mixins[mixin_name]
74-
for arg_key, arg_value in mixin_value.items():
75-
indent = ' ' if context.args.mixin_name is None else ''
76-
print(
77-
'{indent}{arg_key}: {arg_value}'
78-
.format_map(locals()))
70+
71+
# Filter mixins if specific mixin name is requested
72+
if context.args.mixin_name:
73+
if context.args.mixin_name not in mixins:
74+
return (
75+
f'Passed mixin name "{context.args.mixin_name}"'
76+
' is not defined'
77+
)
78+
output_data[verb_key] = {
79+
context.args.mixin_name: mixins[context.args.mixin_name]
80+
}
81+
else:
82+
output_data[verb_key] = mixins
83+
84+
print(yaml.dump(output_data, Dumper=IndentDumper,
85+
sort_keys=True, explicit_start=True), end='')

test/spell_check.words

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ blocklist
77
colcon
88
completers
99
defaultdict
10+
indentless
1011
iterdir
1112
linter
1213
mixins
@@ -31,3 +32,4 @@ urllib
3132
urlopen
3233
urls
3334
yaml
35+
yamllint

0 commit comments

Comments
 (0)