Interactive/MessageDocsForApi.py

##
## (C) Copyright 2012 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.
##

# This will save the extracted message documentation to
# C:/AutodeskShowcaseMessages.html

import os

from ScriptRunner        import ScriptRunner
from Message             import Message
from MessageRegistry     import theMessageRegistry

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

    def Main( self ):
        
        messageIds = sorted(theMessageRegistry.ids())
        
        referenceCategoriesDictionary = self.buildMessageDict( messageIds )
        htmlText =  self.generateHTMLText( referenceCategoriesDictionary )
        self.writeToFile(htmlText)
                
    def buildMessageDict(self, messageIds):
        referenceCategoriesDictionary = {}
        for id in messageIds:
            # Skip the internal ones
            if theMessageRegistry.hasProperty( id, Message.kInternal ):
                continue

            (desc,pars,cats) = ['Undocumented.', [['unknown','unknown']],()]
            doc = theMessageRegistry.documentation(id)
            
            fullcats = ''
            if doc:
                if len(doc) == 3:
                    (desc, pars, cats) = doc
                elif len(doc) == 2:
                    (desc, pars) = doc

            referenceCat = "Command"
            messagesCat  = "Command"
            if cats:
                if len( cats ) >=2:
                    messagesCat  = cats[0]
                    referenceCat = cats[1]
                if "modified_v2013" in cats:
                    referenceCat = "modified_v2013"
            
            if referenceCat not in referenceCategoriesDictionary:
                referenceCategoriesDictionary[referenceCat] = [[id, desc, pars, messagesCat], ]
            else:
                referenceCategoriesDictionary[referenceCat].append([id, desc, pars, messagesCat])   
            
        return referenceCategoriesDictionary
        
    def generateHTMLText(self, referenceCategoriesDictionary):
            
        html = ""
            
        ## header
        html+= "/**\n"
        html+= "\\page messages Messages Reference\n"
        html+= "This is the messages reference documentation for Showcase.\n\n"
        
        sortedRegistry = sorted(list(referenceCategoriesDictionary.keys()))     
        for subpage in sortedRegistry:
            if "modified_v2013" not in subpage:
                html+= "\n- \\subpage "+subpage.title()
        html+= "\n*/"
            
        ##subpages
        for subpage in sortedRegistry:  
            if subpage == "modified_v2013":
                html+= "\n\n/**"
                html+= "\n\\page modified Modified 2013 Messages"
                html+= "\n\n<ul>"
                for messageFull in referenceCategoriesDictionary["modified_v2013"]:
                    html+= "\n<li>"
                    html+= "\n<tt>%s</tt> - %s" % ( messageFull[0],messageFull[1])
                    if messageFull[2]:
                        html+= "\n<p>Parameters:</p>"
                        html+= "\n<ul>"
                        for individualParameters in messageFull[2]:
                            (nameParam,descParam) = individualParameters
                            html+= "\n<li> %s - %s</li>\n" % (nameParam,descParam)
                        html+= "\n</ul>"
                    html+= "\n</li>"
                html+= "\n\n</ul>"
                html+= "\n*/"
                
            else:
                html+= "\n\n/**"
                html+= "\n\\page "+subpage.title()+" "+subpage.title()+" Messages"
                html+= "\n\n<ul>"
                for messageFull in referenceCategoriesDictionary[subpage]:
                    html+= "\n<li>"
                    html+= "\n<tt>%s</tt> - %s" % ( messageFull[0],messageFull[1])
                    if messageFull[2]:
                        html+= "\n<p>Parameters:</p>"
                        html+= "\n<ul>"
                        for individualParameters in messageFull[2]:
                            (nameParam,descParam) = individualParameters
                            html+= "\n<li> %s - %s</li>\n" % (nameParam,descParam)
                        html+= "\n</ul>"
                    html+= "\n</li>"
                html+= "\n\n</ul>"
                html+= "\n*/"
        
        return html
        
    def writeToFile(self, htmlText):
        
        filename = os.path.join("C:", os.sep, "messages.txt")
        f = open( filename, 'w' )
        f.write( htmlText )
        f.close()
        print "Successfully copied file to "+filename
        
# ===============================================================
def instantiate( scriptName, args ):
    return MessageDocumentation( scriptName, args )
# ===============================================================