How mbj4668/pyang works
Project type and purpose
• pyang is an open-source CLI tool and Python library for validating, transforming, and generating code from YANG modules.
• It is not a web or distributed system but a locally executed command-line application with a plugin architecture.
File structure
• Top-level:
• bin/ – entry-point scripts (json2xml, pyang, yang2dsdl, yang2html)
• pyang/ – core library code (context, parser, grammar, statements, plugin API)
• pyang/plugins/ – built-in plugins for output formats (tree, UML, jstree, etc.)
• pyang/translators/ – translation modules (YANG→YIN, DSDL, etc.)
• xslt/, schema/ – external resources for XML/RELAX-NG/Schematron transformations
• modules/ – bundled YANG module repository
• docs/, man/, etc. – documentation and man pages
• test/ – comprehensive test suite across feature areas
• Config files: setup.py/install config; dev-requirements.txt; .pylintrc; CI workflow.
README for additional insights
• Key components described under “Code structure”
• Emphasis on plugin framework and translators
• Uses Python virtual environment, environment variables for resource discovery
a. Frontend: CLI scripts in bin/ (pyang, json2xml, yang2html, yang2dsdl)
b. Core Library (“Pyang Core”):
• Context manager (parsing sessions)
• Repository (YANG module lookup)
• Parsers (YANG lexer, grammar, syntax modules)
• AST and Statement validation
• Plugin Manager (loads pyang/plugins/ and translators/)
c. Plugins & Translators:
• Output plugins (tree, UML, JSON, RESTCONF, sample XML)
• Translator modules (YANG→YIN, YIN→YANG, DSDL schema generation)
• XSLT pipeline using xslt/ stylesheets and schema/ libraries
d. Data Repositories & Resources:
• modules/ directory (standard YANG modules)
• xslt/ and schema/ for XML/DSDL transformations
• External file system (user YANG files, include paths via YANG_MODPATH)
e. External Dependencies:
• Python runtime and standard libraries
• Expat (for XML parsing in yin_parser)
• XSLT engine (e.g., lxml or external xslt processor)
Relationships and interactions
• User invokes bin/pyang → CLI parser
• CLI parser constructs a Context → loads modules from modules/ and user paths via Repository
• Context invokes the YANG lexer/parser → builds AST
• AST fed into Statement validator → validation errors/warnings
• Plugin Manager loads requested plugins/translators
• For format conversion: core calls translator modules or spawns XSLT pipeline (with xslt/ + schema/)
• Plugins may generate UML, tree, JSON, HTML outputs
• Final output written to stdout or files
Architectural patterns and design principles
• Modular, layered architecture:
• CLI layer
• Core parsing/validation layer
• Plugin/translator extension layer
• Resource layer (modules, xslt, schema)
• Plugin framework (inversion of control, dynamic loading)
• Separation of concerns: parsing, validation, transformation, output