@@ -134,25 +134,32 @@ def prompt_for_queue(queues: dict[str, str]) -> str:
134134 return queue
135135
136136
137+ def multiline_str_representer (dumper , data ):
138+ """
139+ Render multiline strings as blocks, leave other strings unchanged.
140+
141+ This is a custom YAML representer for strings.
142+ """
143+ # see section "Constructors, representers resolvers"
144+ # at https://pyyaml.org/wiki/PyYAMLDocumentation
145+ # and https://yaml.org/spec/1.2.2/#literal-style
146+ if "\n " in data :
147+ # remove trailing whitespace from each line
148+ # (suggested fix for https://github.com/yaml/pyyaml/issues/240)
149+ data = "\n " .join (line .rstrip () for line in data .splitlines ()) + "\n "
150+ return dumper .represent_scalar (
151+ "tag:yaml.org,2002:str" , data , style = "|"
152+ )
153+ return dumper .represent_scalar ("tag:yaml.org,2002:str" , data )
154+
155+
156+ yaml .add_representer (str , multiline_str_representer )
157+
158+
137159def pretty_yaml_dump (obj , ** kwargs ) -> str :
138160 """Create a pretty YAML representation of obj.
139161
140162 :param obj: The object to be represented.
141163 :return: A pretty representation of obj as a YAML string.
142164 """
143-
144- # objective is to get multiline strings as blocks leaving any other string
145- # unchanged
146- def multiline_str_representer (dumper , data ):
147- # see section "Constructors, representers resolvers"
148- # at https://pyyaml.org/wiki/PyYAMLDocumentation
149- # and https://yaml.org/spec/1.2.2/#literal-style
150- if "\n " in data :
151- return dumper .represent_scalar (
152- "tag:yaml.org,2002:str" , data , style = "|"
153- )
154- return dumper .represent_scalar ("tag:yaml.org,2002:str" , data )
155-
156- # duplicated calls to this are no-op
157- yaml .add_representer (str , multiline_str_representer )
158165 return yaml .dump (obj , ** kwargs )
0 commit comments