def parse_repository(repository_path, filter_pattern=''): """Parse a directory as a Repository of YANG modules.""" file_repository = FileRepository( path=repository_path, use_env=False, no_path_recurse=False, ) context = Context(repository=file_repository) context.validate() module_revs = context.revs logging.debug('Found %d module(s) in %s.', len(module_revs.keys()), repository_path) if filter_pattern: module_revs = get_filtered_modules(context, filter_pattern) logging.debug('Filtered to %d module(s).', len(module_revs.keys())) modules = parse_modules(context) return modules
def create_context(path='.', *options, **kwargs): """Generates a pyang context Arguments: path (str): location of YANG modules. *options: list of dicts, with options to be passed to context. **kwargs: similar to ``options`` but have a higher precedence. Returns: pyang.Context: Context object for ``pyang`` usage """ opts = objectify(DEFAULT_OPTIONS, *options, **kwargs) repo = FileRepository(path, no_path_recurse=opts.no_path_recurse) ctx = Context(repo) ctx.opts = opts return ctx
def create_context(path='.', *options, **kwargs): """Generates a pyang context. The dict options and keyword arguments are similar to the command line options for ``pyang``. For ``plugindir`` use env var ``PYANG_PLUGINPATH``. For ``path`` option use the argument with the same name, or ``PYANG_MODPATH`` env var. Arguments: path (str): location of YANG modules. (Join string with ``os.pathsep`` for multiple locations). Default is the current working dir. *options: list of dicts, with options to be passed to context. See bellow. **kwargs: similar to ``options`` but have a higher precedence. See bellow. Keyword Arguments: print_error_code (bool): On errors, print the error code instead of the error message. Default ``False``. warnings (list): If contains ``error``, treat all warnings as errors, except any other error code in the list. If contains ``none``, do not report any warning. errors (list): Treat each error code container as an error. ignore_error_tags (list): Ignore error code. (For a list of error codes see ``pyang --list-errors``). ignore_errors (bool): Ignore all errors. Default ``False``. canonical (bool): Validate the module(s) according to the canonical YANG order. Default ``False``. yang_canonical (bool): Print YANG statements according to the canonical order. Default ``False``. yang_remove_unused_imports (bool): Remove unused import statements when printing YANG. Default ``False``. trim_yin (bool): In YIN input modules, trim whitespace in textual arguments. Default ``False``. lax_xpath_checks (bool): Lax check of XPath expressions. Default ``False``. strict (bool): Force strict YANG compliance. Default ``False``. max_line_len (int): Maximum line length allowed. Disabled by default. max_identifier_len (int): Maximum identifier length allowed. Disabled by default. features (list): Features to support, default all. Format ``<modname>:[<feature>,]*``. keep_comments (bool): Do not discard comments. Default ``True``. no_path_recurse (bool): Do not recurse into directories in the yang path. Default ``False``. Returns: pyang.Context: Context object for ``pyang`` usage """ # deviations (list): Deviation module (NOT CURRENTLY WORKING). opts = objectify(DEFAULT_OPTIONS, *options, **kwargs) repo = FileRepository(path, no_path_recurse=opts.no_path_recurse) ctx = Context(repo) ctx.opts = opts for attr in _COPY_OPTIONS: setattr(ctx, attr, getattr(opts, attr)) # make a map of features to support, per module (taken from pyang bin) for feature_name in opts.features: (module_name, features) = _parse_features_string(feature_name) ctx.features[module_name] = features # apply deviations (taken from pyang bin) for file_name in opts.deviations: with io.open(file_name, "r", encoding="utf-8") as fd: module = ctx.add_module(file_name, fd.read()) if module is not None: ctx.deviation_modules.append(module) return ctx
'cisco_xpath': cisco_xpath, 'type': get_fq_type(child), 'primitive_type': get_primitive_type(child), 'description': get_description(child), '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)