#!/usr/bin/python

# import stdlib
import os
import argparse

# third party libs
import yaml

# import iosxr_eznc modules
from iosxr_eznc.namespaces import Namespaces


NAMESPACES_YAML = 'namespaces.yml'


def run():

    usage = """prog <path>
Builds the Namespaces YAML file usign the YANG models found in <path>"""

    argparser = argparse.ArgumentParser(usage=usage)
    argparser.add_argument('path', help='Path to the Cisco YANG models')

    args = argparser.parse_args()

    ns_mapper = Namespaces()
    ns_mapper.init_pyang_context()

    for file in os.listdir(args.path):
        filepath = os.path.join(args.path, file)
        if os.path.isfile(filepath) and file.endswith('.yang'):
            raw_yang_module = open(filepath).read()
            ns_mapper.yang_register(file, raw_yang_module)

    default_namespaces = ns_mapper.get()

    with open(NAMESPACES_YAML, 'w') as ns_file:
        ns_file.write(
            yaml.dump(
                default_namespaces,
                default_flow_style=False
            )
        )

if __name__ == '__main__':
    run()
