Skip to content

Commit

Permalink
remove ordereddict as it's not needed in py3.6+ (#190)
Browse files Browse the repository at this point in the history
  • Loading branch information
dbarrosop committed Jul 19, 2018
1 parent 6de87ee commit 0a1027e
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 36 deletions.
11 changes: 3 additions & 8 deletions nornir/plugins/tasks/data/load_json.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
import collections
import json
from typing import Dict, MutableMapping, Any, Type
from typing import Any, Dict, MutableMapping, Type

from nornir.core.task import Result, Task


def load_json(task: Task, file: str, ordered_dict: bool = False) -> Result:
def load_json(task: Task, file: str) -> Result:
"""
Loads a json file.
Arguments:
file: path to the file containing the json file to load
ordered_dict: If set to true used OrderedDict to load maps
Examples:
Simple example with ``ordered_dict``::
> nr.run(task=load_json,
file="mydata.json",
ordered_dict=True)
file="mydata.json")
file: path to the file containing the json file to load
Expand All @@ -28,8 +25,6 @@ def load_json(task: Task, file: str, ordered_dict: bool = False) -> Result:
* result (``dict``): dictionary with the contents of the file
"""
kwargs: Dict[str, Type[MutableMapping[str, Any]]] = {}
if ordered_dict:
kwargs["object_pairs_hook"] = collections.OrderedDict
with open(file, "r") as f:
data = json.loads(f.read(), **kwargs)

Expand Down
10 changes: 4 additions & 6 deletions nornir/plugins/tasks/data/load_yaml.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,29 @@
import ruamel.yaml
from typing import Dict

from nornir.core.task import Result, Task

import ruamel.yaml


def load_yaml(task: Task, file: str, ordered_dict: bool = False):
def load_yaml(task: Task, file: str):
"""
Loads a yaml file.
Arguments:
file: path to the file containing the yaml file to load
ordered_dict: If set to true used OrderedDict to load maps
Examples:
Simple example with ``ordered_dict``::
> nr.run(task=load_yaml,
file="mydata.yaml",
ordered_dict=True)
file="mydata.yaml")
Returns:
Result object with the following attributes set:
* result (``dict``): dictionary with the contents of the file
"""
kwargs: Dict[str, str] = {}
kwargs["typ"] = "rt" if ordered_dict else "safe"
with open(file, "r") as f:
yml = ruamel.yaml.YAML(pure=True, **kwargs)
data = yml.load(f.read())
Expand Down
11 changes: 0 additions & 11 deletions tests/plugins/tasks/data/test_load_json.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import os
from collections import OrderedDict

from nornir.plugins.tasks import data

Expand All @@ -19,16 +18,6 @@ def test_load_json(self, nornir):
assert d["services"] == ["dhcp", "dns"]
assert isinstance(d["a_dict"], dict)

def test_load_json_ordered_dict(self, nornir):
test_file = "{}/simple.json".format(data_dir)
result = nornir.run(data.load_json, file=test_file, ordered_dict=True)

for h, r in result.items():
d = r.result
assert d["env"] == "test"
assert d["services"] == ["dhcp", "dns"]
assert isinstance(d["a_dict"], OrderedDict)

def test_load_json_error_broken_file(self, nornir):
test_file = "{}/broken.json".format(data_dir)
results = nornir.run(data.load_json, file=test_file)
Expand Down
11 changes: 0 additions & 11 deletions tests/plugins/tasks/data/test_load_yaml.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import os
from collections import OrderedDict

from nornir.plugins.tasks import data

Expand All @@ -21,16 +20,6 @@ def test_load_yaml(self, nornir):
assert d["services"] == ["dhcp", "dns"]
assert isinstance(d["a_dict"], dict)

def test_load_yaml_ordered_dict(self, nornir):
test_file = "{}/simple.yaml".format(data_dir)
result = nornir.run(data.load_yaml, file=test_file, ordered_dict=True)

for h, r in result.items():
d = r.result
assert d["env"] == "test"
assert d["services"] == ["dhcp", "dns"]
assert isinstance(d["a_dict"], OrderedDict)

def test_load_yaml_error_broken_file(self, nornir):
test_file = "{}/broken.yaml".format(data_dir)
results = nornir.run(data.load_yaml, file=test_file)
Expand Down

0 comments on commit 0a1027e

Please sign in to comment.