-
Notifications
You must be signed in to change notification settings - Fork 226
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate to ruamel.yaml and add
ordered_dict
parameter to `load_json…
…` and `load_yaml` plugins (#157)
- Loading branch information
Showing
10 changed files
with
66 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,33 @@ | ||
import collections | ||
import json | ||
|
||
from nornir.core.task import Result | ||
|
||
|
||
def load_json(task, file): | ||
def load_json(task, file, ordered_dict=False): | ||
""" | ||
Loads a json file. | ||
Arguments: | ||
file (str): path to the file containing the json file to load | ||
ordered_dict (bool): 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) | ||
Returns: | ||
:obj:`nornir.core.task.Result`: | ||
* result (``dict``): dictionary with the contents of the file | ||
""" | ||
kwargs = {} | ||
if ordered_dict: | ||
kwargs["object_pairs_hook"] = collections.OrderedDict | ||
with open(file, "r") as f: | ||
data = json.loads(f.read()) | ||
data = json.loads(f.read(), **kwargs) | ||
|
||
return Result(host=task.host, result=data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,32 @@ | ||
from nornir.core.task import Result | ||
|
||
import ruamel.yaml | ||
|
||
import yaml | ||
|
||
|
||
def load_yaml(task, file): | ||
def load_yaml(task, file, ordered_dict=False): | ||
""" | ||
Loads a yaml file. | ||
Arguments: | ||
file (str): path to the file containing the yaml file to load | ||
ordered_dict (bool): 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) | ||
Returns: | ||
:obj:`nornir.core.task.Result`: | ||
* result (``dict``): dictionary with the contents of the file | ||
""" | ||
kwargs = {} | ||
kwargs["typ"] = "rt" if ordered_dict else "safe" | ||
with open(file, "r") as f: | ||
data = yaml.load(f.read()) | ||
yml = ruamel.yaml.YAML(pure=True, **kwargs) | ||
data = yml.load(f.read()) | ||
|
||
return Result(host=task.host, result=data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
colorama | ||
pyyaml | ||
jinja2 | ||
napalm>=2.3.0 | ||
netmiko>=2.1.1 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
--- | ||
num_workers: 10 | ||
raise_on_error: no | ||
raise_on_error: false | ||
user_defined: "asdasd" | ||
my_root: | ||
user_defined: "i am nested" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,5 +3,6 @@ | |
"services": [ | ||
"dhcp", | ||
"dns" | ||
] | ||
], | ||
"a_dict": {"a": 1, "b": 2} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,6 @@ env: test | |
services: | ||
- dhcp | ||
- dns | ||
a_dict: | ||
a: 1 | ||
b: 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters