Skip to content

Commit

Permalink
Switch from PyYAML to ruamel.yaml (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
Cariad Eccleston authored Dec 6, 2020
1 parent b024bbe commit 5b88fe0
Show file tree
Hide file tree
Showing 16 changed files with 121 additions and 199 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ jobs:
- name: Assert
run: |
dwalk --directory testing/bottom \
--filenames dwalk.2.yml dwalk.1.yml > actual
diff testing/expect.txt actual
dwalk --directory testing/bottom \
--filenames dwalk.3.yml dwalk.2.yml dwalk.1.yml > actual
diff testing/expect.yml actual
publish:
if: startsWith(github.ref, 'refs/tags')
Expand Down
4 changes: 4 additions & 0 deletions .yamllint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@ rules:
allow-non-breakable-inline-mappings: true
allow-non-breakable-words: true
max: 80

ignore: |
testing/example-with-metadata.yml
testing/expect.yml
4 changes: 1 addition & 3 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ verify_ssl = true

[dev-packages]
black = "==20.8b1"
boto3-stubs = "*"
coverage = "*"
flake8 = "*"
isort = "*"
Expand All @@ -17,8 +16,7 @@ wheel = "*"
yamllint = "*"

[packages]
boto3 = "*"
pyyaml = "*"
"ruamel.yaml" = "~=0.16"

[requires]
python_version = "3.9"
84 changes: 5 additions & 79 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/bin/bash -e
rm -rf dist
python setup.py bdist_wheel
rm -rf build
8 changes: 6 additions & 2 deletions dwalk/cli.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from argparse import ArgumentParser
from json import dumps
from logging import basicConfig, getLogger
from pathlib import Path
from sys import stdout
from typing import List, Optional

from ruamel.yaml import YAML

from dwalk import dwalk
from dwalk.version import get_version

Expand Down Expand Up @@ -63,7 +65,9 @@ def execute(self) -> int:
directory=Path(self.args.directory) if self.args.directory else None,
include_meta=self.args.include_meta,
)
print(dumps(result, sort_keys=True, indent=2))
yaml = YAML(typ="safe")
yaml.default_flow_style = False
yaml.dump(result, stdout)
return 0
except Exception as e:
self.logger.exception(e)
Expand Down
4 changes: 2 additions & 2 deletions dwalk/dwalk.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from pathlib import Path
from typing import Any, Dict, List, Optional, Union

from yaml import safe_load
from ruamel.yaml import YAML

from dwalk.directories import directories
from dwalk.merge import merge, set_key
Expand Down Expand Up @@ -55,7 +55,7 @@ def dwalk(
with open(path, "r") as stream:
logger.debug("Reading and merging: %s", path)
merge(
from_dict=safe_load(stream),
from_dict=YAML(typ="safe").load(stream),
from_src=str(path) if include_meta else None,
to_dict=result,
)
Expand Down
15 changes: 9 additions & 6 deletions dwalk/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from pathlib import Path

from mock import Mock, patch
from mock import ANY, Mock, patch
from ruamel.yaml import YAML

from dwalk.cli import CLI

Expand Down Expand Up @@ -28,21 +29,23 @@ def test_print_version() -> None:
assert CLI().print_version() == 0


@patch("builtins.print")
def test_execute(print: Mock) -> None:
@patch("dwalk.cli.YAML.dump")
def test_execute(dump: Mock) -> None:
testing = Path(__file__).parent.parent.joinpath("testing").absolute()
bottom = testing.joinpath("bottom")
cli = CLI(
[
"--directory",
str(bottom),
"--filenames",
"dwalk.3.yml",
"dwalk.2.yml",
"dwalk.1.yml",
],
)
assert cli.invoke() == 0

with open(testing.joinpath("expect.txt"), "r") as stream:
expect = stream.read().rstrip()
print.assert_called_with(expect)
with open(testing.joinpath("expect.yml"), "r") as stream:
expect = YAML(typ="safe").load(stream)

dump.assert_called_with(expect, ANY)
1 change: 1 addition & 0 deletions dwalk/test_dwalk.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def test() -> None:
"is_top_2": True,
"shopping_list": ["atari", "bismuth", "cookies"],
"side_count": {"hexagon": 6, "pentagon": 5, "square": 4, "triangle": 3},
("a", "b"): "alpha-bravo",
}


Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ warn_redundant_casts = True
warn_return_any = True
warn_unused_ignores = True

[mypy-botocore.*]
[mypy-ruamel.*]
ignore_missing_imports = True
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
},
include_package_data=True,
install_requires=[
"pyyaml",
"ruamel.yaml~=0.16",
],
license="MIT License",
long_description=long_description,
Expand Down
2 changes: 2 additions & 0 deletions testing/bottom/dwalk.1.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ alphabet:
# "b" is overridden in this file.
b: bravo

[a, b]: alpha-bravo

# Lists are not merged; this one takes precedence.
shopping_list:
- atari
Expand Down
78 changes: 0 additions & 78 deletions testing/example-with-metadata.json

This file was deleted.

Loading

0 comments on commit 5b88fe0

Please sign in to comment.