Skip to content

Commit

Permalink
allow passing encoding to SimpleInventory
Browse files Browse the repository at this point in the history
  • Loading branch information
dbarrosop committed Mar 1, 2021
1 parent ea16c92 commit f0db117
Showing 1 changed file with 6 additions and 3 deletions.
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

0 comments on commit f0db117

Please sign in to comment.