X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=doc%2FDocBook.py;h=4803c29d1565363b0a249aafb22ce9a399c4f463;hb=87e18d743c775fc8f3dfb3972e183567d940e7c6;hp=0b279b255bd4109ca75019daf75ed974627fdf7d;hpb=79514a4adcf7b4bd711ab384bc0c7cd11077a9b7;p=plcapi.git diff --git a/doc/DocBook.py b/doc/DocBook.py index 0b279b2..4803c29 100755 --- a/doc/DocBook.py +++ b/doc/DocBook.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python3 # # Generates a DocBook section documenting all PLCAPI methods on # stdout. @@ -6,39 +6,38 @@ # Mark Huang # 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.Method import * -import DocBookLocal +from PLC.Parameter import Parameter, Mixed, xmlrpc_type, python_type -# 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) +# can no longer create elements out of a document +dom = parseString('') - 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): """text""" 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): """text""" @@ -74,75 +73,86 @@ class paramElement(Element): # 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)) -api_func_list = DocBookLocal.get_func_list() -for func in api_func_list: - method = func.name +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())