Skip to content

Commit

Permalink
Added jaml output filter
Browse files Browse the repository at this point in the history
This format produces predicable and valid yaml without ambiguities.

[noissue]
  • Loading branch information
mdellweg committed Apr 23, 2024
1 parent af76d21 commit 3be4e75
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES/+jaml_output.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added "jaml" output. A limited "yaml" format without disambiguities.
5 changes: 5 additions & 0 deletions cookiecutter/pulp_filter_extension.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import datetime
import shlex
import typing as t

Expand All @@ -20,10 +21,14 @@ def to_nice_yaml(data: t.Any, level: int = 0, embed_in: str = "") -> str:
nl = False
if isinstance(data, str):
result = f'"{data}"'
elif data is None:
result = "null"
elif data is True:
result = "true"
elif data is False:
result = "false"
elif isinstance(data, datetime.datetime):
result = data.isoformat()
elif isinstance(data, int):
result = f"{data}"
elif isinstance(data, list):
Expand Down
64 changes: 64 additions & 0 deletions pulpcore/cli/common/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,74 @@ def _yaml_formatter(result: t.Any) -> str:
return output


def _assert_key(key: t.Any) -> str:
assert isinstance(key, str)
assert key.isidentifier()
return key


def to_nice_yaml(data: t.Any, level: int = 0, embed_in: str = "") -> str:
"""Filter render human readable YAML without disambiguities."""
# Don't even believe this is complete!
# Yes, I have checked pyyaml and ruamel.

nl = False
if isinstance(data, str):
result = f'"{data}"'
elif data is None:
result = "null"
elif data is True:
result = "true"
elif data is False:
result = "false"
elif isinstance(data, datetime.datetime):
result = data.isoformat()
elif isinstance(data, int):
result = f"{data}"
elif isinstance(data, list):
if len(data):
nl = embed_in == "dict"
result = ("\n" + " " * level).join(
("-" + to_nice_yaml(item, level + 1, "list") for item in data)
)
else:
result = "[]"
elif isinstance(data, dict):
if len(data):
nl = embed_in == "dict"
result = ("\n" + " " * level).join(
(
f"{_assert_key(key)}:" + to_nice_yaml(value, level + 1, "dict")
for key, value in sorted(data.items())
)
)
else:
result = "{}"
else:
raise NotImplementedError(
_("Cannot serialize '{data_class}'.").format(data_class=data.__class__.__name__)
)
if nl:
return "\n" + " " * level + result
elif embed_in:
return " " + result
else:
return result


def _jaml_formatter(result: t.Any) -> str:
isatty = sys.stdout.isatty()
output = to_nice_yaml(result)
if PYGMENTS and isatty:
output = highlight(output, YamlLexer(), Terminal256Formatter(style=PYGMENTS_STYLE))
return output


REGISTERED_OUTPUT_FORMATTERS = {
"none": _none_formatter,
"json": _json_formatter,
"yaml": _yaml_formatter,
"jaml": _jaml_formatter,
}


Expand Down

0 comments on commit 3be4e75

Please sign in to comment.