mbj4668 / pyang

An extensible YANG validator and converter in python
ISC License
530 stars 342 forks source link

How to use pyang in another python program? #846

Open rkkilari opened 1 year ago

rkkilari commented 1 year ago

Can we use pyang in a python program? Can anyone help providing some links where such an example code is provided. I tried to use pyang to 1) load yang file 2) parse it in to statements and ... 3) traverse the statements and generate angular code templates as per container leaf, leaf-list, ...

hyberdk commented 8 months ago

hi @rkkilari,

I have used the pyang_tool.py as inspiration..

but essentially you need this to get started..

from pyang import statements
from pyang.statements import ModSubmodStatement
from pyang.repository import FileRepository
from pyang.context import Context

repo = FileRepository(path="ios-xe_17.6.5")
ctx = Context(repo)
module: ModSubmodStatement = ctx.read_module("Cisco-IOS-XE-native")
statements.validate_module(ctx, module)

Hope that helps :-)

Esben