def parse(text, ctx=None): """Parse a YANG statement into an Abstract Syntax subtree. Arguments: text (str): file name for a YANG module or text ctx (optional pyang.Context): context used to validate text Returns: pyang.statements.Statement: Abstract syntax subtree Note: The ``parse`` function can be used to parse small amounts of text. If yout plan to parse an entire YANG (sub)module, please use instead:: ast = ctx.add_module(module_name, text_contet) It is also well known that ``parse`` function cannot solve YANG deviations yet. """ parser = YangParser() filename = 'parser-input' ctx_ = ctx or create_context() if isfile(text): filename = text with open(filename, 'r') as fp: text = fp.read() # ensure reported errors are just from parsing old_errors = ctx_.errors ctx_.errors = [] ast = parser.parse(ctx_, filename, text) # look for errors and warnings check(ctx_) # restore other errors ctx_.errors = old_errors return ast
'children': recurse_children(child, module) } return parsed_children # Let's begin.. base_data_dir = 'testdata/' model_filenames = ['Cisco-IOS-XR-ipv4-bgp-oper.yang'] # Context has a dependency on Repository # Repository is abstract, only FileRepository exists file_repository = FileRepository(path=base_data_dir, use_env=False, no_path_recurse=False) # Contexts are like the base controller for parsing in pyang context = Context(repository=file_repository) parser = YangParser() # YangParser has a dependency on Context # Context also has a dependency on YangParser # Parse all the models in to modules modules = [] for filename in model_filenames: model_file_path = os.path.join(base_data_dir, filename) model_data = None with open(model_file_path, 'r') as model_fd: model_data = model_fd.read() # Some model filenames indicate revision, etc. filename_attributes = syntax.re_filename.search(filename) module = None if filename_attributes is None: module = context.add_module(filename, model_data) else: