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

Format jaml #945

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 ambiguities.
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
Loading