2 # Copyright (c) 2020. Vinci Consulting Corp. All Rights Reserved.
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
8 # http://www.apache.org/licenses/LICENSE-2.0
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.
23 class ContentRenderer:
24 def __init__(self, ws_root, output_dir):
25 self.ws_root = ws_root
26 self.output_dir = output_dir
29 return os.path.join(self.ws_root, "src/plugins")
32 raise NotImplementedError
35 class PluginRenderer(ContentRenderer):
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'):
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))
50 output_file.write(f"* {entry.name} - {description}\n")
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)
63 renderers = [PluginRenderer]
67 if len(sys.argv) != 3:
68 print("You need to pass WS_ROOT and OUTPUT_DIR")
71 print("rendering dynamic includes...")
72 for renderer in renderers:
73 renderer(*sys.argv[1:]).render()
77 if __name__ == "__main__":