Skip to content

Commit

Permalink
fix: make nice yaml formatter optional
Browse files Browse the repository at this point in the history
Signed-off-by: gardar <[email protected]>
  • Loading branch information
gardar committed May 23, 2024
1 parent 2719e87 commit decee7e
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 5 deletions.
1 change: 1 addition & 0 deletions changelogs/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ title: Ansible Changelog Tool
output_formats:
- rst
- md
changelog_nice_yaml: false
6 changes: 6 additions & 0 deletions changelogs/fragments/160-changelog-nice-yaml.yml
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions docs/changelog-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/antsibull_changelog/changes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions src/antsibull_changelog/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
12 changes: 8 additions & 4 deletions src/antsibull_changelog/yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
"""
Expand All @@ -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``.
"""
Expand All @@ -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
)

0 comments on commit decee7e

Please sign in to comment.