Skip to content
This repository has been archived by the owner on May 15, 2019. It is now read-only.

Commit

Permalink
Add show_defaults kwarg to the model_to_dict util fun
Browse files Browse the repository at this point in the history
  • Loading branch information
mirceaulinic committed Jan 29, 2018
1 parent b9bb62b commit b6c18a1
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions napalm_yang/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from napalm_yang import base


def model_to_dict(model, mode=""):
def model_to_dict(model,
mode="",
show_defaults=False):
"""
Given a model, return a representation of the model in a dict.
Expand Down Expand Up @@ -52,9 +54,10 @@ def is_mode(obj, mode):
else:
raise ValueError("mode can only be config, state or ''. Passed: {}".format(mode))

def get_key(key, model, parent_defining_module):
key = "{} {}".format(key, "[rw]" if model._is_config else "[ro]")

def get_key(key, model, parent_defining_module, show_defaults):
if not show_defaults:
# No need to display rw/ro when showing the defaults.
key = "{} {}".format(key, "[rw]" if model._is_config else "[ro]")
if parent_defining_module != model._defining_module:
key = "{}:{}".format(model._defining_module, key)
return key
Expand All @@ -63,11 +66,19 @@ def get_key(key, model, parent_defining_module):
cls = model if model._yang_type in ("container", ) else model._contained_class()
result = {}
for k, v in cls:
r = model_to_dict(v, mode)
r = model_to_dict(v, mode=mode, show_defaults=show_defaults)
if r:
result[get_key(k, v, model._defining_module)] = r
result[get_key(k, v, model._defining_module, show_defaults)] = r
return result
else:
if show_defaults:
if model._default is False:
if model._yang_type != 'boolean':
# Unless the datatype is bool, when the _default attribute
# is False, it means there is not default value defined in
# the YANG model.
return None
return model._default
return model._yang_type if is_mode(model, mode) else None


Expand Down

0 comments on commit b6c18a1

Please sign in to comment.