Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions box/box.py
Original file line number Diff line number Diff line change
Expand Up @@ -1021,7 +1021,7 @@ def to_yaml(
default_flow_style: bool = False,
encoding: str = "utf-8",
errors: str = "strict",
width: int = 120,
width: int = 0,
**yaml_kwargs,
):
"""
Expand All @@ -1031,7 +1031,7 @@ def to_yaml(
:param default_flow_style: False will recursively dump dicts
:param encoding: File encoding
:param errors: How to handle encoding errors
:param width: Line width for YAML output
:param width: Line width for YAML output (0 for no limit)
:param yaml_kwargs: additional arguments to pass to yaml.dump
:return: string of YAML (if no filename provided)
"""
Expand Down Expand Up @@ -1084,7 +1084,7 @@ def to_yaml(
default_flow_style: bool = False,
encoding: str = "utf-8",
errors: str = "strict",
width: int = 120,
width: int = 0,
**yaml_kwargs,
):
raise BoxError('yaml is unavailable on this system, please install the "ruamel.yaml" or "PyYAML" package')
Expand Down
11 changes: 6 additions & 5 deletions box/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,38 +197,39 @@ def _to_yaml(
errors: str = "strict",
ruamel_typ: str = "rt",
ruamel_attrs: dict | None = None,
width: int = 120,
width: int = 0,
**yaml_kwargs,
):
if not ruamel_attrs:
ruamel_attrs = {}
effective_width = width if width else None
if filename:
_exists(filename, create=True)
with open(filename, "w", encoding=encoding, errors=errors) as f:
if ruamel_available:
yaml_dumper = YAML(typ=ruamel_typ)
yaml_dumper.default_flow_style = default_flow_style
yaml_dumper.width = width
yaml_dumper.width = effective_width
for attr, value in ruamel_attrs.items():
setattr(yaml_dumper, attr, value)
return yaml_dumper.dump(obj, stream=f, **yaml_kwargs)
elif pyyaml_available:
return yaml.dump(obj, stream=f, default_flow_style=default_flow_style, width=width, **yaml_kwargs)
return yaml.dump(obj, stream=f, default_flow_style=default_flow_style, width=effective_width, **yaml_kwargs)
else:
raise BoxError(MISSING_PARSER_ERROR)

else:
if ruamel_available:
yaml_dumper = YAML(typ=ruamel_typ)
yaml_dumper.default_flow_style = default_flow_style
yaml_dumper.width = width
yaml_dumper.width = effective_width
for attr, value in ruamel_attrs.items():
setattr(yaml_dumper, attr, value)
with StringIO() as string_stream:
yaml_dumper.dump(obj, stream=string_stream, **yaml_kwargs)
return string_stream.getvalue()
elif pyyaml_available:
return yaml.dump(obj, default_flow_style=default_flow_style, width=width, **yaml_kwargs)
return yaml.dump(obj, default_flow_style=default_flow_style, width=effective_width, **yaml_kwargs)
else:
raise BoxError(MISSING_PARSER_ERROR)

Expand Down