def Test(tester): dom = Parse(XML_INST1) el = filter( lambda E: (E.namespaceURI, E.localName) == (WSA200403_NS, "From"), dom.childNodes[0].childNodes[0].childNodes)[0] tester.startGroup('eclusive c14n (WS-Addressing example)') tester.startTest('regular c14n') stream = StringIO() CanonicalPrint(el, stream, exclusive=False) cxml = stream.getvalue().strip() d1 = base64.encodestring(sha.sha(C14N_INC1).digest()).strip() d2 = base64.encodestring(sha.sha(cxml).digest()).strip() #tester.compare(d1, d2) #tester.compare(d1, C14N_INC1_DIGEST) tester.compare(C14N_INC1, cxml) tester.testDone() tester.startTest('exclusive c14n') stream = StringIO() CanonicalPrint(el, stream, exclusive=True) cxml = stream.getvalue().strip() tester.compare(C14N_EXCL1, cxml) tester.testDone() tester.startTest('exclusive c14n with inclusivePrefixes') stream = StringIO() CanonicalPrint(el, stream, exclusive=True, inclusivePrefixes=['xsi', 'xsd']) cxml = stream.getvalue().strip() tester.compare(C14N_EXCL2, cxml) tester.testDone() tester.groupDone() del dom del el return
def RunQueryFile(fileName, comment, options): doc = Parse(fileName) #doc.xpath('string(ham//em[1])') # read overall benchmark settings root = doc.rootNode.childNodes[0] repeatTimes = int(root.attributes[(None, 'repeatTimes')].value) clearCache = root.attributes[(None, 'clearCache')].value == "True" if options.runQueries: # setup output w = CSVWriter() w.Open(ResultsColumns, fileName + '.out.txt') # for each store stores = root.xpath('./Store') for store in stores: RunStore(w, fileName, store, repeatTimes, clearCache, comment) w.Close() print "Done."
def DoTheShark(fileName, proto): if proto == 2: # print out the protocols print "Select one of the following protocols:\n" doc = Parse(fileName) nodes = doc.xpath('descendant::proto') for n in nodes: print "\t", n.getAttributeNS(None, 'name') raise PeachException("") name = fileName doc = Parse(fileName) node = doc.xpath('descendant::proto[@name="%s"]' % proto)[0] shark = PeachShark() shark.removeTextNodes(node.parentNode) ret = """<?xml version="1.0" encoding="utf-8"?> <Peach xmlns="http://phed.org/2008/Peach" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://phed.org/2008/Peach /peach/peach.xsd"> <!-- ==// Auto Generated by PeachShark //== --> <!-- Please do the following before using this fuzzer: - Take a look through the generated output, see if it makes sense - Integrate into a Peach Fuzzer --> <!-- Import defaults for Peach instance --> <Include ns="default" src="file:defaults.xml"/> """ sibling = node while sibling != None: #shark.removeTextNodes(sibling.parentNode) debug("Handing node: " + sibling.getAttributeNS(None, 'name')) templateStr, s, p = shark.peachNode( sibling, 1, sibling.getAttributeNS(None, 'size'), None) ret += templateStr sibling = sibling.nextSibling ret += '\t<DataModel name="SharkTemplate">\n' for t in shark._templates: ret += '\t\t<Block ref="%s" />\n' % t ret += """ </DataModel> <Test name="MyTest"> %s </Test> <Run name="DefaultRun"> <Test ref="MyTest" /> </Run> </Peach> """ % shark.figureOutPublisher(doc) return ret
def Test(tester): tester.startGroup("Convenience parse functions, part 1") tester.startTest("Parse with string") doc = Parse(TEST_STRING) #Minimal node testing tester.compare(len(doc.childNodes), 1) tester.compare(doc.childNodes[0].nodeType, Node.ELEMENT_NODE) tester.compare(doc.childNodes[0].nodeName, 'test') tester.compare(doc.childNodes[0].namespaceURI, None) tester.compare( doc.childNodes[0].prefix, None, ) tester.testDone() tester.startTest("Parse with stream") stream = open(TEST_FILE) doc = Parse(stream) #Minimal node testing tester.compare(len(doc.childNodes), 1) tester.compare(doc.childNodes[0].nodeType, Node.ELEMENT_NODE) tester.compare(doc.childNodes[0].nodeName, 'disclaimer') tester.compare(doc.childNodes[0].namespaceURI, None) tester.compare( doc.childNodes[0].prefix, None, ) tester.testDone() tester.startTest("Parse with file path") doc = Parse(TEST_FILE) #Minimal node testing tester.compare(len(doc.childNodes), 1) tester.compare(doc.childNodes[0].nodeType, Node.ELEMENT_NODE) tester.compare(doc.childNodes[0].nodeName, 'disclaimer') tester.compare(doc.childNodes[0].namespaceURI, None) tester.compare( doc.childNodes[0].prefix, None, ) tester.testDone() tester.groupDone() if tester.offline: return # These tests require an active network connection. tester.startGroup( "Convenience parse functions, part 2 (may be slow; requires Internet connection)" ) tester.startTest("Parse with URL") doc = Parse(TEST_URL) #Minimal node testing tester.compare(len(doc.childNodes), 1) tester.compare(doc.childNodes[0].nodeType, Node.ELEMENT_NODE) tester.compare(doc.childNodes[0].nodeName, 'disclaimer') tester.compare(doc.childNodes[0].namespaceURI, None) tester.compare( doc.childNodes[0].prefix, None, ) tester.testDone() tester.groupDone() return
def node_set(xml): return Parse(xml)
def CheckbookReception_importItemFile(self, import_file=None, REQUEST=None, **kw): reference_dict = {} reference_dict['CHEQUIER_COMPTE_COURANT_ORDINAIRE'] = 'CHCCO' reference_dict['CHEQUIER_COMPTE_ORDINAIRE_DU_PERSONNEL'] = 'CHCOP' reference_dict['CARNET_BON_VIREMENT'] = 'CABV' reference_dict['BON_VIREMENT'] = 'BV' # We will build a several listbox like it is already done into the user interface # A listbox will be build for every resource xml_content = Parse(import_file) file_item_list = xml_content.xpath('//object') # First, construct a dictionnary for every resource import_dict = {} #self.log("import checkbook", "file_item_list = %s" %(file_item_list,)) for item in file_item_list: checkbook_id = item.xpath("string(@id)") check_quantity = int(float(item.xpath("string(./check_quantity)"))) reference_min = str(item.xpath("string(./reference_min)")) reference_max = str(item.xpath("string(./reference_max)")) quantity = int(float(item.xpath("string(./quantity)"))) internal_account_number = item.xpath("string(./numero_interne)") checkbook_type = item.xpath("string(./checkbook_type)") type = str(item.xpath("string(./checkbook_type)")) gid = str(item.xpath("string(./gid)")) checkbook_dict = import_dict.setdefault(checkbook_type, {}) account_dict = checkbook_dict.setdefault(internal_account_number, {}) item_dict = account_dict.setdefault(gid, {}) item_dict['reference_min'] = reference_min item_dict['reference_max'] = reference_max item_dict['check_quantity'] = check_quantity item_dict['quantity'] = quantity item_dict['internal_account_number'] = internal_account_number #self.log("import checkbook", "item_dict = %s" %(item_dict,)) listbox_dict = {} for (checkbook_type, checkbook_dict) in import_dict.items(): #self.log("checkbook_type %s, checkbook_dict %s" %(checkbook_type, checkbook_dict), "") listbox = [] i = 0 resource_list = self.portal_catalog( portal_type=['Checkbook Model', 'Check Model'], reference=reference_dict[checkbook_type]) if len(resource_list) != 1: raise ValueError, "The import does not support this type : %s" % checkbook_type resource = resource_list[0].getObject() resource_relative_url = resource.getRelativeUrl() resource_amount_dict = {} is_checkbook = 0 if resource.getPortalType() == 'Checkbook Model': is_checkbook = 1 if is_checkbook: for amount in resource.objectValues( portal_type="Checkbook Model Check Amount Variation"): resource_amount_dict[int(amount.getQuantity())] = "check_amount/%s" % \ amount.getRelativeUrl() for (account, account_dict) in checkbook_dict.items(): for (gid, item_dict) in account_dict.items(): #self.log("is checkbook ? %s" %is_checkbook, "will fill value %s" %(item_dict,)) listbox_line = {} listbox_line['listbox_key'] = '%05i' % i listbox_line['reference_range_min'] = item_dict[ 'reference_min'] listbox_line['reference_range_max'] = item_dict[ 'reference_max'] listbox_line['destination_payment_reference'] = item_dict[ 'internal_account_number'] #listbox_line['quantity'] = 1 listbox_line['quantity'] = item_dict['quantity'] if is_checkbook: listbox_line['check_amount'] = resource_amount_dict[ item_dict['check_quantity']] listbox.append(listbox_line) i += 1 listbox_dict[resource_relative_url] = listbox # First make sure there is no errors message = None error_value = 0 for (resource_relative_url, listbox) in listbox_dict.items(): REQUEST['resource'] = resource_relative_url (error_value, field_error_dict) = self.CheckDelivery_generateCheckDetailInputDialog( verbose=1, listbox=listbox, batch_mode=1, resource=resource_relative_url, REQUEST=REQUEST) if error_value: message = ', '.join( [str(x.error_text) for x in field_error_dict.itervalues()]) redirect_url = '%s/view?%s' % ( self.absolute_url(), 'portal_status_message=%s' % message) REQUEST['RESPONSE'].redirect(redirect_url) # Then create everything if not error_value: for (resource_relative_url, listbox) in listbox_dict.items(): REQUEST['resource'] = resource_relative_url self.CheckDetail_saveFastInputLine(listbox=listbox, check=0, resource=resource_relative_url, REQUEST=REQUEST) message = Message(domain='ui', message='File Imported successfully') self.setImported(1) return message
def Test(tester): tester.startGroup('c14n spec') tester.startTest('c14n spec 3.2') doc = Parse(C14N_3_2) stream = cStringIO.StringIO() CanonicalPrint(doc, stream) tester.compare(C14N_3_2_RESULT, stream.getvalue()) tester.testDone() tester.startTest('c14n spec 3.3') doc = Parse(C14N_3_3) stream = cStringIO.StringIO() CanonicalPrint(doc, stream) tester.compare(C14N_3_3_RESULT, stream.getvalue()) tester.testDone() tester.startTest('c14n spec 3.4') doc = Parse(C14N_3_4) stream = cStringIO.StringIO() CanonicalPrint(doc, stream) tester.compare(C14N_3_4_RESULT, stream.getvalue()) tester.testDone() tester.startTest('1st regular c14n example from exclusive spec sect 2.2') doc = Parse(EXCL_C14N_2_2_1) elem2 = doc.xpath(u'//*[local-name() = "elem2"]')[0] stream = cStringIO.StringIO() CanonicalPrint(elem2, stream) tester.compare(EXSPEC_2_2_1_C14N_RESULT, stream.getvalue()) tester.testDone() tester.startTest('2nd regular c14n example from exclusive spec sect 2.2') doc = Parse(EXCL_C14N_2_2_2) elem2 = doc.xpath(u'//*[local-name() = "elem2"]')[0] stream = cStringIO.StringIO() CanonicalPrint(elem2, stream) tester.compare(EXSPEC_2_2_2_C14N_RESULT, stream.getvalue()) tester.testDone() tester.groupDone() tester.startGroup('exclusive c14n spec') tester.startTest('exclusive c14n spec 2.2 eg 1') doc = Parse(EXCL_C14N_2_2_1) elem2 = doc.xpath(u'//*[local-name() = "elem2"]')[0] stream = cStringIO.StringIO() CanonicalPrint(elem2, stream, exclusive=True) tester.compare(EXCL_C14N_2_2_RESULT, stream.getvalue()) tester.testDone() tester.startTest('exclusive c14n spec 2.2 eg 2') doc = Parse(EXCL_C14N_2_2_2) elem2 = doc.xpath(u'//*[local-name() = "elem2"]')[0] stream = cStringIO.StringIO() CanonicalPrint(elem2, stream, exclusive=True) tester.compare(EXCL_C14N_2_2_RESULT, stream.getvalue()) tester.testDone() tester.groupDone() return