Interactive/Documentation.py

##
## (C) Copyright 2008 Autodesk, Inc.  All rights reserved.
##
## Use of this software is subject to the terms of the Autodesk license
## agreement provided at the time of installation or download, or which
## otherwise accompanies this software in either electronic or hard copy
## form.
##

# From the bin directory, "Showcase -script FullPath/Documentation.py"
# This will save the extracted message documentation to
# C:/AutodeskShowcaseMessages.html

import string

from ScriptRunner        import ScriptRunner, ResponseMessageRegistry
from Message             import Message
from MessageRegistry     import theMessageRegistry

class MessageDocumentation( ScriptRunner ):
    def __init__( self, scriptName, args ):
        ScriptRunner.__init__( self, scriptName, args )

    def Main( self ):
        """
        This will save documentation to C:/AutodeskShowcaseMessages.html
        """

        htmlText = '<html>\n' 

        ids = sorted(theMessageRegistry.ids())

        htmlText += '<A href="#Responses">Response Messages</A><br><br>\n'
        for first in string.ascii_uppercase:
            htmlText += '<A href="#Letter%s">%s</A>\n' % (first,first)
        htmlText += "<br><br>\n"

        parHeader = '''
        <table border="0" cellpadding="0" cellspacing="0">
        <tr><td><b>%s:</b></td><td colspan="2">&nbsp;
        </td></tr>''' % (_("Parameters"))

        first = ''
        for k in ids:
            # Skip the internal ones
            if theMessageRegistry.hasProperty( k, Message.kInternal ):
                continue

            (desc,pars,cats) = ['Undocumented.', [['unknown','unknown']],()]
            doc = theMessageRegistry.documentation(k)
            fullcats = ''
            if doc:
                if len(doc) == 3:
                    (desc, pars, cats) = doc
                    if cats and len(cats) > 0:
                        fullcats = '&nbsp;<font size="-1">(%s)</font>' % ' '.join(cats)
                elif len(doc) == 2:
                    (desc, pars) = doc

            if first != k[0]:
                first = k[0]
                htmlText += '<A name="Letter%s">&nbsp;</A><br>\n' % (first)

            # Show all commands, even if they have invalid documentation
            if len(pars) == len(theMessageRegistry.dataType(k)):
                htmlText += '<font size="+1"><b>%s</b></font>%s\n' % ( k,fullcats )
            else:
                htmlText += '<font size="+1" color="red"><b>%s</b></font>%s\n' % ( k,fullcats )
            properties = ''
            if theMessageRegistry.hasProperty( k, Message.kUndoable ) and \
               theMessageRegistry.hasProperty( k, Message.kBroadcastable ):
                properties = _('This message is broadcastable and undoable.')
            elif theMessageRegistry.hasProperty( k, Message.kBroadcastable ):
                properties = _('This message is broadcastable.')
            elif theMessageRegistry.hasProperty( k, Message.kUndoable ):
                properties = _('This message is undoable.')

            htmlText += '<br>%s&nbsp;%s<br>\n' % (desc,properties)

            firstPar = True
            for par in pars:
                ( a, b ) = par
                if a != '' or b != '':
                    if firstPar:
                        htmlText += parHeader
                        firstPar = False
                    htmlText += ( '<tr><td><i>%s</i></td><td>&nbsp;&nbsp;</td><td>%s</td></tr>\n' % ( a, b ) )

            htmlText += '</table><br><br>\n'

        responses = '''
        <A name="Responses"><hr><br>
        <font size="+1"><b>MESSAGE RESPONSES</b></font>
        </A><br><br>\n'''
        responses += '<table border="1">\n'
        responses += '''
        <tr><td align="center"><b>Message sent</b></td>
        <td align="center"><b>Message(s) to wait for</b></td>
        <td align="center"><b>Default maximum waiting time</b></td></tr>'''

        respMessages = sorted(ResponseMessageRegistry)
        print "RESPONSE:", responses; import sys; sys.stdout.flush()
        for msgId in sorted(ResponseMessageRegistry):
            entry      = ResponseMessageRegistry[msgId]
            timeout    = entry[0]
            conditions = entry[1]
            responses += '''
            <tr><td valign="top">%s</td>
            <td valign="top">%s</td>
            <td valign="top" align="center">%s</td>
            </tr>''' % (msgId,str(conditions),str(timeout))

        responses += '</table>\n'

        htmlText += (responses + '</html>\n')

        f = open( "C:/AutodeskShowcaseMessages.html", 'w' )
        f.write( htmlText )
        f.close()

        self.sendMessageSync( 'STATUSBAR_SET_TEXT',
                              (("Saved message documentation to C:/AutodeskShowcaseMessages.html"),))


# ===============================================================
def instantiate( scriptName, args ):
    return MessageDocumentation( scriptName, args )
# ===============================================================