Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make mkylib reusable and install it #24

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions docs/quickstart.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ by reading YANG library data [RFC7895]_ from a file:
'Data model ID: 9a9b7d2d28d4d78fa42e12348346990e3fb1c1b9'

.. note::
Distribution directory *tools/python* contains the script *mkylib.py* that
can help with preparing YANG library data.
YANG library data can be created by running *yangson-mkylib*.

Here is an ASCII art depicting the schema tree:

Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
author_email = "[email protected]",
url = "https://github.com/CZ-NIC/yangson",
entry_points = {
"console_scripts": ["yangson=yangson.__main__:main"]
"console_scripts": ["yangson=yangson.__main__:main",
"yangson-mkylib=yangson.mkylib:main"]
},
install_requires = ["PyXB"],
tests_require = ["pytest"],
Expand Down
25 changes: 16 additions & 9 deletions tools/python/mkylib.py → yangson/mkylib.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

from yangson.statement import ModuleParser

ydir = sys.argv[1]
"""Name of the directory with YANG (sub)modules."""
data_kws = ["augment", "container", "leaf", "leaf-list", "list", "rpc", "notification", "identity"]
"""Keywords of statements that contribute nodes to the schema tree."""
Expand All @@ -23,6 +22,9 @@
submodmap = {}
"""Dictionary for collecting submodule data."""

class GeneratingError(Exception):
"""Error when producing ietf-yang-library data"""


def module_entry(yfile):
"""Add entry for one file containing YANG module text.
Expand Down Expand Up @@ -63,7 +65,7 @@ def module_entry(yfile):
modmap[(mst.argument, rev)] = rec


def main():
def process_directory(ydir):
for infile in os.listdir(ydir):
if not infile.endswith(".yang"):
continue
Expand All @@ -82,13 +84,11 @@ def main():
try:
srec = submodmap[subm]
except KeyError:
print(f"Submodule {subm} not available.", file=sys.stderr)
return 1
raise GeneratingError(f"Submodule {subm} not available.")
if srev is None or srev == srec["revision"]:
sen["revision"] = srec["revision"]
else:
print(f"Submodule {subm} revision mismatch.", file=sys.stderr)
return 1
raise GeneratingError(f"Submodule {subm} revision mismatch.")
imp_only = imp_only or srec["import-only"]
fts += srec["features"]
sarr.append(sen)
Expand All @@ -104,9 +104,16 @@ def main():
"module": marr
}
}
print(json.dumps(res, indent=2))
return 0
return res


def main():
try:
print(json.dumps(process_directory(sys.argv[1]), indent=2))
except GeneratingError as e:
print(e, file=sys.stderr)
sys.exit(1)


if __name__ == '__main__':
sys.exit(main())
main()