Setting tag plcapi-7.1-1
[plcapi.git] / doc / DocBook.py
index 0185ab4..4803c29 100755 (executable)
@@ -1,4 +1,4 @@
-#!/usr/bin/python
+#!/usr/bin/env python3
 #
 # Generates a DocBook section documenting all PLCAPI methods on
 # stdout.
@@ -6,41 +6,38 @@
 # Mark Huang <mlhuang@cs.princeton.edu>
 # Copyright (C) 2006 The Trustees of Princeton University
 #
-# $Id: DocBook.py,v 1.3 2006/10/25 19:35:36 mlhuang Exp $
-#
+
+# dec 2018
+# going for python3; xml.dom.minidom has changed a lot
+# working around the changes in a rather quick & dirty way
 
 import xml.dom.minidom
-from xml.dom.minidom import Element, Text
-import codecs
+from xml.dom.minidom import Element, parseString
 
-from PLC.API import PLCAPI
-from PLC.Method import *
+from PLC.Parameter import Parameter, Mixed, xmlrpc_type, python_type
 
-api = PLCAPI(None)
+# can no longer create elements out of a document
+dom = parseString('<dummydoc/>')
 
-# xml.dom.minidom.Text.writexml adds surrounding whitespace to textual
-# data when pretty-printing. Override this behavior.
-class TrimText(Text):
-    """text"""
-    def __init__(self, text = None):
-        self.data = unicode(text)
-
-    def writexml(self, writer, indent="", addindent="", newl=""):
-        Text.writexml(self, writer, "", "", "")
+def text_node(text):
+    global dom
+    return dom.createTextNode(text)
 
-class TrimTextElement(Element):
+class TextElement(Element):
     """<tagName>text</tagName>"""
     def __init__(self, tagName, text = None):
         Element.__init__(self, tagName)
         if text is not None:
-            self.appendChild(TrimText(text))
+            self.appendChild(text_node(text))
 
     def writexml(self, writer, indent="", addindent="", newl=""):
         writer.write(indent)
         Element.writexml(self, writer, "", "", "")
         writer.write(newl)
 
-class simpleElement(TrimTextElement): pass
+class simpleElement(TextElement):
+    def __init__(self, *args, **kwds):
+        super().__init__(*args, **kwds)
 
 class paraElement(simpleElement):
     """<para>text</para>"""
@@ -76,74 +73,86 @@ class paramElement(Element):
         # <listitem>
         Element.__init__(self, 'listitem')
 
-        description = Element('para')
+        global dom
+        description = dom.createElement('para')
 
         if name:
             description.appendChild(simpleElement('parameter', name))
-            description.appendChild(TrimText(": "))
+            description.appendChild(text_node(": "))
 
-        description.appendChild(TrimText(param_type(param)))
+        description.appendChild(text_node(param_type(param)))
 
         if isinstance(param, (list, tuple, set)) and len(param) == 1:
             param = param[0]
 
         if isinstance(param, Parameter):
-            description.appendChild(TrimText(", " + param.doc))
+            description.appendChild(text_node(", " + param.doc))
             param = param.type
 
         self.appendChild(description)
 
         if isinstance(param, dict):
-            itemizedlist = Element('itemizedlist')
+            itemizedlist = dom.createElement('itemizedlist')
             self.appendChild(itemizedlist)
-            for name, subparam in param.iteritems():
+            for name, subparam in param.items():
                 itemizedlist.appendChild(paramElement(name, subparam))
 
         elif isinstance(param, (list, tuple, set)) and len(param):
-            itemizedlist = Element('itemizedlist')
+            itemizedlist = dom.createElement('itemizedlist')
             self.appendChild(itemizedlist)
             for subparam in param:
                 itemizedlist.appendChild(paramElement(None, subparam))
 
-for method in api.methods:
-    func = api.callable(method)
+class DocBook:
 
-    if func.status == "deprecated":
-        continue
+    def __init__ (self, functions_list):
+        self.functions_list = functions_list
 
-    (min_args, max_args, defaults) = func.args()
+    def Process (self):
 
-    section = Element('section')
-    section.setAttribute('id', func.name)
-    section.appendChild(simpleElement('title', func.name))
+        global dom
 
-    prototype = "%s (%s)" % (method, ", ".join(max_args))
-    para = paraElement('Prototype:')
-    para.appendChild(blockquoteElement(prototype))
-    section.appendChild(para)
+        for func in self.functions_list:
+            method = func.name
 
-    para = paraElement('Description:')
-    para.appendChild(blockquoteElement(func.__doc__))
-    section.appendChild(para)
+            if func.status == "deprecated":
+                continue
 
-    para = paraElement('Allowed Roles:')
-    para.appendChild(blockquoteElement(", ".join(func.roles)))
-    section.appendChild(para)
+            (min_args, max_args, defaults) = func.args()
 
-    section.appendChild(paraElement('Parameters:'))
-    params = Element('itemizedlist')
-    if func.accepts:
-        for name, param, default in zip(max_args, func.accepts, defaults):
-            params.appendChild(paramElement(name, param))
-    else:
-        listitem = Element('listitem')
-        listitem.appendChild(paraElement('None'))
-        params.appendChild(listitem)
-    section.appendChild(params)
+            # with python3 it looks like an element can't be sfa_created
+            # outside of a document
+            section = dom.createElement('section')
+            section.setAttribute('id', func.name)
+            section.appendChild(simpleElement('title', func.name))
+
+            prototype = "%s (%s)" % (method, ", ".join(max_args))
+            para = paraElement('Prototype:')
+            para.appendChild(blockquoteElement(prototype))
+            section.appendChild(para)
+
+            para = paraElement('Description:')
+            para.appendChild(blockquoteElement(func.__doc__))
+            section.appendChild(para)
+
+            para = paraElement('Allowed Roles:')
+            para.appendChild(blockquoteElement(", ".join(func.roles)))
+            section.appendChild(para)
+
+            section.appendChild(paraElement('Parameters:'))
+            params = Element('itemizedlist')
+            if func.accepts:
+                for name, param, default in zip(max_args, func.accepts, defaults):
+                    params.appendChild(paramElement(name, param))
+            else:
+                listitem = Element('listitem')
+                listitem.appendChild(paraElement('None'))
+                params.appendChild(listitem)
+            section.appendChild(params)
 
-    section.appendChild(paraElement('Returns:'))
-    returns = Element('itemizedlist')
-    returns.appendChild(paramElement(None, func.returns))
-    section.appendChild(returns)
+            section.appendChild(paraElement('Returns:'))
+            returns = dom.createElement('itemizedlist')
+            returns.appendChild(paramElement(None, func.returns))
+            section.appendChild(returns)
 
-    print section.toprettyxml(encoding = "UTF-8")
+            print(section.toxml())