docs: better docs, mv doxygen to sphinx
[vpp.git] / docs / _scripts / includes_renderer.py
1 #!/usr/bin/env python3
2 #  Copyright (c) 2020. Vinci Consulting Corp. All Rights Reserved.
3 #
4 #  Licensed under the Apache License, Version 2.0 (the "License");
5 #  you may not use this file except in compliance with the License.
6 #  You may obtain a copy of the License at
7 #
8 #      http://www.apache.org/licenses/LICENSE-2.0
9 #
10 #  Unless required by applicable law or agreed to in writing, software
11 #  distributed under the License is distributed on an "AS IS" BASIS,
12 #  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 #  See the License for the specific language governing permissions and
14 #  limitations under the License.
15
16 import glob
17 import inspect
18 import os
19 import re
20 import sys
21
22
23 class ContentRenderer:
24     def __init__(self, ws_root, output_dir):
25         self.ws_root = ws_root
26         self.output_dir = output_dir
27
28     def plugin_dir(self):
29         return os.path.join(self.ws_root, "src/plugins")
30
31     def render(self):
32         raise NotImplementedError
33
34
35 class PluginRenderer(ContentRenderer):
36
37     def _render_entry(self, output_file, entry):
38         description = "<no-description-found>"
39         # we use glob because a plugin can (ioam for now)
40         # define the plugin definition in
41         # a further subdirectory.
42         path = os.path.join(self.plugin_dir(), entry.name, '**')
43         for f in glob.iglob(path, recursive=True):
44             if not f.endswith('.c'):
45                 continue
46             with open(f, "r", encoding="utf-8") as src:
47                 for match in self.regex.finditer(src.read()):
48                     description = "%s" % (match.group(1))
49
50         output_file.write(f"* {entry.name} - {description}\n")
51
52     def render(self):
53         pattern = r'VLIB_PLUGIN_REGISTER\s?\(\)\s*=\s*{.*\.description\s?=\s?"([^"]*)".*};'  # noqa: 501
54         self.regex = re.compile(pattern, re.MULTILINE | re.DOTALL)
55         fname = os.path.join(self.output_dir, "plugin_list.inc")
56         with open(fname, "w") as output_file:
57             with os.scandir(self.plugin_dir()) as pdir:
58                 for entry in sorted(pdir, key=lambda entry: entry.name):
59                     if not entry.name.startswith('.') and entry.is_dir():
60                         self._render_entry(output_file, entry)
61
62
63 renderers = [PluginRenderer]
64
65
66 def main():
67     if len(sys.argv) != 3:
68         print("You need to pass WS_ROOT and OUTPUT_DIR")
69         exit(1)
70
71     print("rendering dynamic includes...")
72     for renderer in renderers:
73         renderer(*sys.argv[1:]).render()
74     print("done.")
75
76
77 if __name__ == "__main__":
78     main()