2626class 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
0 commit comments