Skip to content
Snippets Groups Projects
Commit 261953f7 authored by Ladislav Lhotka's avatar Ladislav Lhotka
Browse files

Add description argument to DataModel constructor.

parent 40c51a66
Branches
Tags
No related merge requests found
......@@ -25,14 +25,21 @@ document from :ref:`sec-ex1`.
__ http://www.sphinx-doc.org/en/stable/ext/doctest.html
.. class:: DataModel(yltxt: str, mod_path: List[str])
.. class:: DataModel(yltxt: str, mod_path: List[str], \
description: str = None)
This class provides a basic user-level entry point to the *Yangson*
library.
The constructor argument *yltxt* is a string with JSON-encoded YANG
library data [RFC7895]_, and *mod_path* is a list of filesystem
directories in which *Yangson* searches for YANG modules.
directories in which *Yangson* searches for YANG modules (by
default it is only the current directory).
The *description* argument allows for adding a description text to
the entire data model. If it is ``None``, then a default
description is added which contains the ``module-set-id`` value
from the YANG library data.
The class constructor may raise the following exceptions:
......@@ -55,14 +62,14 @@ __ http://www.sphinx-doc.org/en/stable/ext/doctest.html
.. rubric:: Public Methods
.. classmethod:: from_file(name: str, mod_path: List[str] = ["."] ) \
-> DataModel
.. classmethod:: from_file(name: str, mod_path: List[str] = ["."], \
description: str = None) -> DataModel
Initialize the data model from a file containing JSON-encoded
YANG library data and return the :class:`DataModel`
instance. The *name* argument is the name of that file, and
*mod_path* has the same meaning as in the class constructor. By
default, *mod_path* includes only the current directory.
instance. The *name* argument is the name of that file. The
remaining two arguments are passed unchanged to the
:class:`DataModel` class constructor.
This method may raise the same exceptions as the class
constructor.
......@@ -218,7 +225,7 @@ __ http://www.sphinx-doc.org/en/stable/ext/doctest.html
.. doctest::
>>> len(dm.schema_digest())
134
208
.. _6.1: https://tools.ietf.org/html/rfc7951#section-6.1
.. _7.5.1: https://tools.ietf.org/html/rfc7950#section-7.5.1
......
......@@ -34,6 +34,8 @@ by reading YANG library data [RFC7895]_ from a file:
.. doctest::
>>> dm = DataModel.from_file('yang-library-ex2.json')
>>> dm.schema.description
'Data model ID: 9a9b7d2d28d4d78fa42e12348346990e3fb1c1b9'
Here is an ASCII art depicting the schema tree:
......
......@@ -38,12 +38,14 @@ class DataModel:
"""Basic user-level entry point to Yangson library."""
@classmethod
def from_file(cls, name: str, mod_path: List[str] = ["."]) -> "DataModel":
def from_file(cls, name: str, mod_path: List[str] = ["."],
description: str = None) -> "DataModel":
"""Initialize the data model from a file with YANG library data.
Args:
name: Name of a file with YANG library data.
mod_path: List of directories where to look for YANG modules.
description: Optional description of the data model.
Returns:
The data model instance.
......@@ -53,14 +55,16 @@ class DataModel:
"""
with open(name, encoding="utf-8") as infile:
yltxt = infile.read()
return cls(yltxt, mod_path)
return cls(yltxt, mod_path, description)
def __init__(self, yltxt: str, mod_path: List[str]):
def __init__(self, yltxt: str, mod_path: List[str] = ["."],
description: str = None):
"""Initialize the class instance.
Args:
yltxt: JSON text with YANG library data.
mod_path: List of directories where to look for YANG modules.
description: Optional description of the data model.
Raises:
BadYangLibraryData: If YANG library data is invalid.
......@@ -79,6 +83,8 @@ class DataModel:
raise BadYangLibraryData(str(e)) from None
self.schema_data = SchemaData(yl, mod_path)
self._build_schema()
self.schema.description = description if description else (
"Data model ID: " + yl["ietf-yang-library:modules-state"]["module-set-id"])
def module_set_id(self) -> str:
"""Compute unique id of YANG modules comprising the data model.
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment