Skip to content

Commit

Permalink
* Add ability to set attributes to ruamel.yaml class
Browse files Browse the repository at this point in the history
* Fix pytest working with underscore functions
* Fix for pyinstaller
* Fix python publish version issues
  • Loading branch information
cdgriffith committed Jan 23, 2022
1 parent c453973 commit 95dd99a
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pythonpublish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ jobs:
- name: Set up Python 3.10
uses: actions/setup-python@v1
with:
python-version: 3.10
python-version: "3.10"

- uses: RalfG/python-wheels-manylinux-build@v0.4.2-manylinux2014_x86_64
with:
Expand Down
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ include CHANGES.rst
include box/py.typed
include box/*.c
include box/*.so
include box/*.pyd
3 changes: 2 additions & 1 deletion box/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
# -*- coding: utf-8 -*-

__author__ = "Chris Griffith"
__version__ = "6.0.0rc2"
__version__ = "6.0.0rc3"

from box.box import Box
from box.box_list import BoxList
from box.config_box import ConfigBox
from box.exceptions import BoxError, BoxKeyError
from box.from_file import box_from_file
from box.shorthand_box import SBox
import box.converters

__all__ = [
"Box",
Expand Down
16 changes: 15 additions & 1 deletion box/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from io import StringIO
from os import PathLike
from pathlib import Path
from typing import Union
from typing import Union, Optional, Dict

from box.exceptions import BoxError

Expand Down Expand Up @@ -119,14 +119,19 @@ def _to_yaml(
encoding: str = "utf-8",
errors: str = "strict",
ruamel_typ: str = "rt",
ruamel_attrs: Optional[Dict] = None,
**yaml_kwargs,
):
if not ruamel_attrs:
ruamel_attrs = {}
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
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, **yaml_kwargs)
Expand All @@ -137,6 +142,8 @@ def _to_yaml(
if ruamel_available:
yaml_dumper = YAML(typ=ruamel_typ)
yaml_dumper.default_flow_style = default_flow_style
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()
Expand All @@ -152,13 +159,18 @@ def _from_yaml(
encoding: str = "utf-8",
errors: str = "strict",
ruamel_typ: str = "rt",
ruamel_attrs: Optional[Dict] = None,
**kwargs,
):
if not ruamel_attrs:
ruamel_attrs = {}
if filename:
_exists(filename)
with open(filename, "r", encoding=encoding, errors=errors) as f:
if ruamel_available:
yaml_loader = YAML(typ=ruamel_typ)
for attr, value in ruamel_attrs.items():
setattr(yaml_loader, attr, value)
data = yaml_loader.load(stream=f)
elif pyyaml_available:
if "Loader" not in kwargs:
Expand All @@ -169,6 +181,8 @@ def _from_yaml(
elif yaml_string:
if ruamel_available:
yaml_loader = YAML(typ=ruamel_typ)
for attr, value in ruamel_attrs.items():
setattr(yaml_loader, attr, value)
data = yaml_loader.load(stream=yaml_string)
elif pyyaml_available:
if "Loader" not in kwargs:
Expand Down
4 changes: 3 additions & 1 deletion box/converters.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from box.exceptions import BoxError as BoxError
from os import PathLike as PathLike
from typing import Any, Union
from typing import Any, Union, Optional, Dict

yaml_available: bool
toml_available: bool
Expand All @@ -26,6 +26,7 @@ def _to_yaml(
encoding: str = "utf-8",
errors: str = "strict",
ruamel_typ: str = "rt",
ruamel_attrs: Optional[Dict] = None,
**yaml_kwargs,
) -> Any: ...
def _from_yaml(
Expand All @@ -34,6 +35,7 @@ def _from_yaml(
encoding: str = "utf-8",
errors: str = "strict",
ruamel_typ: str = "rt",
ruamel_attrs: Optional[Dict] = None,
**kwargs,
) -> Any: ...
def _to_toml(obj, filename: Union[str, PathLike] = None, encoding: str = "utf-8", errors: str = "strict") -> Any: ...
Expand Down
11 changes: 5 additions & 6 deletions test/test_box.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
import pytest
from ruamel.yaml import YAML

from box import Box, BoxError, BoxKeyError, BoxList, ConfigBox, SBox, box
from box.box import _get_dot_paths # type: ignore
from box import Box, BoxError, BoxKeyError, BoxList, ConfigBox, SBox
from box.box import _get_dot_paths, _camel_killer, _recursive_tuples # type: ignore
from box.converters import BOX_PARAMETERS


Expand Down Expand Up @@ -60,8 +60,8 @@ def test_safe_attrs(self):
assert Box()._safe_attr(356) == "x356"

def test_camel_killer(self):
assert box._camel_killer("CamelCase") == "camel_case"
assert box._camel_killer("Terrible321KeyA") == "terrible321_key_a"
assert _camel_killer("CamelCase") == "camel_case"
assert _camel_killer("Terrible321KeyA") == "terrible321_key_a"
bx = Box(camel_killer_box=True, conversion_box=False)

bx.DeadCamel = 3
Expand Down Expand Up @@ -90,7 +90,7 @@ def test_camel_killer(self):
assert len(bx1.keys()) == 0

def test_recursive_tuples(self):
out = box._recursive_tuples(
out = _recursive_tuples(
({"test": "a"}, ({"second": "b"}, {"third": "c"}, ("fourth",))), dict, recreate_tuples=True
)
assert isinstance(out, tuple)
Expand Down Expand Up @@ -1321,7 +1321,6 @@ def test_box_kwargs_should_not_be_included(self):
assert bx == Box()

for param in BOX_PARAMETERS:
print(param)
assert param in params

def test_box_greek(self):
Expand Down
11 changes: 11 additions & 0 deletions test/test_converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,14 @@ def test_to_msgpack(self):
_to_msgpack(movie_data, filename=m_file)
assert b"Rick Moranis" in open(m_file, "rb").read()
assert msgpack.unpack(open(m_file, "rb")) == msgpack.unpackb(msg_data)

def test_to_yaml_ruamel(self):
movie_string = _to_yaml(movie_data, ruamel_attrs={"width": 12})
multiline_except = """ - name: Roger
Rees
imdb: nm0715953
role: Sheriff
of Rottingham
- name: Amy
Yasbeck"""
assert multiline_except in movie_string

0 comments on commit 95dd99a

Please sign in to comment.