# mbj4668/pyang architecture

> Architecture diagram of the GitHub repository mbj4668/pyang, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-08-07). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/mbj4668/pyang
- Repository: https://github.com/mbj4668/pyang

## Overview

##### 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

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% CLI Layer
    subgraph "CLI Layer" 
        direction TB
        CLI_PYANG["pyang CLI"]:::cli
        CLI_JSON2XML["json2xml CLI"]:::cli
        CLI_YANG2HTML["yang2html CLI"]:::cli
        CLI_YANG2DSDL["yang2dsdl CLI"]:::cli
    end

    %% Core Library
    subgraph "Core Library" 
        direction TB
        Context["Context Manager"]:::core
        Repository["Repository (module lookup)"]:::core
        subgraph "Parsers & Lexers"
            direction TB
            Grammar["grammar.py"]:::core
            YangParser["yang_parser.py"]:::core
            YinParser["yin_parser.py"]:::core
            Yacc["yacc.py"]:::core
            XPathLexer["xpath_lexer.py"]:::core
            XPathParser["xpath_parser.py"]:::core
            Syntax["syntax.py"]:::core
        end
        Validation["AST & Statement Validation"]:::core
        PluginMgr["Plugin Manager"]:::core
        ErrorHandler["Error Handling"]:::core
        Util["Utility Functions"]:::core
    end

    %% Plugins & Translators
    subgraph "Plugins & Translators" 
        direction TB
        subgraph "Output Plugins"
            direction TB
            Plugin_Tree["tree.py"]:::plugin
            Plugin_UML["uml.py"]:::plugin
            Plugin_JSTree["jstree.py"]:::plugin
            Plugin_JSONXSL["jsonxsl.py"]:::plugin
            Plugin_RESTCONF["restconf.py"]:::plugin
        end
        subgraph "Translators"
            direction TB
            Trans_YANG["yang.py"]:::plugin
            Trans_YIN["yin.py"]:::plugin
            Trans_DSDL["dsdl.py"]:::plugin
            Trans_Schemanode["schemanode.py"]:::plugin
        end
    end

    %% Resources
    subgraph "Data Repositories & Resources"
        direction TB
        Modules["modules/ (YANG modules)"]:::resource
        XSLT["xslt/ (XSLT stylesheets)"]:::resource
        Schema["schema/ (RELAX-NG schemas)"]:::resource
        UserFiles["User YANG Files"]:::external
    end

    %% Test Harness
    TestHarness["Test Harness"]:::test

    %% Connections
    CLI_PYANG -->|invokes| Context
    CLI_JSON2XML -->|invokes| Context
    CLI_YANG2HTML -->|invokes| Context
    CLI_YANG2DSDL -->|invokes| Context

    Context -->|lookup modules| Repository
    Context -->|parse| Grammar
    Context -->|parse| YangParser
    Context -->|parse| YinParser
    Context -->|parse| Yacc
    Context -->|lexer| XPathLexer
    Context -->|parser| XPathParser
    Context -->|syntax| Syntax

    Grammar -->|AST| Validation
    YangParser --> Validation
    YinParser --> Validation
    Yacc --> Validation
    XPathParser --> Validation
    Syntax --> Validation

    Validation -->|validate| PluginMgr
    PluginMgr -->|load| Plugin_Tree
    PluginMgr -->|load| Plugin_UML
    PluginMgr -->|load| Plugin_JSTree
    PluginMgr -->|load| Plugin_JSONXSL
    PluginMgr -->|load| Plugin_RESTCONF

    PluginMgr -->|invoke| Trans_YANG
    PluginMgr -->|invoke| Trans_YIN
    PluginMgr -->|invoke| Trans_DSDL
    PluginMgr -->|invoke| Trans_Schemanode

    Trans_DSDL -->|XSLT transform| XSLT
    Trans_DSDL -->|schema validation| Schema

    Repository --> Modules
    Context --> UserFiles

    Context -->|error handling| ErrorHandler
    Context --> Util

    TestHarness -->|tests| CLI_PYANG
    TestHarness -->|tests| CLI_JSON2XML
    TestHarness -->|tests| CLI_YANG2HTML
    TestHarness -->|tests| CLI_YANG2DSDL

    %% Click Events CLI
    click CLI_PYANG "https://github.com/mbj4668/pyang/tree/master/bin/pyang"
    click CLI_JSON2XML "https://github.com/mbj4668/pyang/tree/master/bin/json2xml"
    click CLI_YANG2HTML "https://github.com/mbj4668/pyang/tree/master/bin/yang2html"
    click CLI_YANG2DSDL "https://github.com/mbj4668/pyang/tree/master/bin/yang2dsdl"
    click CLI_PYANG "https://github.com/mbj4668/pyang/blob/master/pyang/scripts/pyang_tool.py"
    click CLI_JSON2XML "https://github.com/mbj4668/pyang/blob/master/pyang/scripts/json2xml.py"
    click CLI_YANG2HTML "https://github.com/mbj4668/pyang/blob/master/pyang/scripts/yang2html.py"

    %% Click Events Core
    click Context "https://github.com/mbj4668/pyang/blob/master/pyang/context.py"
    click Repository "https://github.com/mbj4668/pyang/blob/master/pyang/repository.py"
    click Grammar "https://github.com/mbj4668/pyang/blob/master/pyang/grammar.py"
    click YangParser "https://github.com/mbj4668/pyang/blob/master/pyang/yang_parser.py"
    click YinParser "https://github.com/mbj4668/pyang/blob/master/pyang/yin_parser.py"
    click Yacc "https://github.com/mbj4668/pyang/blob/master/pyang/yacc.py"
    click XPathLexer "https://github.com/mbj4668/pyang/blob/master/pyang/xpath_lexer.py"
    click XPathParser "https://github.com/mbj4668/pyang/blob/master/pyang/xpath_parser.py"
    click Syntax "https://github.com/mbj4668/pyang/blob/master/pyang/syntax.py"
    click Validation "https://github.com/mbj4668/pyang/blob/master/pyang/statements.py"
    click Util "https://github.com/mbj4668/pyang/blob/master/pyang/util.py"
    click ErrorHandler "https://github.com/mbj4668/pyang/blob/master/pyang/error.py"
    click PluginMgr "https://github.com/mbj4668/pyang/blob/master/pyang/plugin.py"

    %% Click Events Plugins & Translators
    click Plugin_Tree "https://github.com/mbj4668/pyang/blob/master/pyang/plugins/tree.py"
    click Plugin_UML "https://github.com/mbj4668/pyang/blob/master/pyang/plugins/uml.py"
    click Plugin_JSTree "https://github.com/mbj4668/pyang/blob/master/pyang/plugins/jstree.py"
    click Plugin_JSONXSL "https://github.com/mbj4668/pyang/blob/master/pyang/plugins/jsonxsl.py"
    click Plugin_RESTCONF "https://github.com/mbj4668/pyang/blob/master/pyang/plugins/restconf.py"
    click Trans_YANG "https://github.com/mbj4668/pyang/blob/master/pyang/translators/yang.py"
    click Trans_YIN "https://github.com/mbj4668/pyang/blob/master/pyang/translators/yin.py"
    click Trans_DSDL "https://github.com/mbj4668/pyang/blob/master/pyang/translators/dsdl.py"
    click Trans_Schemanode "https://github.com/mbj4668/pyang/blob/master/pyang/translators/schemanode.py"

    %% Click Events Resources
    click XSLT "https://github.com/mbj4668/pyang/tree/master/xslt/"
    click Schema "https://github.com/mbj4668/pyang/tree/master/schema/"
    click Modules "https://github.com/mbj4668/pyang/tree/master/modules/"

    %% Click Events Test
    click TestHarness "https://github.com/mbj4668/pyang/tree/master/test/"

    %% Styles
    classDef core fill:#c2f0c2,stroke:#2c662d,color:#000;
    classDef plugin fill:#c2d9ff,stroke:#1a3e7c,color:#000;
    classDef resource fill:#fff2cc,stroke:#7a591f,color:#000;
    classDef cli fill:#e0e0e0,stroke:#707070,color:#000;
    classDef test fill:#f2f2f2,stroke:#999999,color:#000;
    classDef external fill:#f9f9f9,stroke:#bbbbbb,color:#333;
```
