xref: /linux/tools/lib/python/kdoc/kdoc_item.py (revision e394855fcc897f73f23c364a3a596b54cc879e4c)
1# SPDX-License-Identifier: GPL-2.0
2#
3# A class that will, eventually, encapsulate all of the parsed data that we
4# then pass into the output modules.
5#
6
7"""
8Data class to store a kernel-doc Item.
9"""
10
11class KdocItem:
12    """
13    A class that will, eventually, encapsulate all of the parsed data that we
14    then pass into the output modules.
15    """
16
17    def __init__(self, name, fname, type, start_line, **other_stuff):
18        self.name = name
19        self.fname = fname
20        self.type = type
21        self.declaration_start_line = start_line
22        self.sections = {}
23        self.sections_start_lines = {}
24        self.parameterlist = []
25        self.parameterdesc_start_lines = {}
26        self.parameterdescs = {}
27        self.parametertypes = {}
28
29        self.warnings = []
30
31        #
32        # Just save everything else into our own dict so that the output
33        # side can grab it directly as before.  As we move things into more
34        # structured data, this will, hopefully, fade away.
35        #
36        known_keys = {
37            'declaration_start_line',
38            'sections',
39            'sections_start_lines',
40            'parameterlist',
41            'parameterdesc_start_lines',
42            'parameterdescs',
43            'parametertypes',
44            'warnings',
45        }
46
47        self.other_stuff = {}
48        for k, v in other_stuff.items():
49            if k in known_keys:
50                setattr(self, k, v)           # real attribute
51            else:
52                self.other_stuff[k] = v
53
54    def get(self, key, default = None):
55        """
56        Get a value from optional keys.
57        """
58        return self.other_stuff.get(key, default)
59
60    def __getitem__(self, key):
61        return self.get(key)
62
63    @classmethod
64    def from_dict(cls, d):
65        """Create a KdocItem from a plain dict."""
66
67        cp = d.copy()
68        name        = cp.pop('name', None)
69        fname       = cp.pop('fname', None)
70        type        = cp.pop('type', None)
71        start_line  = cp.pop('start_line', 1)
72        other_stuff = cp.pop('other_stuff', {})
73
74        # Everything that’s left goes straight to __init__
75        return cls(name, fname, type, start_line, **cp, **other_stuff)
76
77    #
78    # Tracking of section and parameter information.
79    #
80    def set_sections(self, sections, start_lines):
81        """
82        Set sections and start lines.
83        """
84        self.sections = sections
85        self.sections_start_lines = start_lines
86
87    def set_params(self, names, descs, types, starts):
88        """
89        Set parameter list: names, descriptions, types and start lines.
90        """
91        self.parameterlist = names
92        self.parameterdescs = descs
93        self.parametertypes = types
94        self.parameterdesc_start_lines = starts
95