Interactive/InternalMessagesCustom.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.
##

# List the internal messages that usually get left out of the docs.
# 

__all__ = ['InternalMessagesCustom', 'instantiate']

from MessageRegistry     import theMessageRegistry
from MessageInterpreter  import MessageInterpreter
from UserCustomization   import UserCustomBase, CustomInfo
from awSupportApi        import Product
from Dialogs.AboutDialog import AboutDialog
from Message             import Message
import string

# -----------------------------------------------------------------------

class InternalMessagesCustom (UserCustomBase):
    def __init__(self):
        self.__myMenu = None
        self.__myInternalId = None
        self.__myInterpreter = LocalInterpreter()

    def getInterpreter(self,isInteractive):
        if isInteractive:
            return self.__myInterpreter
        return None


    def appendMenuItems(self,id,menu):
        if "Help" == id:
            menu.appendSeparator()
            self.__myInternalId = menu.appendItem(_( 'Internal Messages' ),
                                                  self.__onInternalMessages )
            self.__myMenu = menu


    def enableMenuStates(self,id,enableStates):
        if "Help" == id:
            enableStates[self.__myInternalId] = True

    # ------------------------------------------------
    def __onInternalMessages( self, event ):
        self.__myInterpreter.InternalMessages( self.__myMenu )


# -----------------------------------------------------------------------

class LocalInterpreter( MessageInterpreter ):

    def __init__( self ):
        MessageInterpreter.__init__( self )


    def InternalMessages( self, menu ):
        product = Product.instance().getFullNameNoFlavour()
        yearId = "%s" % (Product.instance().getCutId()[0:4])

        msgs = AboutDialog(scrollHeight=500,doHtml=True,printTitle=product)
        (text, count) = self.__getMessageDoc()
        msgs.show( menu.getParentWindow(),
                   _( '%(prodName)s Internal Message Reference' ) % ({'prodName':product} ),
                   'About.png',
                   "Total %d messages marked internal" % (count),
                   "Copyright %s Autodesk, Inc. All rights reserved. Do not distribute." % (yearId),
                   "",
                   text )


    def __getMessageDoc(self):
        yearId = "%s" % (Product.instance().getCutId()[0:4])

        htmlText = ''

        disclaimer = """
Copyright %s Autodesk, Inc. All rights reserved.<br>
This publication, or parts thereof, may not be reproduced in any form,
by any method, for any purpose.<br>
<br>
AUTODESK, INC., MAKES NO WARRANTY, EITHER EXPRESS OR IMPLIED, INCLUDING BUT
NOT LIMITED TO ANY IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A
PARTICULAR PURPOSE REGARDING THESE MATERIALS, AND MAKES SUCH MATERIALS
AVAILABLE SOLELY ON AN "AS-IS" BASIS.<br>
<br>
IN NO EVENT SHALL AUTODESK, INC., BE LIABLE TO ANYONE FOR SPECIAL, COLLATERAL,
INCIDENTAL, OR CONSEQUENTIAL DAMAGES IN CONNECTION WITH OR ARISING OUT OF
PURCHASE OR USE OF THESE MATERIALS. THE SOLE AND EXCLUSIVE LIABILITY TO
AUTODESK, INC., REGARDLESS OF THE FORM OF ACTION, SHALL NOT EXCEED THE
PURCHASE PRICE OF THE MATERIALS DESCRIBED HEREIN. <br>
<br> 
Autodesk, Inc., reserves the right to revise and improve its products as it
sees fit. This publication describes the state of this product at the time of
its publication, and may not reflect the product at all times in the future. 
<br><br>
Autodesk Confidential Document.  Do not distribute.<br><br>
""" % (yearId)
        ids = sorted(theMessageRegistry.ids())
        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 = ''
        count = 0
        for k in ids:
            # Skip the internal ones
            if not theMessageRegistry.hasProperty( k, Message.kInternal ):
                continue

            count += 1
            (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
            dataTypes = theMessageRegistry.dataType(k)
            dtLen = len(dataTypes)
            if len(pars) == dtLen:
                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 i in range(len(pars)):
                ( a, b ) = pars[i]
                dType = ''
                if i < dtLen:
                    dt = str(dataTypes[i]).split("'")
                    if len(dt) > 1:
                        dType = " (%s)" % (dt[1])

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

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

        htmlText += '<hr>' + disclaimer
        return ( htmlText, count )




# --------------------------------------------------------------------

def instantiate():
    return InternalMessagesCustom()


def info():
    customInfo = CustomInfo()
    customInfo.vendor = 'Autodesk'
    customInfo.version = '1.0'
    customInfo.api = '2013'
    customInfo.shortInfo = "Show internal messages."
    customInfo.longInfo = \
"""Add a menu item under "Help" to show internal messages.
"""
    return customInfo