def createXMLTags(sTagName): ''' Adds the brackets to tag name to make Opening and Closing XML tags Returns '<tag_name>' and '</tag_name>' from 'tag_name' ''' #VERBOSE = True if(VERBOSE == True): print2("Parameters - createXMLTags:") print2(" sTagName: " + sTagName) # end # Add the brackets to make the Opening tag sFullOpeningTag = "<" + sTagName + ">" # Add the brackets and slash to make the Closing tag sFullClosingTag = "</" + sTagName + ">" if (VERBOSE == True): print2("Opening tag: " + sFullOpeningTag) print2("Closing tag: " + sFullClosingTag) # end sXMLTags = [sFullOpeningTag, sFullClosingTag] return sXMLTags
def getSfdcUserIds(input, context): #from PyWorks_SoaTest_SfdcUtilities import sfdc_StoreDictInSoaTestVar # PyWorks SOATest for Salesforce.com Utilities #from PyWorks_WebUtilities import * #VERBOSE = True sReadSoaTestDataSourceName = "SFDC Saved Data" sReadVariableName = "SFDC: QueryUser_XML" sWriteSoaTestDataSourceName = "SFDC Saved Data" sWriteVariableName = "SFDC: UserIds" sTagNameKey = "sf:Username" sTagNameValue = "sf:Id" print2("\tSaving SFDC User's Name and ID") # Set the return of this function status based on the return status of the called function bStatus = sfdc_StoreDictInSoaTestVar(context, sReadSoaTestDataSourceName, sReadVariableName, sWriteSoaTestDataSourceName, sWriteVariableName, sTagNameKey, sTagNameValue) if(VERBOSE == True): # Read back the saved DICT hUserIDs = context.get(sWriteVariableName) #print2("\tSaved value '" + str(hUserIDs) + "'") print2("\tEntries in DICT = " + str(len(hUserIDs))) print2("\tIterate over the DICT '" + sWriteVariableName + "'") for sKey, sValue in hUserIDs.iteritems(): print2("\t\t" + sKey + " = " + sValue) # end # end return bStatus
def removeXMLBrackets(sTag): ''' Removes the brackets from Opening or Closing tags Modifies '</tag_name>' or '<tag_name>' to 'tag_name' ''' #VERBOSE = True if(VERBOSE == True): print2("Parameters - removeBrackets:") print2(" sTag: " + sTag) # end # TODO: Improve to ignore brackets within the tag name # Remove the brackets and slash from the Closing tag sTag = sTag.replace('</', '') #sTag = sTag.replace(r'^</', '') # Remove the bracket from the Opening tag sTag = sTag.replace('<', '') #sTag = sTag.replace(r'^<', '') # Remove the trailing bracket sTag = sTag.replace('>', '') #sTag = sTag.replace(r'>$', '') if (VERBOSE == True): print2("Tag w/o brackets: " + sTag) # end return sTag
def findSfdcObjectId(context, sReadVariableName, sNameKey, bCaseSensitive=True): ''' RReads a SOATest Project Variable that contains a DICT of an SFDC Object's Name:Id pairs, Searches the DICT for a Key that matches the specified Object's Name Returns a LIST with the matching Object Name and it's ID value. ''' # VERBOSE = True # Set a default value aMatchingNameValuePair = ["", ""] # Retrieve the DICT from the SOATest variable hNameValuePairs = context.get(sReadVariableName) # Perform the Search aMatchingNameValuePair = getMatchingKeyValue(hNameValuePairs,sNameKey, bCaseSensitive) if(VERBOSE == True): print2("\tFound Name:Id pair of..") print2(aMatchingNameValuePair) # end return aMatchingNameValuePair
def isTagInXML(sXML, sTagName): ''' Parse a string containing XML Tags to see if the specified tag name exists. Note that a empty closing tag (e.g. </tag_name> ) with no opening <tag_name> will be identified as existing ''' # VERBOSE = True if(VERBOSE == True): print2("Parameters - isTagInXML:") print2(" sXML: " + sXML) print2(" sTagName: " + sTagName) # end # Set default return value bFound = False # Instead of checking the specified tag for enclosing brackets <tag_name>. # ITs easier to just remove them (if they existed) and then adding them. sTagWithoutBrackets = removeXMLBrackets(sTagName) # Generate the Opening and Closing tags (with brackets) aXMLTags = createXMLTags(sTagWithoutBrackets) #sFullOpeningTag = aTags[0] sFullClosingTag = aXMLTags[1] if (VERBOSE == True): #print2("Opening tag: " + sFullOpeningTag) print2("Closing tag: " + sFullClosingTag) # end # Parse the XML String to see if there is a Closing Tag # If the Closing tag is found then the tag exists, even if # there is no Opening tag if(re.search(sFullClosingTag, sXML)): bFound = True # end return bFound
def getXMLTagValue(sXML, sTagName): ''' Parses an XML string containing XML Tags to get the values of the first occurrence of the specified tag name. If more than one occurrence of the specified tag name can exist in the XML string use: getMultipleXMLTagValues() Note that a Closing XML tag (</tag_name> ) with no Opening XML Tag <tag_name> proceeding it will return None ''' #VERBOSE = True if(VERBOSE == True): print2("Parameters - getXMLTagValue:") print2(" sXML: " + sXML) print2(" sTagName: " + sTagName) # end # Verify that the specified tag exists if (isTagInXML == False): if (VERBOSE == True): print2("No tag: " + sTagName) # end return None # end # Generate the Opening and Closing XML tags # Its easier to first remove brackets (if they existed). sTagWithoutBrackets = removeXMLBrackets(sTagName) # Generate the Opening and Closing tags (with brackets) aXMLTags = createXMLTags(sTagWithoutBrackets) sFullOpeningTag = aXMLTags[0] sFullClosingTag = aXMLTags[1] if (VERBOSE == True): print2("Opening tag: " + sFullOpeningTag) print2("Closing tag: " + sFullClosingTag) # end # Determine the length of the Opening and Closing Tags iOpeningTagLength = len(sFullOpeningTag) iClosingTagLength = len(sFullClosingTag) if (VERBOSE == True): print2("Opening tag length: " + str(iOpeningTagLength)) print2("Closing tag length: " + str(iClosingTagLength)) # end # # Find the position of the first character of each Tag in the XML string iIndexOfOpeningTag = sXML.find(sFullOpeningTag) iIndexOfClosingTag = sXML.find(sFullClosingTag) if (VERBOSE == True): print2("Opening tag index: " + str(iIndexOfOpeningTag)) print2("Closing tag index: " + str(iIndexOfClosingTag)) # end # If either of the Tags can't be found, return None if ((iIndexOfOpeningTag == -1 ) | (iIndexOfClosingTag == -1 )): if (VERBOSE == True): print2("Missing either Opening XML Tag or Closing XML tag") # end return None # end # Save the STRING between the Opening and Closing Tags sXMLTagValue = sXML[(iIndexOfOpeningTag + iOpeningTagLength):iIndexOfClosingTag] return sXMLTagValue
def getMultipleXMLTagValues(sXML, sTagName): ''' Parses a string containing XML Tags to get the values of the specified XML tag name, when there are multiple occurrences of the same Tag name Note that a Closing XML tag (</tag_name> ) with no Opening XML Tag <tag_name> proceeding it will return '' ''' #VERBOSE = True # Define default return value aMatchingTagValues = [] if(VERBOSE == True): print2("Parameters - getMultipleXMLTagValues:") print2(" sXML: " + sXML) print2(" sTagName: " + sTagName) # end # Generate the Opening and Closing XML tags # # Its easier to first remove brackets (if they existed). sTagWithoutBrackets = removeXMLBrackets(sTagName) # Generate the Opening and Closing tags (with brackets) aXMLTags = createXMLTags(sTagWithoutBrackets) sFullOpeningTag = aXMLTags[0] sFullClosingTag = aXMLTags[1] if (VERBOSE == True): print2("Opening tag: " + sFullOpeningTag) print2("Closing tag: " + sFullClosingTag) # end # Determine the length of the Opening and Closing Tags iOpeningTagLength = len(sFullOpeningTag) iClosingTagLength = len(sFullClosingTag) if (VERBOSE == True): print2("Opening tag length: " + str(iOpeningTagLength)) print2("Closing tag length: " + str(iClosingTagLength)) # end # iTotalTagCount = sXML.count(sFullClosingTag) # Verify that the specified tag exists if (iTotalTagCount == 0): if (VERBOSE == True): print2("No Closing tag found: " + sFullClosingTag) # end # No occurrence of the Closing Tag found so get out of this function return aMatchingTagValues # end if (VERBOSE == True): print2("Total Closing tags found: " + str(iTotalTagCount)) # end # Seed the counter iCurrentTag = iTotalTagCount # Loop to get that values of each of the Tags while iCurrentTag > 0: # Find the position of the first character of each Tag in the XML string iIndexOfFirstOpeningTag = sXML.find(sFullOpeningTag) iIndexOfFirstClosingTag = sXML.find(sFullClosingTag) if (VERBOSE == True): print2("Opening tag index: " + str(iIndexOfFirstOpeningTag)) print2("Closing tag index: " + str(iIndexOfFirstClosingTag)) # end # See if either of the Tags can't be found, save a value of '' if ((iIndexOfFirstOpeningTag == -1 ) | (iIndexOfFirstClosingTag == -1 )): if (VERBOSE == True): print2("Missing either Opening XML Tag or Closing XML tag") # end # Save a value of '' fro the current occurrence of the TAg aMatchingTagValues.append('') else: # Get the text between the Opening and Closing Tags sFoundXMLTagValue = sXML[(iIndexOfFirstOpeningTag + iOpeningTagLength):iIndexOfFirstClosingTag] if (VERBOSE == True): print2("Found XML tag value: '" + sFoundXMLTagValue + "'") # end # Save the STRING between the Opening and Closing Tags aMatchingTagValues.append(sFoundXMLTagValue) # end - See if either of the Tags... # Update the XML STRING with the current Closing tag removed sXML = sXML[(iIndexOfFirstClosingTag + iClosingTagLength) :] if (VERBOSE == True): print2("Updated XML STRING: '" + sXML + "'") # end # Update the count of the remaining Closing Tags iCurrentTag = sXML.count(sFullClosingTag) if (VERBOSE == True): print2("Remaining number of XML Tags: " + str(iCurrentTag)) # end # end - Loop to get that values return aMatchingTagValues
# Define the LIST to hold the list of test files aMyTestList = [] # Define the search criteria sFilesToFind = "_unittest.py" # Loop through the directory and sub-directories using the find command to collect # the list of files. Weeding out the numerous pathnames that don't end with a valid # test file name (files ending with _test,py). #aPathlist = os.listdir(".") # Get the Current Working Directory sRootDir = os.getcwd() if (VERBOSE == True): print2("PWD = " + sRootDir) # end # Get a Recursive listing of sub-directories aPathlist = list_tree(sRootDir) if(VERBOSE == True): #print2(aPathlist) pass # end # Parse the Recursive listing of sub-directories for files that match the search criteria # Loop through the Recursive listing of sub-directories for sPath in aPathlist:
def sfdc_StoreDictInSoaTestVar(context, sReadSoaTestDataSourceName, sReadVariableName, sWriteSoaTestDataSourceName, sWriteVariableName, sTagNameKey="Id", sTagNameValue="Name"): ''' Saves the specified XML Tag settings as Key and Values of a DICT Object into a specified SOATest Variable in the specified SOATest Data Source. ''' #VERBOSE = True if(VERBOSE == True): print2("Parameters - sfdc_StoreDictInSoaTestVar:") print2(" sReadSoaTestDataSourceName: " + sReadSoaTestDataSourceName) print2(" sReadVariableName: " + sReadVariableName) print2(" sTagNameKey: " + sTagNameKey) print2(" sTagNameValue: " + sTagNameValue) print2(" sWriteSoaTestDataSourceName: " + sWriteSoaTestDataSourceName) print2(" sWriteVariableName: " + sWriteVariableName) # end # Read the SOAP Response XML STRING from the SOATest Data Source sXML = str(context.getValue(sReadSoaTestDataSourceName, sReadVariableName)) if(VERBOSE == True): pass print2("XML '" + sXML + "'") # end # Parse the Tag settings to use as the Key into a LIST aKeyList = getMultipleXMLTagValues(sXML, sTagNameKey) # Parse the Tag settings to use as the Value into a LIST aValueList = getMultipleXMLTagValues(sXML, sTagNameValue) if(VERBOSE == True): print2("aKeyList '" + str(aKeyList) + "'" ) print2("aValueList '" + str(aValueList) + "'" ) # end # Create a DICT from the two LIST objects hLookupDict = dict(zip(aKeyList, aValueList)) if(VERBOSE == True): pass print2("DICT ...") print2(str(hLookupDict)) # end # Save the DICT into a SOATest Data Source context.put(sWriteVariableName, hLookupDict) return True