misc: fix python sonarcloud BLOCKER level issues
[vpp.git] / doxygen / siphon / process.py
index c7f8f1a..ce70be5 100644 (file)
 
 # Generation template class
 
-import logging, os,sys, cgi, json, jinja2, HTMLParser
+import html.parser
+import json
+import logging
+import os
+import sys
+
+import jinja2
 
 # Classes register themselves in this dictionary
 """Mapping of known processors to their classes"""
 siphons = {}
 
+"""Mapping of known output formats to their classes"""
+formats = {}
+
 
-"""Generate rendered output for siphoned data."""
 class Siphon(object):
+    """Generate rendered output for siphoned data."""
 
     # Set by subclasses
     """Our siphon name"""
@@ -51,29 +60,37 @@ class Siphon(object):
     """Template environment, if we're using templates"""
     _tplenv = None
 
-    def __init__(self, template_directory=None):
+    def __init__(self, template_directory, format):
         super(Siphon, self).__init__()
         self.log = logging.getLogger("siphon.process.%s" % self.name)
 
-        if template_directory is not None:
-          self.template_directory = template_directory
-          searchpath = [
-              template_directory + "/" + self.name,
-              template_directory + "/" + "default",
-          ]
-          loader = jinja2.FileSystemLoader(searchpath=searchpath)
-          self._tplenv = jinja2.Environment(
-              loader=loader,
-              trim_blocks=True,
-              keep_trailing_newline=True)
-
-          # Convenience, get a reference to the internal escape and
-          # unescape methods in cgi and HTMLParser. These then become
-          # available to templates to use, if needed.
-          self._h = HTMLParser.HTMLParser()
-          self.escape = cgi.escape
-          self.unescape = self._h.unescape
-
+        # Get our output format details
+        fmt_klass = formats[format]
+        fmt = fmt_klass()
+        self._format = fmt
+
+        # Sort out the template search path
+        def _tpldir(name):
+            return os.sep.join((template_directory, fmt.name, name))
+
+        self.template_directory = template_directory
+        searchpath = [
+            _tpldir(self.name),
+            _tpldir("default"),
+        ]
+        loader = jinja2.FileSystemLoader(searchpath=searchpath)
+        self._tplenv = jinja2.Environment(
+            loader=loader,
+            trim_blocks=True,
+            autoescape=True,
+            keep_trailing_newline=True)
+
+        # Convenience, get a reference to the internal escape and
+        # unescape methods in html.parser. These then become
+        # available to templates to use, if needed.
+        self._h = html.parser.HTMLParser()
+        self.escape = html.escape
+        self.unescape = html.unescape
 
     # Output renderers
 
@@ -157,12 +174,11 @@ class Siphon(object):
 
     """Template processor"""
     def template(self, name, **kwargs):
-      tpl = self._tplenv.get_template(name + ".md")
-      return tpl.render(
+        tpl = self._tplenv.get_template(name + self._format.extension)
+        return tpl.render(
             this=self,
             **kwargs)
 
-
     # Processing methods
 
     """Parse the input file into a more usable dictionary structure."""
@@ -185,11 +201,11 @@ class Siphon(object):
             for item in data["items"]:
                 try:
                     o = self._parser.parse(item['block'])
-                except:
-                    self.log.error("Exception parsing item: %s\n%s" \
-                            % (json.dumps(item, separators=(',', ': '),
-                                indent=4),
-                                item['block']))
+                except Exception:
+                    self.log.error("Exception parsing item: %s\n%s"
+                                   % (json.dumps(item, separators=(',', ': '),
+                                                 indent=4),
+                                      item['block']))
                     raise
 
                 # Augment the item with metadata
@@ -236,8 +252,8 @@ class Siphon(object):
             if group.startswith('_'):
                 continue
 
-            self.log.info("Processing items in group \"%s\" (%s)." % \
-                (group, group_sort_key(group)))
+            self.log.info("Processing items in group \"%s\" (%s)." %
+                          (group, group_sort_key(group)))
 
             # Generate the section index entry (write it now)
             out.write(self.index_section(group))
@@ -249,14 +265,15 @@ class Siphon(object):
                 return self.item_sort_key(self._cmds[group][key])
 
             for key in sorted(self._cmds[group].keys(), key=item_sort_key):
-                self.log.debug("--- Processing key \"%s\" (%s)." % \
-                    (key, item_sort_key(key)))
+                self.log.debug("--- Processing key \"%s\" (%s)." %
+                               (key, item_sort_key(key)))
 
                 o = self._cmds[group][key]
                 meta = {
                     "directory": o['meta']['directory'],
                     "file": o['meta']['file'],
                     "macro": o['macro'],
+                    "name": o['name'],
                     "key": key,
                     "label": self.item_label(group, key),
                 }
@@ -269,3 +286,33 @@ class Siphon(object):
 
         # Deliver the accumulated body output
         out.write(contents)
+
+
+class Format(object):
+    """Output format class"""
+
+    """Name of this output format"""
+    name = None
+
+    """Expected file extension of templates that build this format"""
+    extension = None
+
+
+class FormatMarkdown(Format):
+    """Markdown output format"""
+    name = "markdown"
+    extension = ".md"
+
+
+# Register 'markdown'
+formats["markdown"] = FormatMarkdown
+
+
+class FormatItemlist(Format):
+    """Itemlist output format"""
+    name = "itemlist"
+    extension = ".itemlist"
+
+
+# Register 'itemlist'
+formats["itemlist"] = FormatItemlist