Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

address UTF-8 error on windows #654

Merged
merged 4 commits into from
Mar 2, 2021
Merged
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
9 changes: 6 additions & 3 deletions nornir/plugins/inventory/simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ def __init__(
host_file: str = "hosts.yaml",
group_file: str = "groups.yaml",
defaults_file: str = "defaults.yaml",
encoding: str = "utf-8",
) -> None:
"""
SimpleInventory is an inventory plugin that loads data from YAML files.
Expand All @@ -82,32 +83,34 @@ def __init__(
it doesn't exist it will be skipped
defaults_file: path to file with defaults definition.
If it doesn't exist it will be skipped
encoding: Encoding used to save inventory files. Defaults to utf-8
"""

self.host_file = pathlib.Path(host_file).expanduser()
self.group_file = pathlib.Path(group_file).expanduser()
self.defaults_file = pathlib.Path(defaults_file).expanduser()
self.encoding = encoding

def load(self) -> Inventory:
yml = ruamel.yaml.YAML(typ="safe")

if self.defaults_file.exists():
with open(self.defaults_file, "r") as f:
with open(self.defaults_file, "r", encoding=self.encoding) as f:
defaults_dict = yml.load(f) or {}
defaults = _get_defaults(defaults_dict)
else:
defaults = Defaults()

hosts = Hosts()
with open(self.host_file, "r") as f:
with open(self.host_file, "r", encoding=self.encoding) as f:
hosts_dict = yml.load(f)

for n, h in hosts_dict.items():
hosts[n] = _get_inventory_element(Host, h, n, defaults)

groups = Groups()
if self.group_file.exists():
with open(self.group_file, "r") as f:
with open(self.group_file, "r", encoding=self.encoding) as f:
groups_dict = yml.load(f) or {}

for n, g in groups_dict.items():
Expand Down
3 changes: 2 additions & 1 deletion tests/plugins/inventory/data/hosts.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ dev5.no_group:
username:
password:
platform: linux
data: {}
data:
smiley: 😜
groups: []
connection_options: {}
2 changes: 1 addition & 1 deletion tests/plugins/inventory/test_simple_inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ def test(self):
},
"dev5.no_group": {
"connection_options": {},
"data": {},
"data": {"smiley": "😜"},
"groups": [],
"hostname": "localhost",
"name": "dev5.no_group",
Expand Down