diff --git a/changelogs/config.yaml b/changelogs/config.yaml index eebf2d40..cd2f4674 100644 --- a/changelogs/config.yaml +++ b/changelogs/config.yaml @@ -36,3 +36,4 @@ title: Ansible Changelog Tool output_formats: - rst - md +changelog_nice_yaml: false diff --git a/changelogs/fragments/160-changelog-nice-yaml.yml b/changelogs/fragments/160-changelog-nice-yaml.yml new file mode 100644 index 00000000..1be9b586 --- /dev/null +++ b/changelogs/fragments/160-changelog-nice-yaml.yml @@ -0,0 +1,6 @@ +--- +minor_changes: + - | + Adds yaml boundary marker --- to the header of the file. + Increases indentation level on list items. + Makes changelog.yaml pass ansible-lint diff --git a/docs/changelog-configuration.md b/docs/changelog-configuration.md index ab4cb39b..725327df 100644 --- a/docs/changelog-configuration.md +++ b/docs/changelog-configuration.md @@ -247,6 +247,13 @@ The default is `["rst"]`. A list of output formats to write the changelog as. Supported formats are `rst` for ReStructuredText and `md` for MarkDown. +### `changelog_nice_yaml`(boolean)` + +The default is `false`. + +OWhen set to `true` the chanelog.yaml format will be written with nice yaml +format that's compatible with `ansible-lint`default rules + ## Deprecated options ### `new_plugins_after_name` (string) diff --git a/src/antsibull_changelog/changes.py b/src/antsibull_changelog/changes.py index 9e6617d5..cbb2dc5f 100644 --- a/src/antsibull_changelog/changes.py +++ b/src/antsibull_changelog/changes.py @@ -156,7 +156,7 @@ def save(self) -> None: """ self.sort() self.data["ancestor"] = self.ancestor - store_yaml(self.path, self.data) + store_yaml(self.path, self.data, self.config.changelog_nice_yaml) def add_release( self, diff --git a/src/antsibull_changelog/config.py b/src/antsibull_changelog/config.py index a42cd18c..0c577873 100644 --- a/src/antsibull_changelog/config.py +++ b/src/antsibull_changelog/config.py @@ -378,6 +378,7 @@ class ChangelogConfig: is_other_project: bool sections: Mapping[str, str] output_formats: set[TextFormat] + changelog_nice_yaml: bool def __init__( self, @@ -465,6 +466,8 @@ def __init__( f"Found unknown extension in output_formats: {exc}" ) from exc + self.changelog_nice_yaml = self.config.get("changelog_nice_yaml", False) + self._validate_config(ignore_is_other_project) def _validate_config(self, ignore_is_other_project: bool) -> None: @@ -514,6 +517,7 @@ def store(self) -> None: # noqa: C901 "trivial_section_name": self.trivial_section_name, "ignore_other_fragment_extensions": self.ignore_other_fragment_extensions, "sanitize_changelog": self.sanitize_changelog, + "changelog_nice_yaml": self.changelog_nice_yaml, } if not self.is_collection: if self.use_semantic_versioning: @@ -592,6 +596,7 @@ def default( "use_fqcn": True, "ignore_other_fragment_extensions": True, "sanitize_changelog": True, + "changelog_nice_yaml": False, } if title is not None: config["title"] = title diff --git a/src/antsibull_changelog/yaml.py b/src/antsibull_changelog/yaml.py index c86e0021..7b160ce8 100644 --- a/src/antsibull_changelog/yaml.py +++ b/src/antsibull_changelog/yaml.py @@ -14,15 +14,19 @@ import yaml + _SafeLoader: Any +_SafeDumper: Any try: # use C version if possible for speedup + from yaml import CSafeDumper as _SafeDumper from yaml import CSafeLoader as _SafeLoader except ImportError: + from yaml import SafeDumper as _SafeDumper from yaml import SafeLoader as _SafeLoader -class _SafeDumper(yaml.SafeDumper): +class _IndentedDumper(yaml.SafeDumper): """ Extend YAML dumper to increase indent of list items. """ @@ -38,7 +42,7 @@ def load_yaml(path: str) -> Any: return yaml.load(stream, Loader=_SafeLoader) -def store_yaml(path: str, content: Any) -> None: +def store_yaml(path: str, content: Any, nice: bool) -> None: """ Store ``content`` as YAML file under ``path``. """ @@ -47,6 +51,6 @@ def store_yaml(path: str, content: Any) -> None: content, stream, default_flow_style=False, - Dumper=_SafeDumper, - explicit_start=True + Dumper=_IndentedDumper if nice else _SafeDumper, + explicit_start=nice )