#! /usr/env/python #-------------------------------------------------------------------------------- """ Application: Xml_Utils for DAQ_96_Gui Xml Utilities for the Graphical User Interface to the DAQ_96 Control Program """ #-------------------------------------------------------------------------------- #-------------------------------------------------------------------------------- import sys import string from types import StringType, ListType from xml.dom.minidom import Document from xml.dom.minidom import parseString from Itc_Gui_Client import IsXmlTracingOn from Print_Utils import printLine from showException import showException from Misc_Constants import eOk, eError #-------------------------------------------------------------------------------- #-------------------------------------------------------------------------------- def CreateXmlCommand ( CmdName, EltAttrDict = {} ) : # ----------------------------------------------- # # CmdName = The command keyword name, e.g. "Rio_WriteReg" # EltAttrDict = A dictionary (say: primary dict) of dictionaries (say: secondary dict) # The primary dict keys are the desired xml elements, e.g. "ADF" # Each primary dict entry (xml element) is another secondary dictionary # Each secondary dictionary is a list of xml attributes for the element, e.g. "SLOT" # Each secondary dict entry (xml attribute) is the string value of the xml attribute, e.g. "11" # return = The xml document object # Create XML document object to create the Gui command # note: this document object must be deleted by the gui client after sending xmlCmdDoc = Document() # First element is to specified the command name command_elt = xmlCmdDoc.createElement("COMMAND") command_node = xmlCmdDoc.appendChild(command_elt) command_elt.setAttribute("NAME", CmdName ) #command_elt.setAttribute("ID","node-id:process-id:msg-id") # add this maybe later # now add the element+atttribute list as specified in the dictionary passed for EltName in EltAttrDict.keys() : # the key names are the element name EltObj = xmlCmdDoc.createElement( EltName ) command_node.appendChild( EltObj ) # each dictionary entry is another dictionary for AttrName in EltAttrDict[EltName].keys() : # the key names are the attribute names # the dictionary value is the attribute name EltObj.setAttribute( AttrName, EltAttrDict[EltName][AttrName] ) return xmlCmdDoc #-------------------------------------------------------------------------------- #-------------------------------------------------------------------------------- def AppendCommentField ( xmlCmdDoc, Comment ) : # ----------------------------------------- # # xmlCmdDoc = the xml document object created by CreateXmlCommand # Comment = A String append to the xml document as # None is accepted as a value and this does not change the xml document # return = nothing if ( Comment <> None ) : # we need to re-locate the main xml node command_node_list = xmlCmdDoc.getElementsByTagName("COMMAND") if ( len(command_node_list) != 0 ) : command_node = command_node_list[0] comment_elt = xmlCmdDoc.createElement("COMMENT") command_node.appendChild(comment_elt) comment_elt.setAttribute("TEXT", str(Comment) ) else : printLine ( "Cmd_Comment", "No COMMAND Node found in XML while trying to add Comment" ) #-------------------------------------------------------------------------------- #-------------------------------------------------------------------------------- def RetrieveXmlAttr ( CmdReply, CmdName, EltAttrList = [], ReplyBox = None ) : # Figure out how many return values are expected (plus if ( type ( EltAttrList ) == ListType ) : Return_Status = eOk # this will be corrected if we find a problem Return_Values = len(EltAttrList) * [ eError ] # these values will be replaced as we find them else : printLine ( "ParseXML", "Unexpected call parameter type for XML Element/Attribute List" ) return eError # we don't know what to do ReplyFields = string.split ( CmdReply, ' ', 2 ) if ( ReplyBox != None ) : ReplyBox.setentry( ReplyFields[1:] ) if ( len( ReplyFields ) > 2 ) : if ( ReplyFields[1] != "Ok" ) : printLine ( "**Error**", "Command Reply Status = <%s>" % ReplyFields[1] ) Return_Status = eError try : xmlReplyDoc = parseString ( ReplyFields[2] ) command_node_list = xmlReplyDoc.getElementsByTagName("REPLY") if ( len(command_node_list) != 0 ) : command_name_attr = command_node_list[0].getAttributeNode("NAME") if ( command_name_attr != None ) : command_name = command_name_attr.value if ( string.upper(command_name) == string.upper(CmdName) ) : AttrNum = -1 for EltName, AttrName, AttrOptions in EltAttrList : AttrNum = AttrNum + 1 AttrDefault = None AttrBox = None if ( AttrOptions != None ) : if ( type ( AttrOptions ) == StringType ) : AttrDefault = AttrOptions elif ( type ( AttrOptions ) == ListType ) : if ( len ( AttrOptions ) > 1 ) : if ( type ( AttrOptions[1] ) == StringType ) : AttrDefault = AttrOptions[1] else : # must be ComboBox AttrBox = AttrOptions[1] if ( len ( AttrOptions ) > 2 ) : if ( type ( AttrOptions[2] ) == StringType ) : AttrDefault = AttrOptions[2] else : # must be ComboBox AttrBox = AttrOptions[2] else : # must be ComboBox AttrBox = AttrOptions Return_Values[AttrNum] = AttrDefault this_elt_node_list = command_node_list[0].getElementsByTagName( EltName ) if ( len(this_elt_node_list) != 0 ) : this_elt_name_attr = this_elt_node_list[0].getAttributeNode( AttrName ) if ( this_elt_name_attr != None ) : this_attr_value = str(this_elt_name_attr.value) Return_Values[AttrNum] = this_attr_value if ( AttrBox != None ) : AttrBox.setentry ( this_attr_value ) if IsXmlTracingOn() : printLine ( "parseXML", "Element <%s> Attribute <%s> Value <%s> " % ( EltName, AttrName, this_attr_value ) ) else : if ( AttrDefault == None ) : # omitted elements are not an error if default value was provided printLine ( "parseXML", "Couldn't Find Attribute Object for <%s>" % AttrName ) else : if ( AttrDefault == None ) : # omitted elements are not an error if default value was provided printLine ( "parseXML", "Couldn't Find Element Object for <%s>" % EltName ) else : printLine ( "parseXML", "Command Name Mismatch: Expected <%s> Received <%s>" % ( CmdName, command_name ) ) Return_Status = eError else : printLine ( "parseXML", "Couldn't Find Attribute Object for " ) Return_Status = eError else : printLine ( "parseXML", "Couldn't Find Element Object for " ) Return_Status = eError except : printLine ( "parseXML", "XML Parsing Failure for GUI Command Reply" ) Return_Status = eError showException ( ) else : printLine ( "**Error**", "Command Reply Incomplete = %s" % CmdReply ) Return_Status = eError for Return_Value in Return_Values : if ( Return_Value == eError ) : Return_Status = eError if ( ReplyBox != None ) : if ( Return_Status == eError ) : ReplyBox.component('entry').configure ( bg = 'red' ) else : ReplyBox.component('entry').configure ( bg = 'gray80' ) # printLine ( "debug**", str(EltAttrList) ) # printLine ( "debug**", str(Return_Values) ) # printLine ( "debug**", Return_Status ) if ( len ( Return_Values ) > 0 ) : Return_Values.append ( Return_Status ) return tuple ( Return_Values ) else : return Return_Status