Skip to content

Commit

Permalink
Feature: Cli-flags for controlling the dumping behavior (kapicorp#861)
Browse files Browse the repository at this point in the history
* Added presenter for multiline-strings (kapicorp#852)

* Add different presenter for null-values (kapicorp#843)

* Applied Formatting

* Added cli-flag functionality for dumping behavior

* Changed argument names

* Fixed Bug when flag was not enabled

* Added exception handling

* Applied code style

* Applied naming suggestions

* Added documentation for yaml-cli-flags

* Changed how to get args from cached

* Applied suggestion how to get args
  • Loading branch information
MatteoVoges committed Oct 7, 2022
1 parent b02a1d8 commit 257f539
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
4 changes: 4 additions & 0 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ optional arguments:
ignore the version from .kapitan
--schemas-path SCHEMAS_PATH
set schema cache path, default is "./schemas"
--yaml-multiline-string-style STYLE
set multiline string style to STYLE, default is 'double-quotes'
--yaml-dump-null-as-empty
dumps all none-type entries as empty, default is dumping as 'null'
--targets TARGET [TARGET ...], -t TARGET [TARGET ...]
targets to compile, default is all
--labels [key=value [key=value ...]], -l [key=value [key=value ...]]
Expand Down
17 changes: 17 additions & 0 deletions kapitan/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,23 @@ def build_parser():
help='set schema cache path, default is "./schemas"',
)

compile_parser.add_argument(
"--yaml-multiline-string-style",
type=str,
choices=["literal", "folded", "double-quotes"],
metavar="STYLE",
action="store",
default=from_dot_kapitan("compile", "yaml-multiline-string-style", "double-quotes"),
help="set multiline string style to STYLE, default is 'double-quotes'",
)

compile_parser.add_argument(
"--yaml-dump-null-as-empty",
default=from_dot_kapitan("compile", "yaml-dump-null-as-empty", False),
action="store_true",
help="dumps all none-type entries as empty, default is dumping as 'null'",
)

compile_selector_parser = compile_parser.add_mutually_exclusive_group()
compile_selector_parser.add_argument(
"--targets",
Expand Down
43 changes: 43 additions & 0 deletions kapitan/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,49 @@ def increase_indent(self, flow=False, indentless=False):
return super(PrettyDumper, self).increase_indent(flow, False)


def multiline_str_presenter(dumper, data):
"""
Configures yaml for dumping multiline strings with given style.
By default, strings are getting dumped with style='"'.
Ref: https://github.com/yaml/pyyaml/issues/240#issuecomment-1018712495
"""
# get parsed args from cached.py
compile_args = cached.args.get("compile", None)
style = None
if compile_args:
style = compile_args.yaml_multiline_string_style

if style == "literal":
style = "|"
elif style == "folded":
style = ">"
else:
style = '"'
if data.count("\n") > 0: # check for multiline string
return dumper.represent_scalar("tag:yaml.org,2002:str", data, style=style)
return dumper.represent_scalar("tag:yaml.org,2002:str", data)


PrettyDumper.add_representer(str, multiline_str_presenter)


def null_presenter(dumper, data):
"""Configures yaml for omitting value from null-datatype"""
# get parsed args from cached.py
compile_args = cached.args.get("compile", None)
flag_value = None
if compile_args:
flag_value = compile_args.yaml_dump_null_as_empty

if flag_value:
return dumper.represent_scalar("tag:yaml.org,2002:null", "")
else:
return dumper.represent_scalar("tag:yaml.org,2002:null", "null")


PrettyDumper.add_representer(type(None), null_presenter)


def flatten_dict(d, parent_key="", sep="."):
"""Flatten nested elements in a dictionary"""
items = []
Expand Down

0 comments on commit 257f539

Please sign in to comment.