Source

protocol-example / document.py

Full commit

from string import Template
from protocols import load_protocols

message_tpl = Template("""<table><tr><th>${name}</th><th>Marker byte</th><td>${marker}</td</tr>
<tr><td colspan=3>${desc}</td></tr>
<tr><th>Token name</th><th>Type</th><th>Width</th></tr>""")
token_tpl = Template("""<tr><td>${name}</td><td>${jtype}</td><td>${size}</td></tr>""")

protocol_tpl = Template("""<html>
<head><title>Protocol: ${name}</title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>

<table>
<tr><th >${name}</th></tr>
<tr style='margin.bottom: 10px'><td >${doc}</td></tr>
</table>
${messages}
</body></html>""")

def documentation(path):
    """Generate html documentation for protocols"""
    protocols = load_protocols(".").values()
    for protocol in protocols:
        messages = []
        
        for message in sorted(protocol.messages(), key=lambda x: x.type):
            messages.append(message_tpl.substitute(name=message.name, 
                                                   desc=message.__doc__,
                                                   marker=message.type))
            
            for token in message.tokens:
                messages.append(token_tpl.substitute(name=token.name, 
                                                   jtype=token.javaname, 
                                                   size=token.pattern_size))
            messages.append("</table>")
        out = protocol_tpl.substitute(name=protocol.version, 
                                      doc=protocol.__doc__,
                                      messages = "\n".join(messages))
        
        open("protocol%s.html" % (protocol.version,), 'w').write(out)
        
            
if __name__ == "__main__":
    documentation(".")