Skip to content

Commit 70bf6b7

Browse files
authored
Support for multiline line string dump without \n (#17)
* Added support for dumping multiline strings without using \n * Added the multi line string block representer under a flag
1 parent af2922e commit 70bf6b7

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

himl/config_generator.py

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
class ConfigProcessor(object):
2727

2828
def process(self, cwd=None, path=None, filters=(), exclude_keys=(), enclosing_key=None, remove_enclosing_key=None, output_format="yaml",
29-
print_data=False, output_file=None, skip_interpolations=False, skip_interpolation_validation=False, skip_secrets=False):
29+
print_data=False, output_file=None, skip_interpolations=False, skip_interpolation_validation=False, skip_secrets=False, multi_line_string=False):
3030

3131
path = self.get_relative_path(path)
3232

@@ -39,7 +39,7 @@ def process(self, cwd=None, path=None, filters=(), exclude_keys=(), enclosing_ke
3939
if cwd is None:
4040
cwd = os.getcwd()
4141

42-
generator = ConfigGenerator(cwd, path)
42+
generator = ConfigGenerator(cwd, path, multi_line_string)
4343
generator.generate_hierarchy()
4444
generator.process_hierarchy()
4545

@@ -112,13 +112,16 @@ class ConfigGenerator(object):
112112
will contain merged data on each layer.
113113
"""
114114

115-
def __init__(self, cwd, path):
115+
def __init__(self, cwd, path, multi_line_string):
116116
self.cwd = cwd
117117
self.path = path
118118
self.hierarchy = self.generate_hierarchy()
119119
self.generated_data = OrderedDict()
120120
self.interpolation_validator = InterpolationValidator()
121121

122+
if multi_line_string is True:
123+
yaml.representer.BaseRepresenter.represent_scalar = ConfigGenerator.custom_represent_scalar
124+
122125
@staticmethod
123126
def yaml_dumper():
124127
try:
@@ -267,3 +270,27 @@ def resolve_secrets(self, default_aws_profile):
267270

268271
def validate_interpolations(self):
269272
self.interpolation_validator.check_all_interpolations_resolved(self.generated_data)
273+
274+
@staticmethod
275+
def should_use_block(value):
276+
"""
277+
https://stackoverflow.com/questions/8640959/how-can-i-control-what-scalar-form-pyyaml-uses-for-my-data
278+
"""
279+
280+
for c in u"\u000a\u000d\u001c\u001d\u001e\u0085\u2028\u2029":
281+
if c in value:
282+
return True
283+
return False
284+
285+
@staticmethod
286+
def custom_represent_scalar(self, tag, value, style=None):
287+
if style is None:
288+
if ConfigGenerator.should_use_block(value):
289+
style = '|'
290+
else:
291+
style = self.default_style
292+
293+
node = yaml.representer.ScalarNode(tag, value, style=style)
294+
if self.alias_key is not None:
295+
self.represented_objects[self.alias_key] = node
296+
return node

himl/main.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def do_run(self, opts):
3131
config_processor = ConfigProcessor()
3232
config_processor.process(cwd, opts.path, filters, excluded_keys, opts.enclosing_key, opts.remove_enclosing_key,
3333
opts.output_format, opts.print_data, opts.output_file, opts.skip_interpolation_resolving,
34-
opts.skip_interpolation_validation, opts.skip_secrets)
34+
opts.skip_interpolation_validation, opts.skip_secrets, opts.multi_line_string)
3535

3636
@staticmethod
3737
def get_parser(parser=None):
@@ -61,6 +61,8 @@ def get_parser(parser=None):
6161
help='remove enclosed data from under a common key')
6262
parser.add_argument('--cwd', dest='cwd', type=str, default="",
6363
help='the working directory')
64+
parser.add_argument('--multi-line-string', action='store_true',
65+
help='will overwrite the global yaml dumper to use block style')
6466
return parser
6567

6668

0 commit comments

Comments
 (0)