Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spaces at start/end of a sequence #113

Open
dga-nagra opened this issue Apr 30, 2024 · 0 comments
Open

Spaces at start/end of a sequence #113

dga-nagra opened this issue Apr 30, 2024 · 0 comments

Comments

@dga-nagra
Copy link

Issue Description

Dumping a flow sequence does not allow much control over the spacing.

I can for example generate this sequence:

[item1, item2, item3]

but this is not compliant with a possible yamllint configuration like this one:

...
  brackets:
    min-spaces-inside: 1
    max-spaces-inside: 1
    min-spaces-inside-empty: 0
    max-spaces-inside-empty: 0

We need to be able to generate the following

[ item1, item2, item3 ]

Workaround

I am currently inheriting the Emitter class and injecting it in my YAML instance

import ruamel.yaml as yaml
from ruamel.yaml.emitter import Emitter as BaseEmitter
from ruamel.yaml.events import SequenceEndEvent

# This inheritance is made in order to add leading and trealing spaces in sequences
# [item] -> [ item ]
class Emitter(BaseEmitter):
    def expect_first_flow_sequence_item(self) -> None:
        if isinstance(self.event, SequenceEndEvent):
            self.indent = self.indents.pop()
            popped = self.flow_context.pop()
            assert popped == "["  # noqa
            self.write_indicator(self.flow_seq_end, True)  # EDITED  <-----------------------------------------------
            if self.event.comment and self.event.comment[0]:
                # eol comment on empty flow sequence
                self.write_post_comment(self.event)
            elif self.flow_level == 0:
                self.write_line_break()
            self.state = self.states.pop()
        else:
            self.write_indicator("", True)  # ADDED  <-----------------------------------------------
            if self.canonical or self.column > self.best_width:
                self.write_indent()
            self.states.append(self.expect_flow_sequence_item)
            self.expect_node(sequence=True)

    def expect_flow_sequence_item(self) -> None:
        if isinstance(self.event, SequenceEndEvent):
            self.indent = self.indents.pop()
            popped = self.flow_context.pop()
            assert popped == "["  # noqa
            if self.canonical:
                # ToDo: so-39595807, maybe add a space to the flow_seq_separator
                # and strip the last space, if space then indent, else do not
                # not sure that [1,2,3] is a valid YAML seq
                self.write_indicator(self.flow_seq_separator, False)
                self.write_indent()
            self.write_indicator(self.flow_seq_end, True)  # EDITED  <-----------------------------------------------
            if self.event.comment and self.event.comment[0]:
                # eol comment on flow sequence
                self.write_post_comment(self.event)
            else:
                self.no_newline = False
            self.state = self.states.pop()
        else:
            self.write_indicator(self.flow_seq_separator, False)
            if self.canonical or self.column > self.best_width:
                self.write_indent()
            self.states.append(self.expect_flow_sequence_item)
            self.expect_node(sequence=True)


ruamel= yaml.YAML(typ=typ)
ruamel.Emitter = Emitter

Modification proposal

As we can see, the correction to do is very small. I would like to add an option to control the number of spaces at the start/end of the sequence that would only be used here. I can make the PR but I want to confirm that someone will be checking, hopefully approving it, and that the names chosen will be validated.

Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant