Path: blob/master/tools/lib/python/kdoc/kdoc_yaml_file.py
170954 views
#!/usr/bin/env python31# SPDX-License-Identifier: GPL-2.02# Copyright(c) 2026: Mauro Carvalho Chehab <[email protected]>.34import os56from kdoc.kdoc_output import ManFormat, RestFormat789class KDocTestFile():10"""11Handles the logic needed to store kernel‑doc output inside a YAML file.12Useful for unit tests and regression tests.13"""1415def __init__(self, config, yaml_file, yaml_content):16#17# Bail out early if yaml is not available18#19try:20import yaml21except ImportError:22sys.exit("Warning: yaml package not available. Aborting it.")2324self.config = config25self.test_file = os.path.expanduser(yaml_file)26self.yaml_content = yaml_content27self.test_names = set()2829self.tests = []3031out_dir = os.path.dirname(self.test_file)32if out_dir and not os.path.isdir(out_dir):33sys.exit(f"Directory {out_dir} doesn't exist.")3435self.out_style = []3637if "man" in self.yaml_content:38out_style = ManFormat()39out_style.set_config(self.config)4041self.out_style.append(out_style)4243if "rst" in self.yaml_content:44out_style = RestFormat()45out_style.set_config(self.config)4647self.out_style.append(out_style)4849def set_filter(self, export, internal, symbol, nosymbol,50function_table, enable_lineno, no_doc_sections):51"""52Set filters at the output classes.53"""54for out_style in self.out_style:55out_style.set_filter(export, internal, symbol,56nosymbol, function_table,57enable_lineno, no_doc_sections)5859@staticmethod60def get_kdoc_item(arg, start_line=1):6162d = vars(arg)6364declaration_start_line = d.get("declaration_start_line")65if not declaration_start_line:66return d6768d["declaration_start_line"] = start_line6970parameterdesc_start_lines = d.get("parameterdesc_start_lines")71if parameterdesc_start_lines:72for key in parameterdesc_start_lines:73ln = parameterdesc_start_lines[key]74ln += start_line - declaration_start_line7576parameterdesc_start_lines[key] = ln7778sections_start_lines = d.get("sections_start_lines")79if sections_start_lines:80for key in sections_start_lines:81ln = sections_start_lines[key]82ln += start_line - declaration_start_line8384sections_start_lines[key] = ln8586return d8788def output_symbols(self, fname, symbols):89"""90Store source, symbols and output strings at self.tests.91"""9293#94# KdocItem needs to be converted into dicts95#96kdoc_item = []97expected = []9899#100# Source code didn't produce any symbol101#102if not symbols:103return104105expected_dict = {}106start_line=1107108for arg in symbols:109source = arg.get("source", "")110111if arg and "KdocItem" in self.yaml_content:112msg = self.get_kdoc_item(arg)113114other_stuff = msg.get("other_stuff", {})115if "source" in other_stuff:116del other_stuff["source"]117118expected_dict["kdoc_item"] = msg119120base_name = arg.name121if not base_name:122base_name = fname123base_name = base_name.lower().replace(".", "_").replace("/", "_")124125126# Don't add duplicated names127i = 0128name = base_name129while name in self.test_names:130i += 1131name = f"{base_name}_{i:03d}"132133self.test_names.add(name)134135for out_style in self.out_style:136if isinstance(out_style, ManFormat):137key = "man"138else:139key = "rst"140141expected_dict[key]= out_style.output_symbols(fname, [arg]).strip()142143test = {144"name": name,145"description": f"{fname} line {arg.declaration_start_line}",146"fname": fname,147"source": source,148"expected": [expected_dict]149}150151self.tests.append(test)152153expected_dict = {}154155def write(self):156"""157Output the content of self.tests to self.test_file.158"""159import yaml160161# Helper function to better handle multilines162def str_presenter(dumper, data):163if "\n" in data:164return dumper.represent_scalar("tag:yaml.org,2002:str", data, style="|")165166return dumper.represent_scalar("tag:yaml.org,2002:str", data)167168# Register the representer169yaml.add_representer(str, str_presenter)170171data = {"tests": self.tests}172173with open(self.test_file, "w", encoding="utf-8") as fp:174yaml.dump(data, fp,175sort_keys=False, width=120, indent=2,176default_flow_style=False, allow_unicode=True,177explicit_start=False, explicit_end=False)178179180