def output_child(self, ctx, child, output_writer, parent_deviated=False, override_flag=None): deviated = getattr(child, "i_this_not_supported", False) \ or parent_deviated # Keys map to self.__field_names for CSV output output_content = { "xpath": statements.get_xpath(child, prefix_to_module=True) } # Sometimes we won't have the full set of YANG models... # Handle whether to error out or just set as "nil" for primitive type try: primitive_type = statements.get_primitive_type(child) or "nil" except Exception as e: if ctx.opts.ignore_no_primitive: primitive_type = "nil" else: raise e # To handle inputs and outputs we're going to have an override flag. # input children should flag as w all the way through. flag, override_flag = ((override_flag, override_flag) if override_flag else self.get_flag(child)) # Set the output content based on the options specified if ctx.opts.flatten_keyword: output_content["keyword"] = child.keyword if ctx.opts.flatten_type: output_content["type"] = statements.get_qualified_type(child) \ or "nil" if ctx.opts.flatten_primitive_type: output_content["primitive_type"] = primitive_type if ctx.opts.flatten_flag: output_content["flag"] = flag if ctx.opts.flatten_description: output_content["description"] = statements.get_description(child) if ctx.opts.flatten_deviated: output_content["deviated"] = "deviated" if deviated else "present" if set(output_content.keys()) != self.__field_names_set: raise Exception("Output keys do not match CSV field names!") # Filters are specified as a positive in the command line arguments # In this case we're negating compared to what we want to output # Final statement: Always ignore input/output, children will be printed. output_filters = set([ ctx.opts.flatten_filter_keyword and child.keyword not in ctx.opts.flatten_filter_keyword, ctx.opts.flatten_filter_primitive and primitive_type not in ctx.opts.flatten_filter_primitive, ctx.opts.flatten_filter_flag and flag != ctx.opts.flatten_filter_flag, child.keyword in {"input", "output"}, ]) if not any(output_filters): # We want to traverse the entire tree for output # Simply don't output what we don't want, don't stop processing output_writer.writerow(output_content) if hasattr(child, "i_children"): self.output_module(ctx, child, output_writer, deviated, override_flag)
def output_module( self, ctx, module, output_writer, parent_deviated=False, override_flag=None, known_keys=None, ): module_children = [ child for child in getattr(module, "i_children", []) if child.keyword in self.__keywords ] if ctx.opts.flatten_deviated: deviated_module_children = [ child for child in getattr(module, "i_not_supported", []) if child.keyword in self.__keywords ] module_children = module_children + deviated_module_children module_children = sorted( module_children, key=lambda child: statements.get_xpath( child, prefix_to_module=(not ctx.opts.flatten_prefix_in_xpath), qualified=ctx.opts.flatten_qualified_in_xpath, ), ) for child in module_children: self.output_child( ctx, child, output_writer, parent_deviated, override_flag, known_keys, )
def output_child( self, ctx, child, output_writer, parent_deviated=False, override_flag=None, known_keys=None, ): deviated = (getattr(child, "i_this_not_supported", False) or parent_deviated) # Keys map to self.__field_names for CSV output output_content = { "xpath": statements.get_xpath( child, prefix_to_module=(not ctx.opts.flatten_prefix_in_xpath), qualified=ctx.opts.flatten_qualified_in_xpath, with_keys=ctx.opts.flatten_keys_in_xpath, ) } # Sometimes we won't have the full set of YANG models... # Handle whether to error out or just set as "nil" for primitive type try: primitive_type = statements.get_primitive_type(child) or "nil" except Exception as e: if ctx.opts.ignore_no_primitive: primitive_type = "nil" else: raise e # To handle inputs and outputs we're going to have an override flag. # input children should flag as w all the way through. flag, override_flag = ((override_flag, override_flag) if override_flag else self.get_flag(child)) child_keys = set(statements.get_keys(child)) # Set the output content based on the options specified if ctx.opts.flatten_keyword: output_content["keyword"] = child.keyword if ctx.opts.flatten_type: output_content["type"] = (statements.get_qualified_type(child) or "nil") if ctx.opts.flatten_primitive_type: output_content["primitive_type"] = primitive_type if ctx.opts.flatten_flag: output_content["flag"] = flag if ctx.opts.flatten_description: output_content["description"] = statements.get_description(child) if ctx.opts.flatten_keys: if not known_keys: output_content["key"] = None else: child_name = child.arg output_content["key"] = ("key" if child_name in known_keys else None) if ctx.opts.flatten_deviated: output_content["deviated"] = "deviated" if deviated else "present" if ctx.opts.flatten_qualified_module_and_prefix_path: output_content["mod_prefix_path"] = self.get_mod_prefix_path( child, ctx.opts.flatten_keys_in_xpath) if ctx.opts.flatten_status: # If no status is specified, the default is "current". status = "current" status_statement = child.search_one("status") if status_statement is not None: status = status_statement.arg output_content["status"] = status if ctx.opts.flatten_resolve_leafref: if primitive_type == "leafref": output_content["resolved_leafref"] = statements.get_xpath( child.i_leafref.i_target_node, prefix_to_module=(not ctx.opts.flatten_prefix_in_xpath), qualified=ctx.opts.flatten_qualified_in_xpath, with_keys=ctx.opts.flatten_keys_in_xpath, ) else: output_content["resolved_leafref"] = None if set(output_content.keys()) != self.__field_names_set: raise Exception("Output keys do not match CSV field names!") # Filters are specified as a positive in the command line arguments # In this case we're negating compared to what we want to output # Final statement: Always ignore input/output, children will be printed. output_filters = set([ ctx.opts.flatten_filter_keyword and child.keyword not in ctx.opts.flatten_filter_keyword, ctx.opts.flatten_filter_primitive and primitive_type not in ctx.opts.flatten_filter_primitive, ctx.opts.flatten_filter_flag and flag != ctx.opts.flatten_filter_flag, child.keyword in {"input", "output"}, ]) if not any(output_filters): # We want to traverse the entire tree for output # Simply don't output what we don't want, don't stop processing output_writer.writerow(output_content) if hasattr(child, "i_children"): self.output_module(ctx, child, output_writer, deviated, override_flag, child_keys)