Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def dump( data, dst=unicode, safe=False, force_embed=False, vspacing=None,
string_val_style=None, sort_dicts=True, multiple_docs=False, **pyyaml_kws ):
buff = io.BytesIO()
Dumper = PrettyYAMLDumper if safe else UnsafePrettyYAMLDumper
Dumper = ft.partial( Dumper,
force_embed=force_embed, string_val_style=string_val_style, sort_dicts=sort_dicts )
if not multiple_docs: data = [data]
else: pyyaml_kws.setdefault('explicit_start', True)
yaml.dump_all( data, buff, Dumper=Dumper,
default_flow_style=False, allow_unicode=True, encoding='utf-8', **pyyaml_kws )
if vspacing is not None:
dump_add_vspacing(buff, vspacing)
buff = buff.getvalue()
if dst is bytes: return buff
elif dst is unicode: return buff.decode('utf-8')
else:
try: dst.write(b'') # tests if dst is unicode- or bytestream
except: dst.write(buff.decode('utf-8'))
def choose_scalar_style(self):
is_dict_key = self.states[-1] == self.expect_block_mapping_simple_value
if is_dict_key:
# Don't mess-up (replace) styles for dict keys, if possible
if self.pyaml_string_val_style: self.event.style = 'plain'
else:
# Make sure we don't create "key: null" mapping accidentally
if self.event.value.endswith(':'): self.event.style = "'"
return super(UnsafePrettyYAMLDumper, self).choose_scalar_style()\
if self.event.style != 'plain' else ("'" if ' ' in self.event.value else None)