@@ -26,7 +26,7 @@ def to_yaml_dict(self) -> Dict[str, Any]:
2626 Implementors should transform the object into a dictionary containing all information necessary to decode the
2727 object in the future. That dictionary will be serialized as a YAML mapping.
2828
29- Default implementation returns vars(self)
29+ Default implementation returns vars(self). TODO maybe some day we'll need to rather make a copy...?
3030 :return:
3131 """
3232 return vars (self )
@@ -49,51 +49,65 @@ def from_yaml_dict(cls: 'Type[Y]', dct: Dict[Any, Any], yaml_tag: str) -> Y:
4949 """
5050 return cls (** dct )
5151
52- def dump_yaml (self , file_path_or_stream : Union [str , TextIOBase ], ** pyyaml_kwargs ):
52+ def dump_yaml (self , file_path_or_stream : Union [str , TextIOBase ], safe : bool = True , ** pyyaml_kwargs ):
5353 """
5454 Dumps this object to a yaml file or stream using pyYaml.
5555
5656 :param file_path_or_stream: either a string representing the file path, or a stream where to write
57+ :param safe: True (default) uses `yaml.safe_dump`. False uses `yaml.dump`
5758 :param pyyaml_kwargs: keyword arguments for the pyYaml dump method
5859 :return:
5960 """
60- from yaml import dump
61+ from yaml import safe_dump , dump
6162 if isinstance (file_path_or_stream , str ):
6263 with open (file_path_or_stream , mode = 'wt' ) as f :
63- dump (self , f , ** pyyaml_kwargs )
64+ if safe :
65+ safe_dump (self , f , ** pyyaml_kwargs )
66+ else :
67+ dump (self , f , ** pyyaml_kwargs )
6468 else :
6569 with file_path_or_stream as f :
66- dump (self , f , ** pyyaml_kwargs )
70+ if safe :
71+ safe_dump (self , f , ** pyyaml_kwargs )
72+ else :
73+ dump (self , f , ** pyyaml_kwargs )
6774
68- def dumps_yaml (self , ** pyyaml_kwargs ):
75+ def dumps_yaml (self , safe : bool = True , ** pyyaml_kwargs ):
6976 """
7077 Dumps this object to a yaml string and returns it.
7178
7279 :param pyyaml_kwargs: keyword arguments for the pyYaml dump method
80+ :param safe: True (default) uses `yaml.safe_dump`. False uses `yaml.dump`
7381 :return:
7482 """
75- from yaml import dump
76- return dump (self , ** pyyaml_kwargs )
83+ from yaml import safe_dump , dump
84+ if safe :
85+ return safe_dump (self , ** pyyaml_kwargs )
86+ else :
87+ return dump (self , ** pyyaml_kwargs )
7788
7889 @classmethod
79- def loads_yaml (cls : 'Type[Y]' , yaml_str : str ) -> Y :
90+ def loads_yaml (cls : 'Type[Y]' , yaml_str : str , safe : bool = True ) -> Y :
8091 """
81- Utility method to
82- :param yaml_str
92+ Utility method to load an instance of this class from the provided yaml string. This methods only returns
93+ successfully if the result is an instance of `cls`.
94+
95+ :param yaml_str:
96+ :param safe: True (default) uses `yaml.safe_load`. False uses `yaml.load`
8397 :return:
8498 """
85- return cls .load_yaml (StringIO (yaml_str ))
99+ return cls .load_yaml (StringIO (yaml_str ), safe = safe )
86100
87101 @classmethod
88- def load_yaml (cls : 'Type[Y]' , file_path_or_stream : Union [str , TextIOBase ]) -> Y :
102+ def load_yaml (cls : 'Type[Y]' , file_path_or_stream : Union [str , TextIOBase ], safe : bool = True ) -> Y :
89103 """
90- Parses the given file path or stream as a yaml document with the
104+ Parses the given file path or stream as a yaml document. This methods only returns successfully if the result
105+ is an instance of `cls`.
91106
92107 :param file_path_or_stream:
108+ :param safe: True (default) uses `yaml.safe_load`. False uses `yaml.load`
93109 :return:
94110 """
95- safe = True # note: we cannot offer this as a parameter, otherwise to_yaml/from_yaml are not used apparently
96-
97111 from yaml import safe_load , load
98112 if isinstance (file_path_or_stream , str ):
99113 with open (file_path_or_stream , mode = 'rt' ) as f :
0 commit comments