@@ -820,6 +820,16 @@ def write_version_directive(self, version_text: Any) -> None:
820
820
class FormattedYAML (YAML ):
821
821
"""A YAML loader/dumper that handles ansible content better by default."""
822
822
823
+ default_config = {
824
+ "explicit_start" : True ,
825
+ "explicit_end" : False ,
826
+ "width" : 160 ,
827
+ "indent_sequences" : True ,
828
+ "preferred_quote" : '"' ,
829
+ "min_spaces_inside" : 0 ,
830
+ "max_spaces_inside" : 1 ,
831
+ }
832
+
823
833
def __init__ ( # pylint: disable=too-many-arguments
824
834
self ,
825
835
* ,
@@ -828,6 +838,7 @@ def __init__( # pylint: disable=too-many-arguments
828
838
output : Any = None ,
829
839
plug_ins : list [str ] | None = None ,
830
840
version : tuple [int , int ] | None = None ,
841
+ config : dict [str , bool | int | str ] | None = None ,
831
842
):
832
843
"""Return a configured ``ruamel.yaml.YAML`` instance.
833
844
@@ -897,7 +908,8 @@ def __init__( # pylint: disable=too-many-arguments
897
908
898
909
# NB: We ignore some mypy issues because ruamel.yaml typehints are not great.
899
910
900
- config = self ._defaults_from_yamllint_config ()
911
+ if not config :
912
+ config = self ._defaults_from_yamllint_config ()
901
913
902
914
# these settings are derived from yamllint config
903
915
self .explicit_start : bool = config ["explicit_start" ] # type: ignore[assignment]
@@ -950,15 +962,8 @@ def __init__( # pylint: disable=too-many-arguments
950
962
@staticmethod
951
963
def _defaults_from_yamllint_config () -> dict [str , bool | int | str ]:
952
964
"""Extract FormattedYAML-relevant settings from yamllint config if possible."""
953
- config = {
954
- "explicit_start" : True ,
955
- "explicit_end" : False ,
956
- "width" : 160 ,
957
- "indent_sequences" : True ,
958
- "preferred_quote" : '"' ,
959
- "min_spaces_inside" : 0 ,
960
- "max_spaces_inside" : 1 ,
961
- }
965
+ config = FormattedYAML .default_config
966
+
962
967
for rule , rule_config in load_yamllint_config ().rules .items ():
963
968
if not rule_config :
964
969
# rule disabled
@@ -1062,6 +1067,7 @@ def dumps(self, data: Any) -> str:
1062
1067
return self ._post_process_yaml (
1063
1068
text ,
1064
1069
strip_version_directive = strip_version_directive ,
1070
+ strip_explicit_start = not self .explicit_start ,
1065
1071
)
1066
1072
1067
1073
def _prevent_wrapping_flow_style (self , data : Any ) -> None :
@@ -1150,7 +1156,12 @@ def _pre_process_yaml(self, text: str) -> tuple[str, str | None]:
1150
1156
return text , "" .join (preamble_comments ) or None
1151
1157
1152
1158
@staticmethod
1153
- def _post_process_yaml (text : str , * , strip_version_directive : bool = False ) -> str :
1159
+ def _post_process_yaml (
1160
+ text : str ,
1161
+ * ,
1162
+ strip_version_directive : bool = False ,
1163
+ strip_explicit_start : bool = False ,
1164
+ ) -> str :
1154
1165
"""Handle known issues with ruamel.yaml dumping.
1155
1166
1156
1167
Make sure there's only one newline at the end of the file.
@@ -1166,6 +1177,10 @@ def _post_process_yaml(text: str, *, strip_version_directive: bool = False) -> s
1166
1177
if strip_version_directive and text .startswith ("%YAML" ):
1167
1178
text = text .split ("\n " , 1 )[1 ]
1168
1179
1180
+ # remove explicit document start
1181
+ if strip_explicit_start and text .startswith ("---" ):
1182
+ text = text .split ("\n " , 1 )[1 ]
1183
+
1169
1184
text = text .rstrip ("\n " ) + "\n "
1170
1185
1171
1186
lines = text .splitlines (keepends = True )
0 commit comments