napalm_yang/utils.py
def model_to_dict(model, mode="", show_defaults=False):
    """
    Given a model, return a representation of the model in a dict.

    This is mostly useful to have a quick visual represenation of the model.

    Args:

        model (PybindBase): Model to transform.
        mode (string): Whether to print config, state or all elements ("" for all)

    Returns:

        dict: A dictionary representing the model.

    Examples:


        >>> config = napalm_yang.base.Root()
        >>>
        >>> # Adding models to the object
        >>> config.add_model(napalm_yang.models.openconfig_interfaces())
        >>> config.add_model(napalm_yang.models.openconfig_vlan())
        >>> # Printing the model in a human readable format
        >>> pretty_print(napalm_yang.utils.model_to_dict(config))
        >>> {
        >>>     "openconfig-interfaces:interfaces [rw]": {
        >>>         "interface [rw]": {
        >>>             "config [rw]": {
        >>>                 "description [rw]": "string",
        >>>                 "enabled [rw]": "boolean",
        >>>                 "mtu [rw]": "uint16",
        >>>                 "name [rw]": "string",
        >>>                 "type [rw]": "identityref"
        >>>             },
        >>>             "hold_time [rw]": {
        >>>                 "config [rw]": {
        >>>                     "down [rw]": "uint32",
        >>>                     "up [rw]": "uint32"
            (trimmed for clarity)
    """

    def is_mode(obj, mode):
        if mode == "":
            return True
        elif mode == "config":
            return obj._yang_name == "config" or obj._is_config
        elif mode == "state":
            return obj._yang_name == "state" or not obj._is_config
        else:
            raise ValueError(
                "mode can only be config, state or ''. Passed: {}".format(mode)
            )

    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

    if model._yang_type in ("container", "list"):
        cls = model if model._yang_type in ("container",) else model._contained_class()
        result = {}
        for k, v in cls:
            r = model_to_dict(v, mode=mode, show_defaults=show_defaults)
            if 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
def model_to_dict(model, mode="", show_defaults=False):
    def is_mode(obj, mode):
        if mode == "":
            return True
        elif mode == "config":
            return obj._yang_name == "config" or obj._is_config
        elif mode == "state":
            return obj._yang_name == "state" or not obj._is_config
        else:
            raise ValueError(
                "mode can only be config, state or ''. Passed: {}".format(mode)
            )

    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

    if model._yang_type in ("container", "list"):
        cls = model if model._yang_type in ("container",) else model._contained_class()
        result = {}
        for k, v in cls:
            r = model_to_dict(v, mode=mode, show_defaults=show_defaults)
            if 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
def model_to_dict ( model , mode = "" , show_defaults = False ) : def is_mode ( obj , mode ) : if mode == "" : return True elif mode == "config" : return obj . _yang_name == "config" or obj . _is_config elif mode == "state" : return obj . _yang_name == "state" or not obj . _is_config else : raise ValueError ( "mode can only be config, state or ''. Passed: {}" . format ( mode ) ) 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 if model . _yang_type in ( "container" , "list" ) : cls = model if model . _yang_type in ( "container" , ) else model . _contained_class ( ) result = { } for k , v in cls : r = model_to_dict ( v , mode = mode , show_defaults = show_defaults ) if 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
Given a model, return a representation of the model in a dict.

    This is mostly useful to have a quick visual represenation of the model.

    Args:

        model (PybindBase): Model to transform.
        mode (string): Whether to print config, state or all elements ("" for all)

    Returns:

        dict: A dictionary representing the model.

    Examples:


        >>> config = napalm_yang.base.Root()
        >>>
        >>> # Adding models to the object
        >>> config.add_model(napalm_yang.models.openconfig_interfaces())
        >>> config.add_model(napalm_yang.models.openconfig_vlan())
        >>> # Printing the model in a human readable format
        >>> pretty_print(napalm_yang.utils.model_to_dict(config))
        >>> {
        >>>     "openconfig-interfaces:interfaces [rw]": {
        >>>         "interface [rw]": {
        >>>             "config [rw]": {
        >>>                 "description [rw]": "string",
        >>>                 "enabled [rw]": "boolean",
        >>>                 "mtu [rw]": "uint16",
        >>>                 "name [rw]": "string",
        >>>                 "type [rw]": "identityref"
        >>>             },
        >>>             "hold_time [rw]": {
        >>>                 "config [rw]": {
        >>>                     "down [rw]": "uint32",
        >>>                     "up [rw]": "uint32"
            (trimmed for clarity)
Given a model return a representation of the model in a dict .