def get_verses(self, xml): """ Iterates through the verses in the XML and returns a list of verses and their attributes. :param xml: The XML of the song to be parsed. The returned list has the following format:: [[{'type': 'v', 'label': '1'}, u"optional slide split 1[---]optional slide split 2"], [{'lang': 'en', 'type': 'c', 'label': '1'}, u"English chorus"]] """ self.song_xml = None verse_list = [] if not xml.startswith('<?xml') and not xml.startswith('<song'): # This is an old style song, without XML. Let's handle it correctly by iterating through the verses, and # then recreating the internal xml object as well. self.song_xml = objectify.fromstring('<song version="1.0" />') self.lyrics = etree.SubElement(self.song_xml, 'lyrics') verses = xml.split('\n\n') for count, verse in enumerate(verses): verse_list.append([{'type': 'v', 'label': str(count)}, str(verse)]) self.add_verse_to_lyrics('v', str(count), verse) return verse_list elif xml.startswith('<?xml'): xml = xml[38:] try: self.song_xml = objectify.fromstring(xml) except etree.XMLSyntaxError: log.exception('Invalid xml %s', xml) xml_iter = self.song_xml.getiterator() for element in xml_iter: if element.tag == 'verse': if element.text is None: element.text = '' verse_list.append([element.attrib, str(element.text)]) return verse_list
def get_networks(self): """ Return a list of networks as lxml ObjectifiedElement objects. :return: List of networks represented as ObjectifiedElement objects. """ networks = [] # Get org networks. response = self._client.request( 'GET', self._client.url('query?type=orgNetwork')) records = objectify.fromstring(response.content) for record in records.OrgNetworkRecord: networks.append(record) # Get external network. response = self._client.request( 'GET', self._client.url('query?type=externalNetwork')) records = objectify.fromstring(response.content) for record in records.NetworkRecord: networks.append(record) return networks
def query(self, endpoint, arguments=None): if not endpoint.startswith('/'): endpoint = '/' + endpoint if not endpoint.endswith(ApiClient.ENDPOINT_EXT): endpoint += ApiClient.ENDPOINT_EXT if arguments is None: arguments = {} query_string = '?'.join([endpoint, urlencode(arguments)]) result = self.cache.get(query_string) if result is not None: return objectify.fromstring(result) connection = HTTPSConnection(ApiClient.API_HOST) connection.request('GET', query_string) response = connection.getresponse() if response.status != 200: raise ApiError('Error response: ' + str(response.status)) result = response.read() result_object = objectify.fromstring(result) cached_until = datetime.strptime(result_object.cachedUntil.text, ApiClient.TIMESTAMP_FORMAT) self.cache.set(query_string, result, cached_until) return result_object
def payload_from_request(cid, request, data_format, transport): """ Converts a raw request to a payload suitable for usage with SimpleIO. """ if request is not None: if data_format == DATA_FORMAT.XML: if transport == 'soap': if isinstance(request, objectify.ObjectifiedElement): soap = request else: soap = objectify.fromstring(request) body = soap_body_xpath(soap) if not body: raise ZatoException(cid, 'Client did not send the [{}] element'.format(soap_body_path)) payload = get_body_payload(body) else: if isinstance(request, objectify.ObjectifiedElement): payload = request else: payload = objectify.fromstring(request) elif data_format == DATA_FORMAT.JSON: if not request: return '' if isinstance(request, basestring): payload = loads(request) else: payload = request else: payload = request else: payload = request return payload
def listAssetGroups(self, groupName=''): call = 'asset_group_list.php' if groupName == '': agData = objectify.fromstring(self.request(call)) else: agData = objectify.fromstring(self.request(call, 'title='+groupName)).RESPONSE groupsArray = [] scanipsArray = [] scandnsArray = [] scannersArray = [] for group in agData.ASSET_GROUP: try: for scanip in group.SCANIPS: scanipsArray.append(scanip.IP) except AttributeError: scanipsArray = [] # No IPs defined to scan. try: for scanner in group.SCANNER_APPLIANCES.SCANNER_APPLIANCE: scannersArray.append(scanner.SCANNER_APPLIANCE_NAME) except AttributeError: scannersArray = [] # No scanner appliances defined for this group. try: for dnsName in group.SCANDNS: scandnsArray.append(dnsName.DNS) except AttributeError: scandnsArray = [] # No DNS names assigned to group. groupsArray.append(AssetGroup(group.BUSINESS_IMPACT, group.ID, group.LAST_UPDATE, scanipsArray, scandnsArray, scannersArray, group.TITLE)) return groupsArray
def attachDeviceFlags(dom, xml, flags=0): old_xml = dom.XMLDesc(libvirt.VIR_DOMAIN_XML_SECURE) root = objectify.fromstring(old_xml) dev = objectify.fromstring(xml) root.devices.append(dev) MockModel._mock_vms[dom.name()] = ET.tostring(root, encoding="utf-8")
def check(cls, path): try: data = _read_storefile(path, cls.filename()) objectify.fromstring(data) except etree.XMLSyntaxError as e: return False return True
def setUp(self): xml = """<?xml version="1.0" encoding="UTF-8"?> <metadata> <title /> <meta qualifier='metadataCreator'></meta> </metadata> """ self.tree = objectify.fromstring(xml) agent_xml = """<?xml version="1.0" encoding="UTF-8"?> <metadata> <creator> <info /> <type /> <name /> </creator> <meta /> </metadata> """ self.agent_tree = objectify.fromstring(agent_xml) pub_xml = """<?xml version="1.0" encoding="UTF-8"?> <metadata> <publisher> <name /> <location /> </publisher> <meta /> </metadata> """ self.pub_tree = objectify.fromstring(pub_xml)
def testDCXMLNonRootElements(self): """Test for expected Dublin Core elements.""" generated_dxml = generate_dc_xml(UNTL_DICT) generated_dxml_root = objectify.fromstring(generated_dxml) constant_dxml_root = objectify.fromstring(DUBLIN_CORE_XML) results = [] # Check if each of the root's children from the generated XML # has a match in the string constant's XML. for child in generated_dxml_root.iterchildren(): # For each child, get all of the children with the same tag # from the XML being compared. same_tag_list = constant_dxml_root.findall(child.tag) element_found = False child_children = len(child.getchildren()) # Check if any of the possible elements are a match. for current in same_tag_list: if (current.text == child.text and current.tail == child.tail and current.attrib == child.attrib and len(current.getchildren()) == child_children): element_found = True break results.append(element_found) # If all elements came up with a match. Let it be True. self.assertTrue(all(results))
def xml_validator(some_xml_string, xsd_file): try: schema = etree.XMLSchema(file=xsd_file) parser = objectify.makeparser(schema=schema) objectify.fromstring(some_xml_string, parser) return None except XMLSyntaxError as xml_error: return six.text_type(xml_error)
def domainXMLDesc(dom, flags=0): xml = MockModel._XMLDesc(dom, flags) root = objectify.fromstring(xml) for dev_xml in MockModel._mock_vms.get(dom.name(), []): dev = objectify.fromstring(dev_xml) root.devices.append(dev) return ET.tostring(root, encoding="utf-8")
def __init__(self, obj): """obj can be easyXML data set""" from lxml import objectify try: self.root = objectify.fromstring(obj.data) except: # try something else self.root = objectify.fromstring(obj) self.obj = obj
def execute(self): self.endpoint = APIOperationBase.__environment logging.debug('Executing http post to url: %s', self.endpoint) self.beforeexecute() proxyDictionary = {'http' : utility.helper.getproperty("http"), 'https' : utility.helper.getproperty("https"), 'ftp' : utility.helper.getproperty("ftp")} #requests is http request try: self.setClientId() xmlRequest = self.buildrequest() self._httpResponse = requests.post(self.endpoint, data=xmlRequest, headers=constants.headers, proxies=proxyDictionary) except Exception as httpException: logging.error( 'Error retrieving http response from: %s for request: %s', self.endpoint, self.getprettyxmlrequest()) logging.error( 'Exception: %s, %s', type(httpException), httpException.args ) if self._httpResponse: self._httpResponse.encoding = constants.response_encoding self._httpResponse = self._httpResponse.text[3:] #strip BOM self.afterexecute() try: self._response = apicontractsv1.CreateFromDocument(self._httpResponse) #objectify code xmlResponse= self._response.toxml(encoding=constants.xml_encoding, element_name=self.getrequesttype()) xmlResponse = xmlResponse.replace(constants.nsNamespace1, b'') xmlResponse = xmlResponse.replace(constants.nsNamespace2, b'') self._mainObject = objectify.fromstring(xmlResponse) except Exception as objectifyexception: logging.error( 'Create Document Exception: %s, %s', type(objectifyexception), objectifyexception.args ) pyxb.GlobalValidationConfig._setForBinding(False) self._response = apicontractsv1.CreateFromDocument(self._httpResponse) #objectify code xmlResponse= self._response.toxml(encoding=constants.xml_encoding, element_name=self.getrequesttype()) xmlResponse = xmlResponse.replace(constants.nsNamespace1, b'') xmlResponse = xmlResponse.replace(constants.nsNamespace2, b'') self._mainObject = objectify.fromstring(xmlResponse) else: #if type(self.getresponseclass()) == type(self._response): if type(self.getresponseclass()) != type(self._mainObject): if self._response.messages.resultCode == "Error": logging.debug("Response error") domResponse = xml.dom.minidom.parseString(self._httpResponse) logging.debug('Received response: %s' % domResponse.toprettyxml()) else: #Need to handle ErrorResponse logging.debug('Error retrieving response for request: %s' % self._request) else: logging.debug("Did not receive http response") return
def xml_validator(some_xml_string, xsd_file='/path/to/my_schema_file.xsd'): try: schema = etree.XMLSchema(file=xsd_file) parser = objectify.makeparser(schema=schema) objectify.fromstring(some_xml_string, parser) print "YEAH!, my xml file has validated" except XMLSyntaxError: #handle exception here print "Oh NO!, my xml file does not validate" pass
def fromstring(text, schema=None): """Parses a KML text string This function parses a KML text string and optionally validates it against a provided schema object""" if schema: parser = objectify.makeparser(schema = schema.schema) return objectify.fromstring(text, parser=parser) else: return objectify.fromstring(text)
def test0010categories(self): ''' Test import with simple values ''' Product = POOL.get('product.product') IRLang = POOL.get('ir.lang') TreeNode = POOL.get('product.tree_node') dir = os.path.dirname(__file__) objectified_xml = objectify.fromstring( open(os.path.join(dir, 'simple_product.xml')).read() ) # get xml categories TreeNode._get_icecat_categorieslist_data = Mock( return_value=objectify.fromstring( open(os.path.join(dir, 'CategoriesList.xml')).read() ).xpath('//CategoriesList')[0] ) with Transaction().start(DB_NAME, USER, context=CONTEXT): # set translatable languages de_de, = IRLang.search([('code', '=', 'de_DE')]) de_de.translatable = True de_de.icecat_id = 2 de_de.save() bg_bg, = IRLang.search([('code', '=', 'bg_BG')]) bg_bg.translatable = True bg_bg.icecat_id = 20 # should not exist in test XML files bg_bg.save() cs_cz, = IRLang.search([('code', '=', 'cs_CZ')]) cs_cz.translatable = True cs_cz.icecat_id = 3 # has empty string in XML cs_cz.save() # Import the product product = Product.create_from_icecat_data(objectified_xml) self.assertEqual(product.nodes[0].name, "monitors CRT") # Test if both translated values got saved with Transaction().set_context(language='de_DE'): product = Product(product.id) self.assertEqual(product.nodes[0].name, "monitoren CRT") with Transaction().set_context(language='bg_BG'): product = Product(product.id) self.assertEqual(product.nodes[0].name, "monitors CRT") with Transaction().set_context(language='cz_CZ'): product = Product(product.id) self.assertEqual(product.nodes[0].name, "monitors CRT")
def testGOOD(self): settings.DEBUG = DEBUG self.queryDict["RETURN"] = 'GOOD' self.request.REQUEST = self.queryDict content = views.sync(self.request).content objTree = objectify.fromstring(content) removeSelfSource(objTree) actual = etree.tostring(objTree, pretty_print=True) verificationXSD.assertValid(objTree) expected = etree.tostring(objectify.fromstring(open(settings.BASE_PATH + "/nodes/" + settings.NODENAME + "/test/verGOOD.xml").read()), pretty_print=True) self.assertEquals(expected, actual)
def testSyncSelectMoleculeInchiKey(self): settings.DEBUG = DEBUG sql = "SELECT All WHERE (InchiKey='RWSOTUBLDIXVET-IQRQJSDFSA-N' AND RadTransWavenumber > 40 AND RadTransWavenumber < 45) OR (Inchi IN ('InChI=1S/H2O/h1H2') AND RadTransWavenumber > 1239.2185 AND RadTransWavenumber < 1239.2191)" self.queryDict["QUERY"] = sql self.request.REQUEST = self.queryDict objTree = objectify.fromstring(views.sync(self.request).content) removeSelfSource(objTree) actual = etree.tostring(objTree, pretty_print=True) xsamsXSD.assertValid(objTree) expected = etree.tostring(objectify.fromstring(open(settings.BASE_PATH + "/nodes/" + settings.NODENAME + "/test/InchiKey.xml").read()), pretty_print=True) self.assertEquals(expected, actual)
def get_item_variations_from_parent(parentASIN, amazon_api=None): ''' take an amazon "parent" ASIN returns a list of item variation ASINs ''' if amazon_api is None: amazon_api = get_amazon_api() # different product types handle related items differently, so we need to # know how to handle this product product_group = get_product_group(ASIN=parentASIN, amazon_api=amazon_api) if product_group in ('Book', 'Authority Non Buyable'): response = clean_response(amazon_api.ItemLookup(ItemId=parentASIN, ResponseGroup="RelatedItems", Condition='All', RelationshipType='AuthorityTitle')) logger.info('API Call to get variations from parent for {0}'.format(parentASIN)) root = objectify.fromstring(response) if hasattr(root.Items.Item, 'RelatedItems'): relatedItems = root.Items.Item.RelatedItems numberOfPages = relatedItems.RelatedItemPageCount variationASINs = [] for i in range(1, numberOfPages + 1): variationASINs.extend(get_book_variations_from_page(ASIN=parentASIN, page_number=i, amazon_api=amazon_api )) else: variationASINs = [parentASIN] else: response = clean_response(amazon_api.ItemLookup(ItemId=parentASIN, ResponseGroup="Variations", Condition='All')) logger.info('API Call to get variations from parent for {0}'.format(parentASIN)) root = objectify.fromstring(response) variationASINs = [] # if root.Items.Item.Variations doesn't exist, # there are no variations, so just the one version # which we add to the array and return. try: variations = root.Items.Item.Variations for v in variations.iterchildren(tag='Item'): varASIN = str(v.parentASIN).zfill(10) variationASINs.append(varASIN) except: variationASINs = [parentASIN] return variationASINs
def testSyncSelectMoleculeEnergy(self): settings.DEBUG = DEBUG sql = "SELECT All WHERE StateEnergy > 9895.327 AND StateEnergy < 9895.328" self.queryDict["QUERY"] = sql self.request.REQUEST = self.queryDict content = views.sync(self.request).content objTree = objectify.fromstring(content) removeSelfSource(objTree) actual = etree.tostring(objTree, pretty_print=True) xsamsXSD.assertValid(objTree) expected = etree.tostring(objectify.fromstring(open(settings.BASE_PATH + "/nodes/" + settings.NODENAME + "/test/Energy.xml").read()), pretty_print=True) self.assertEquals(expected, actual)
def parse_xml(self, validate=True): """Importar el contingut de l'xml""" self.check_fpos(self.f_xsd) schema = etree.XMLSchema(file=self.f_xsd) parser = objectify.makeparser(schema=schema) try: self.obj = objectify.fromstring(self.str_xml, parser) except Exception as e: self.error = e.message if validate: raise except_f1('Error', _(u'Document invà lid: {0}').format(e)) else: parser = objectify.makeparser(schema=None) self.obj = objectify.fromstring(self.str_xml, parser)
def validate_schema( xml_file, schema_file ): with open( xml_file, 'r' ) as f: try: schema = etree.XMLSchema( file=schema_file ) parser = objectify.makeparser( schema=schema ) objectify.fromstring( f.read(), parser ) except etree.XMLSyntaxError as e: print( '{} ({})'.format( e, xml_file ) ) return
def testSyncSelectMoleculeH_17OD_W_Int(self): settings.DEBUG = DEBUG sql = "SELECT All WHERE ((Inchi='InChI=1S/H2O/h1H2/i1+1/hD' AND RadTransWavenumber > 1234.23 AND RadTransWavenumber < 1244.24 AND RadTransProbabilityIdealisedIntensity < 1) AND MethodCategory = 'experiment')" self.queryDict["QUERY"] = sql self.request.REQUEST = self.queryDict content = views.sync(self.request).content objTree = objectify.fromstring(content) removeSelfSource(objTree) actual = etree.tostring(objTree, pretty_print=True) xsamsXSD.assertValid(objTree) expected = etree.tostring(objectify.fromstring(open(settings.BASE_PATH + "/nodes/" + settings.NODENAME + "/test/H_17OD_W_Int.xml").read()), pretty_print=True) self.assertEquals(expected, actual)
def testSyncSelectSaga2_co(self): settings.DEBUG = DEBUG sql = "SELECT All WHERE ((Inchi='InChI=1S/CO/c1-2' AND RadTransWavenumber > 2135.3135 AND RadTransWavenumber < 2135.3137) AND MethodCategory = 'experiment')" self.queryDict["QUERY"] = sql self.request.REQUEST = self.queryDict content = views.sync(self.request).content objTree = objectify.fromstring(content) removeSelfSource(objTree) actual = etree.tostring(objTree, pretty_print=True) xsamsXSD.assertValid(objTree) expected = etree.tostring(objectify.fromstring(open(settings.BASE_PATH + "/nodes/" + settings.NODENAME + "/test/co.xml").read()), pretty_print=True) self.assertEquals(expected, actual)
def similarity_lookup(self, ResponseGroup="Large", **kwargs): """Similarty Lookup. Returns up to ten products that are similar to all items specified in the request. Example: >>> api.similarity_lookup(ItemId='B002L3XLBO,B000LQTBKI') """ response = self.api.SimilarityLookup( ResponseGroup=ResponseGroup, **kwargs) root = objectify.fromstring(response) if root.Items.Request.IsValid == 'False': code = root.Items.Request.Errors.Error.Code msg = root.Items.Request.Errors.Error.Message raise SimilartyLookupException( "Amazon Similarty Lookup Error: '{0}', '{1}'".format( code, msg)) return [ AmazonProduct( item, self.aws_associate_tag, self.api, region=self.region ) for item in getattr(root.Items, 'Item', []) ]
def process_xml(self, xml): ''' Parse Outcome Request data from XML. ''' root = objectify.fromstring(xml) self.message_identifier = str(root.imsx_POXHeader.imsx_POXRequestHeaderInfo.imsx_messageIdentifier) try: result = root.imsx_POXBody.replaceResultRequest self.operation = REPLACE_REQUEST # Get result sourced id from resultRecord self.lis_result_sourcedid = result.resultRecord.sourcedGUID.sourcedId self.score = str(result.resultRecord.result.resultScore.textString) except: pass try: result = root.imsx_POXBody.deleteResultRequest self.operation = DELETE_REQUEST # Get result sourced id from resultRecord self.lis_result_sourcedid = result.resultRecord.sourcedGUID.sourcedId except: pass try: result = root.imsx_POXBody.readResultRequest self.operation = READ_REQUEST # Get result sourced id from resultRecord self.lis_result_sourcedid = result.resultRecord.sourcedGUID.sourcedId except: pass
def _ogone_s2s_get_tx_status(self, tx): account = tx.acquirer_id #reference = tx.reference or "ODOO-%s-%s" % (datetime.datetime.now().strftime('%Y%m%d_%H%M%S'), tx.partner_id.id) data = { 'PAYID': tx.acquirer_reference, 'PSPID': account.ogone_pspid, 'USERID': account.ogone_userid, 'PSWD': account.ogone_password, } query_direct_url = 'https://secure.ogone.com/ncol/%s/querydirect.asp' % (tx.acquirer_id.environment) _logger.debug("Ogone data %s", pformat(data)) request = urllib2.Request(query_direct_url, urlencode(data)) result = urllib2.urlopen(request).read() _logger.debug('Ogone response = %s', result) try: tree = objectify.fromstring(result) except etree.XMLSyntaxError: # invalid response from ogone _logger.exception('Invalid xml response from ogone') raise return tree
def load_string(self, xml): try: obj = objectify.fromstring(xml.encode('utf-8')) except Exception as e: log.error("Could not parse xml: %s " % e.args) raise Exception("Could not parse the XML response") #print type(obj) #print obj.get('info') self._response = { '_info': obj.get('info'), '_err': obj.get('err'), '_code': obj.get('code'), '_ret': obj.get('ret'), '_ts': obj.get('ts'), '_txn': obj.get('txn') } self._response['_ver'] = self._response['_info'][0:2] self._response['_uid_hash'] = self._response['_info'][2:66] self._response['_demo_hash'] = self._response['_info'][66:130] # => Turn the hex info into a flag def hextobin(s): bin_data = "" for i in range(0,len(s)): h=s[i] bin_data = bin_data + bin(int(h, 16))[2:].zfill(4) return bin_data usage_data =self._response['_info'][130:142] self._response['_usage_data'] = hextobin(usage_data) return
def lookup(self, ResponseGroup="Large", **kwargs): """Lookup an Amazon Product. :return: An instance of :class:`~.AmazonProduct` if one item was returned, or a list of :class:`~.AmazonProduct` instances if multiple items where returned. """ response = self.api.ItemLookup(ResponseGroup=ResponseGroup, **kwargs) root = objectify.fromstring(response) if root.Items.Request.IsValid == 'False': code = root.Items.Request.Errors.Error.Code msg = root.Items.Request.Errors.Error.Message raise LookupException( "Amazon Product Lookup Error: '{0}', '{1}'".format(code, msg)) if not hasattr(root.Items, 'Item'): raise AsinNotFound("ASIN(s) not found: '{0}'".format( etree.tostring(root, pretty_print=True))) if len(root.Items.Item) > 1: return [ AmazonProduct( item, self.aws_associate_tag, self, region=self.region) for item in root.Items.Item ] else: return AmazonProduct( root.Items.Item, self.aws_associate_tag, self, region=self.region )
def test_11_paypal_form_with_fees(self): # be sure not to do stupid things self.assertEqual(self.paypal.environment, 'test', 'test without test environment') # update acquirer: compute fees self.paypal.write({ 'fees_active': True, 'fees_dom_fixed': 1.0, 'fees_dom_var': 0.35, 'fees_int_fixed': 1.5, 'fees_int_var': 0.50, }) # render the button res = self.paypal.render( 'test_ref0', 12.50, self.currency_euro.id, values=self.buyer_values) # check form result handling_found = False tree = objectify.fromstring(res) self.assertEqual(tree.get('action'), 'https://www.sandbox.paypal.com/cgi-bin/webscr', 'paypal: wrong form POST url') for form_input in tree.input: if form_input.get('name') in ['handling']: handling_found = True self.assertEqual(form_input.get('value'), '1.57', 'paypal: wrong computed fees') self.assertTrue(handling_found, 'paypal: fees_active did not add handling input in rendered form')
def generate_report(qgc, args, WAS_SCAN_ID): ''' Generate scan report from scan_id ''' call = '/create/was/report' ServiceRequest_xml_header = ''' <ServiceRequest> <data> <Report> <name><![CDATA[with all parameters PDF]]></name> <description><![CDATA[A simple scan report]]></description> <format>PDF</format> <type>WAS_SCAN_REPORT</type> <config> <scanReport> <target> ''' ServiceRequest_xml_footer = ''' </target> <display> <contents> <ScanReportContent>DESCRIPTION</ScanReportContent> <ScanReportContent>SUMMARY</ScanReportContent> <ScanReportContent>GRAPHS</ScanReportContent> <ScanReportContent>RESULTS</ScanReportContent> <ScanReportContent>INDIVIDUAL_RECORDS</ScanReportContent> <ScanReportContent>RECORD_DETAILS</ScanReportContent> <ScanReportContent>ALL_RESULTS</ScanReportContent> <ScanReportContent>APPENDIX</ScanReportContent> </contents> <graphs> <ScanReportGraph>VULNERABILITIES_BY_SEVERITY</ScanReportGraph> <ScanReportGraph>VULNERABILITIES_BY_GROUP</ScanReportGraph> <ScanReportGraph>VULNERABILITIES_BY_OWASP</ScanReportGraph> <ScanReportGraph>VULNERABILITIES_BY_WASC</ScanReportGraph> <ScanReportGraph>SENSITIVE_CONTENTS_BY_GROUP</ScanReportGraph> </graphs> <groups> <ScanReportGroup>URL</ScanReportGroup> <ScanReportGroup>GROUP</ScanReportGroup> <ScanReportGroup>OWASP</ScanReportGroup> <ScanReportGroup>WASC</ScanReportGroup> <ScanReportGroup>STATUS</ScanReportGroup> <ScanReportGroup>CATEGORY</ScanReportGroup> <ScanReportGroup>QID</ScanReportGroup> </groups> <options> <rawLevels>true</rawLevels> </options> </display> <filters> <status> <ScanFindingStatus>NEW</ScanFindingStatus> <ScanFindingStatus>ACTIVE</ScanFindingStatus> <ScanFindingStatus>REOPENED</ScanFindingStatus> <ScanFindingStatus>FIXED</ScanFindingStatus> </status> </filters> </scanReport> </config> </Report> </data> </ServiceRequest> ''' scan_id_content = '<scans><WasScan><id>' + WAS_SCAN_ID + '</id></WasScan></scans>' parameters = ServiceRequest_xml_header + scan_id_content + ServiceRequest_xml_footer xml_output = qgc.request(call, parameters) root = objectify.fromstring(xml_output) logger.debug('xml_output: %s', xml_output) if root.responseCode != 'SUCCESS': logger.error('Error Found: %s', root.responseErrorDetails.errorMessage.text) sys.exit(1) else: REPORT_ID = root.data.Report.id.text logger.debug('Report id: %s', REPORT_ID) # Download report if REPORT_ID: call = '/download/was/report/' + REPORT_ID sleep_time = 60 while True: # get report status scan_status = get_report_status(qgc, REPORT_ID) if scan_status != 'COMPLETE': time.sleep(sleep_time) logger.info('Sleeping ... %s', sleep_time) else: break output = qgc.request(call) pdf_report_name = "Scan_Report_" + args.web_name + '_' + args.bld_num + '_' + args.scan_type_name + '_' + REPORT_ID + ".pdf" with open(pdf_report_name, "wb") as report: report.write(output) logger.info('Report has been downloaded successfully: %s', pdf_report_name)
# -*- coding: utf-8 -*- """ Created on Wed Sep 06 17:20:34 2017 @author: Yuna Noh """ import requests from lxml import objectify num_periods = "6" div = "0" state_id = "44" num_months = "8" year = "2016" template_base = 'https://www.ncdc.noaa.gov/temp-and-precip/climatological-rankings/download.xml?parameter=tavg&' template_add = template_base + 'state=%s&div=%s&month=%s&periods[]=%s&year=%s' insert_these = (state_id, div, num_months, num_periods, year) template_add = template_add % insert_these response = requests.get(template_add).content root = objectify.fromstring(response) my_wm_username = "******" print my_wm_username print root["data"]["value"] print root["data"]["twentiethCenturyMean"] print root["data"]["lowRank"] print root["data"]["highRank"]
def test_10_ogone_form_render(self): base_url = self.env['ir.config_parameter'].get_param('web.base.url') # be sure not to do stupid thing self.assertEqual(self.ogone.environment, 'test', 'test without test environment') # ---------------------------------------- # Test: button direct rendering + shasign # ---------------------------------------- form_values = { 'PSPID': 'dummy', 'ORDERID': 'test_ref0', 'AMOUNT': '1', 'CURRENCY': 'EUR', 'LANGUAGE': 'en_US', 'CN': 'Norbert Buyer', 'EMAIL': '*****@*****.**', 'OWNERZIP': '1000', 'OWNERADDRESS': 'Huge Street 2/543', 'OWNERCTY': 'Belgium', 'OWNERTOWN': 'Sin City', 'OWNERTELNO': '0032 12 34 56 78', 'SHASIGN': '815f67b8ff70d234ffcf437c13a9fa7f807044cc', 'ACCEPTURL': urls.url_join(base_url, OgoneController._accept_url), 'DECLINEURL': urls.url_join(base_url, OgoneController._decline_url), 'EXCEPTIONURL': urls.url_join(base_url, OgoneController._exception_url), 'CANCELURL': urls.url_join(base_url, OgoneController._cancel_url), } # render the button res = self.ogone.render('test_ref0', 0.01, self.currency_euro.id, partner_id=None, partner_values=self.buyer_values) # check form result tree = objectify.fromstring(res) self.assertEqual( tree.get('action'), 'https://secure.ogone.com/ncol/test/orderstandard.asp', 'ogone: wrong form POST url') for form_input in tree.input: if form_input.get('name') in ['submit']: continue self.assertEqual( form_input.get('value'), form_values[form_input.get('name')], 'ogone: wrong value for input %s: received %s instead of %s' % (form_input.get('name'), form_input.get('value'), form_values[form_input.get('name')])) # ---------------------------------------- # Test2: button using tx + validation # ---------------------------------------- # create a new draft tx tx = self.env['payment.transaction'].create({ 'amount': 0.01, 'acquirer_id': self.ogone.id, 'currency_id': self.currency_euro.id, 'reference': 'test_ref0', 'partner_id': self.buyer_id }) # render the button res = self.ogone.render('should_be_erased', 0.01, self.currency_euro, tx_id=tx.id, partner_id=None, partner_values=self.buyer_values) # check form result tree = objectify.fromstring(res) self.assertEqual( tree.get('action'), 'https://secure.ogone.com/ncol/test/orderstandard.asp', 'ogone: wrong form POST url') for form_input in tree.input: if form_input.get('name') in ['submit']: continue self.assertEqual( form_input.get('value'), form_values[form_input.get('name')], 'ogone: wrong value for form input %s: received %s instead of %s' % (form_input.get('name'), form_input.get('value'), form_values[form_input.get('name')]))
def __init__(self, output): self.issue_list = [] self.obj_xml = objectify.fromstring(output)
# -*- coding: utf-8 -*- """ Created on Tue May 31 16:32:02 2016 @author: james.bradley """ from lxml import objectify f_in = open('CourseScheduleMult.xml') strResponse = f_in.read() f_in.close() root = objectify.fromstring(strResponse) print(root.tag) for i in range(len(root)): print('\nCOURSE_NAME: ',root['COURSE'][i]["COURSE_NAME"]) for j in range(len(root['COURSE'][i]['SESSION'])): print(root['COURSE'][i]['SESSION'][j]['SESSION_NUMBER'],root['COURSE'][i]['SESSION'][j]['DAY'],root['COURSE'][i]['SESSION'][j]['SESSION_TIME'])
def test_find_tags_ecospold(self): o = objectify.fromstring(ecospold2_sample) self.assertEqual(len(find_tag(o, 'generalComment')), 1)
def process_xml(self, xml): ''' Parse tool configuration data out of the Common Cartridge LTI link XML. ''' root = objectify.fromstring(xml, parser=etree.XMLParser()) # Parse all children of the root node for child in root.getchildren(): if 'title' in child.tag: self.title = child.text if 'description' in child.tag: self.description = child.text if 'secure_launch_url' in child.tag: self.secure_launch_url = child.text elif 'launch_url' in child.tag: self.launch_url = child.text if 'icon' in child.tag: self.icon = child.text if 'secure_icon' in child.tag: self.secure_icon = child.text if 'cartridge_bundle' in child.tag: self.cartridge_bundle = child.attrib['identifierref'] if 'catridge_icon' in child.tag: self.cartridge_icon = child.atrib['identifierref'] if 'vendor' in child.tag: # Parse vendor tag for v_child in child.getchildren(): if 'code' in v_child.tag: self.vendor_code = v_child.text if 'description' in v_child.tag: self.vendor_description = v_child.text if 'name' in v_child.tag: self.vendor_name = v_child.text if 'url' in v_child.tag: self.vendor_url = v_child.text if 'contact' in v_child.tag: # Parse contact tag for email and name for c_child in v_child: if 'name' in c_child.tag: self.vendor_contact_name = c_child.text if 'email' in c_child.tag: self.vendor_contact_email = c_child.text if 'custom' in child.tag: # Parse custom tags for custom_child in child.getchildren(): self.custom_params[custom_child.attrib['name']] =\ custom_child.text if 'extensions' in child.tag: platform = child.attrib['platform'] properties = {} # Parse extension tags for ext_child in child.getchildren(): if 'property' in ext_child.tag: properties[ext_child.attrib['name']] = ext_child.text elif 'options' in ext_child.tag: opt_name = ext_child.attrib['name'] options = {} for option_child in ext_child.getchildren(): options[option_child.attrib['name']] =\ option_child.text properties[opt_name] = options self.set_ext_params(platform, properties)
8] not in notas: notas[line_split[8]] = {'mod': line_split[5]} elif line_split[1] == 'C100' and line_split[2] == '0' and line_split[ 8] in notas and notas[line_split[8]] != line_split[5]: notas[line_split[8]] = { 'mod': line_split[5], #'chNFe': line_split[9] } for file in glob.glob('nfe_entrada/*.xml'): arq = open(file, 'r', encoding='utf-8') texto = arq.read() texto = texto.encode('utf-8') arq.close() nfe = objectify.fromstring(texto) if hasattr(nfe, 'NFe'): if str(nfe.NFe.infNFe.ide.nNF) not in notas1: notas1[str(nfe.NFe.infNFe.ide.nNF)] = { 'mod': str(nfe.NFe.infNFe.ide.mod), 'chNFe': nfe.protNFe.infProt.chNFe } elif str(nfe.NFe.infNFe.ide.nNF) in notas1 and notas1[str( nfe.NFe.infNFe.ide.nNF)] != str(nfe.NFe.infNFe.ide.mod): notas1[str(nfe.NFe.infNFe.ide.nNF)] = { 'mod': str(nfe.NFe.infNFe.ide.mod), 'chNFe': nfe.protNFe.infProt.chNFe } if len(list(set(notas) - set(notas1))) > 0: print("Ta no SPED, mas não deveria esta:\n")
def __init__(self, xmlfile='insarProc.xml'): '''Constructor. Not much here.''' self.xmlfile = xmlfile fin = open(self.xmlfile) self.xml = OB.fromstring(fin.read()) fin.close()
def test_10_paypal_form_render(self): base_url = self.env['ir.config_parameter'].get_param('web.base.url') # be sure not to do stupid things self.paypal.write({ 'paypal_email_account': '*****@*****.**', 'fees_active': False }) self.assertEqual(self.paypal.environment, 'test', 'test without test environment') # ---------------------------------------- # Test: button direct rendering # ---------------------------------------- # render the button res = self.paypal.render('test_ref0', 0.01, self.currency_euro.id, values=self.buyer_values) form_values = { 'cmd': '_xclick', 'business': '*****@*****.**', 'item_name': 'YourCompany: test_ref0', 'item_number': 'test_ref0', 'first_name': 'Norbert', 'last_name': 'Buyer', 'amount': '0.01', 'currency_code': 'EUR', 'address1': 'Huge Street 2/543', 'city': 'Sin City', 'zip': '1000', 'country': 'BE', 'email': '*****@*****.**', 'return': '%s' % urlparse.urljoin(base_url, PaypalController._return_url), 'notify_url': '%s' % urlparse.urljoin(base_url, PaypalController._notify_url), 'cancel_return': '%s' % urlparse.urljoin(base_url, PaypalController._cancel_url), } # check form result tree = objectify.fromstring(res) self.assertEqual(tree.get('action'), 'https://www.sandbox.paypal.com/cgi-bin/webscr', 'paypal: wrong form POST url') for form_input in tree.input: if form_input.get('name') in ['submit']: continue self.assertEqual( form_input.get('value'), form_values[form_input.get('name')], 'paypal: wrong value for input %s: received %s instead of %s' % (form_input.get('name'), form_input.get('value'), form_values[form_input.get('name')]))
def get_domain_list(self, csv_file=None): """Fetch a list of registered domains for the specified Namecheap account. A valid API key, username, and whitelisted IP address must be used. The returned XML contains entries for domains like this: <RequestedCommand>namecheap.domains.getList</RequestedCommand> <CommandResponse Type="namecheap.domains.getList"> <DomainGetListResult> <Domain ID="127" Name="domain1.com" User="******" Created="02/15/2016" Expires="02/15/2022" IsExpired='False' IsLocked='False' AutoRenew='False' WhoisGuard="ENABLED" IsPremium="true" IsOurDNS="true"/> </DomainGetListResult> An optional csv file can be provided with domain information instead of using the Namecheap API to pull the data. """ domains_list = [] if csv_file: click.secho( '[+] Will try to use the provided csv file instead of accessing the Namecheap API.', fg='green') pass # TODO else: try: # The Namecheap API call requires both usernames, a key, and a whitelisted IP req = self.session.get( self.get_domain_list_endpoint.format( self.namecheap_api_username, self.namecheap_api_key, self.namecheap_username, self.client_ip, self.namecheap_page_size)) # Check if request returned a 200 OK if req.ok: # Convert Namecheap XML into an easy to use object for iteration root = objectify.fromstring(req.content) # Check the status to make sure it says "OK" namecheap_api_result = root.attrib['Status'] if namecheap_api_result == 'OK': # Get all "Domain" node attributes from the XML response click.secho( '[+] Namecheap returned status "{}"'.format( namecheap_api_result), fg='green') for domain in root.CommandResponse.DomainGetListResult.Domain: domains_list.append(domain.attrib) elif namecheap_api_result == 'ERROR': click.secho( '[!] Namecheap returned an "ERROR" response, so no domains were returned.', fg='red') if 'Invalid request IP' in req.text: click.secho( 'L.. You are not connecting to Namecheap using your whitelisted IP address.', fg='red') click.secho('Full Response:\n{}'.format(req.text), fg='red') else: click.secho( '[!] Namecheap did not return an "OK" response, so no domains were returned.', fg='red') click.secho('Full Response:\n{}'.format(req.text), fg='red') else: click.secho( '[!] Namecheap API request failed. Namecheap did not return a 200 response.', fg='red') click.secho('L.. API request returned status "{}"'.format( req.status_code), fg='red') except Exception as error: click.secho( '[!] Namecheap API request failed with error: {}'.format( error), fg='red') # There's a chance no domains are returned if the provided usernames don't have any domains if domains_list: return domains_list else: click.secho( '[!] No domains were returned for the provided account! Exiting...', fg='red') exit()
def load_func(self, data): return objectify.fromstring(data)
def test_10_Buckaroo_form_render(self): cr, uid, context = self.cr, self.uid, {} # be sure not to do stupid things buckaroo = self.payment_acquirer.browse(self.cr, self.uid, self.buckaroo_id, None) self.assertEqual(buckaroo.environment, 'test', 'test without test environment') # ---------------------------------------- # Test: button direct rendering # ---------------------------------------- form_values = { 'add_returndata': None, 'Brq_websitekey': buckaroo.brq_websitekey, 'Brq_amount': '2240.0', 'Brq_currency': 'EUR', 'Brq_invoicenumber': 'SO004', 'Brq_signature': '1b8c10074c622d965272a91a9e88b5b3777d2474', # update me 'brq_test': 'True', 'Brq_return': '%s' % urlparse.urljoin(self.base_url, BuckarooController._return_url), 'Brq_returncancel': '%s' % urlparse.urljoin(self.base_url, BuckarooController._cancel_url), 'Brq_returnerror': '%s' % urlparse.urljoin(self.base_url, BuckarooController._exception_url), 'Brq_returnreject': '%s' % urlparse.urljoin(self.base_url, BuckarooController._reject_url), 'Brq_culture': 'en-US', } # render the button res = self.payment_acquirer.render(cr, uid, self.buckaroo_id, 'SO004', 2240.0, self.currency_euro_id, partner_id=None, partner_values=self.buyer_values, context=context) # check form result tree = objectify.fromstring(res) self.assertEqual(tree.get('action'), 'https://testcheckout.buckaroo.nl/html/', 'Buckaroo: wrong form POST url') for form_input in tree.input: if form_input.get('name') in ['submit']: continue self.assertEqual( form_input.get('value'), form_values[form_input.get('name')], 'Buckaroo: wrong value for input %s: received %s instead of %s' % (form_input.get('name'), form_input.get('value'), form_values[form_input.get('name')])) # ---------------------------------------- # Test2: button using tx + validation # ---------------------------------------- # create a new draft tx tx_id = self.payment_transaction.create( cr, uid, { 'amount': 2240.0, 'acquirer_id': self.buckaroo_id, 'currency_id': self.currency_euro_id, 'reference': 'SO004', 'partner_id': self.buyer_id, }, context=context) # render the button res = self.payment_acquirer.render(cr, uid, self.buckaroo_id, 'should_be_erased', 2240.0, self.currency_euro, tx_id=tx_id, partner_id=None, partner_values=self.buyer_values, context=context) # check form result tree = objectify.fromstring(res) self.assertEqual(tree.get('action'), 'https://testcheckout.buckaroo.nl/html/', 'Buckaroo: wrong form POST url') for form_input in tree.input: if form_input.get('name') in ['submit']: continue self.assertEqual( form_input.get('value'), form_values[form_input.get('name')], 'Buckaroo: wrong value for form input %s: received %s instead of %s' % (form_input.get('name'), form_input.get('value'), form_values[form_input.get('name')]))
def get_report_status(qgc, report_id): call = '/status/was/report/' + report_id xml_output = qgc.request(call) root = objectify.fromstring(xml_output) return root.data.Report.status
def updateDeviceFlags(dom, xml, flags=0): root, old_dev = MockModel._get_device_node(dom, xml) root.devices.replace(old_dev, objectify.fromstring(xml)) MockModel._mock_vms[dom.name()] = ET.tostring(root, encoding="utf-8")
def _objectify(self, url, *args, **kwargs): doc = self._fetch(url, *args, **kwargs) return objectify.fromstring(doc)
def test_string_substitution_ecospold(self): o = objectify.fromstring(ecospold2_sample) self.assertEqual(render_text_block(find_tag(o, 'generalComment')), converted_string_ecospold)
def test_10_Authorize_form_render(self): authorize = self.env['payment.acquirer'].browse(self.authorize_id) self.assertEqual(authorize.environment, 'test', 'test without test environment') # ---------------------------------------- # Test: button direct rendering # ---------------------------------------- form_values = { 'x_login': authorize.authorize_login, 'x_trans_key': authorize.authorize_transaction_key, 'x_amount': '320.0', 'x_show_form': 'PAYMENT_FORM', 'x_type': 'AUTH_CAPTURE', 'x_method': 'CC', 'x_fp_sequence': '%s%s' % (authorize.id, int(time.time())), 'x_version': '3.1', 'x_relay_response': 'TRUE', 'x_fp_timestamp': str(int(time.time())), 'x_relay_url': '%s' % urlparse.urljoin(self.base_url, AuthorizeController._return_url), 'x_cancel_url': '%s' % urlparse.urljoin(self.base_url, AuthorizeController._cancel_url), 'return_url': None, 'x_currency_code': 'USD', 'x_invoice_num': 'SO004', 'x_first_name': 'Norbert', 'x_last_name': 'Buyer', 'x_address': 'Huge Street 2/543', 'x_city': 'Sin City', 'x_zip': '1000', 'x_country': 'Belgium', 'x_phone': '0032 12 34 56 78', 'x_email': '*****@*****.**', 'x_state': None, } form_values['x_fp_hash'] = self._authorize_generate_hashing( form_values) # render the button cr, uid, context = self.env.cr, self.env.uid, {} res = self.payment_acquirer.render(cr, uid, self.authorize_id, 'SO004', 320.0, self.currency_usd.id, values=self.buyer_values, context=context) # check form result tree = objectify.fromstring(res) self.assertEqual(tree.get('action'), 'https://test.authorize.net/gateway/transact.dll', 'Authorize: wrong form POST url') for form_input in tree.input: # Generated and received 'x_fp_hash' are always different so skeep it. if form_input.get('name') in ['submit', 'x_fp_hash']: continue self.assertEqual( form_input.get('value'), form_values[form_input.get('name')], 'Authorize: wrong value for input %s: received %s instead of %s' % (form_input.get('name'), form_input.get('value'), form_values[form_input.get('name')]))
def test_objectify(self): o = objectify.fromstring(ecospold2_sample) self.assertEqual(len(o), 1) self.assertEqual(len(o['childActivityDataset']), 1)
def test_10_Authorize_form_render(self): self.assertEqual(self.authorize.environment, 'test', 'test without test environment') # ---------------------------------------- # Test: button direct rendering # ---------------------------------------- base_url = self.env['ir.config_parameter'].get_param('web.base.url') form_values = { 'x_login': self.authorize.authorize_login, 'x_trans_key': self.authorize.authorize_transaction_key, 'x_amount': '56.16', 'x_show_form': 'PAYMENT_FORM', 'x_type': 'AUTH_CAPTURE', 'x_method': 'CC', 'x_fp_sequence': '%s%s' % (self.authorize.id, int(time.time())), 'x_version': '3.1', 'x_relay_response': 'TRUE', 'x_fp_timestamp': str(int(time.time())), 'x_relay_url': urls.url_join(base_url, AuthorizeController._return_url), 'x_cancel_url': urls.url_join(base_url, AuthorizeController._cancel_url), 'return_url': None, 'x_currency_code': 'USD', 'x_invoice_num': 'SO004', 'x_first_name': 'Norbert', 'x_last_name': 'Buyer', 'x_company': 'Big Company', 'x_address': 'Huge Street 2/543', 'x_city': 'Sin City', 'x_zip': '1000', 'x_country': 'Belgium', 'x_phone': '0032 12 34 56 78', 'x_email': '*****@*****.**', 'x_state': None, 'x_ship_to_first_name': 'Norbert', 'x_ship_to_last_name': 'Buyer', 'x_ship_to_address': 'Huge Street 2/543', 'x_ship_to_city': 'Sin City', 'x_ship_to_zip': '1000', 'x_ship_to_country': 'Belgium', 'x_ship_to_phone': '0032 12 34 56 78', 'x_ship_to_email': '*****@*****.**', 'x_ship_to_state': None, } form_values['x_fp_hash'] = self._authorize_generate_hashing(form_values) # render the button res = self.authorize.render('SO004', 56.16, self.currency_usd.id, values=self.buyer_values) # check form result tree = objectify.fromstring(res) data_set = tree.xpath("//input[@name='data_set']") self.assertEqual(len(data_set), 1, 'Authorize: Found %d "data_set" input instead of 1' % len(data_set)) self.assertEqual(data_set[0].get('data-action-url'), 'https://test.authorize.net/gateway/transact.dll', 'Authorize: wrong data-action-url POST url') for el in tree.iterfind('input'): values = list(el.attrib.values()) if values[1] in ['submit', 'x_fp_hash', 'return_url', 'x_state', 'x_ship_to_state', 'data_set']: continue self.assertEqual( values[2], form_values[values[1]], 'Authorize: wrong value for input %s: received %s instead of %s' % (values[1], values[2], form_values[values[1]]) )
def xml2vec(args): model_path = os.path.abspath(args.model_path) xml_dir = os.path.abspath(args.xml_dir) out_path = os.path.abspath(args.saved_embedding) print('\nmodel path : ', model_path) print(xml_dir) print(out_path) model = None if args.model_type == 'tf': from server.nlp.TensorflowModel import TensorflowModel model = TensorflowModel(args.lang) elif args.model_type == 'sent2vec': from server.nlp.Sent2VecModel import Sent2VecModel model = Sent2VecModel(args.lang) print('loading model... ') model.load_model(model_path) num_word = 0 vec_size = None # Writing to .vec file. vecfile_content = '' episodes = os.listdir(xml_dir) episodes.sort() # batched restuls list_of_file_path = [] list_of_text = [] list_of_embedding = [] embedding_dimension = 0 for episode in episodes: epi_dir = os.path.join(xml_dir, episode) if not os.path.isdir(epi_dir): continue print('\n## Episode : ', episode) tagged_xmls = os.listdir(os.path.join(xml_dir, episode)) tagged_xmls.sort() for tagged_xml in tagged_xmls: tagged_xml_path = os.path.join(epi_dir, tagged_xml) # TODO: check if it's relative path rel_path = os.path.join(episode, tagged_xml) if not tagged_xml_path.lower().endswith('.xml'): continue # open .xml with open(tagged_xml_path) as f: xml_string = f.read() if not xml_string: continue xml_root = objectify.fromstring(xml_string) # Embedding text is in xml['object']['name'] m_text = refine_text(str(xml_root['object']['name'])) embedding = model.embed_sentence(m_text) reshaped_embedding = embedding.reshape(-1) list_of_text.append(m_text) list_of_embedding.append(reshaped_embedding) embedding_dimension = len(reshaped_embedding) image_file_name = refine_text(str(xml_root['filename'])) list_of_file_path.append(os.path.join(episode, image_file_name)) print(str(tagged_xml) + ' ' + str(m_text)) # embedding shape - (total_file_num, 1, vector_size) print('Save to pickle..') saved_dict = {} saved_dict['text'] = list_of_text saved_dict['embedding'] = np.array(list_of_embedding) saved_dict['embedding_dimension'] = embedding_dimension saved_dict['file_path'] = list_of_file_path pickle.dump(saved_dict, open(out_path, 'wb'))
def retrieve_xml(url, session=requests.session()): response = session.get(url, timeout=30) response.raise_for_status() data = objectify.fromstring(response.content) return data
def _validate_xml(self, xml_signed): try: expense_line = self.env['tms.expense.line'].search( [('invoice_id', '=', self.id)]) xml_str = base64.decodestring(xml_signed).lstrip(BOM_UTF8) xml_str_rep = xml_str.replace( 'xmlns:schemaLocation', 'xsi:schemaLocation') xml = objectify.fromstring(xml_str_rep) xml_vat_emitter = xml.Emisor.get( 'rfc', xml.Emisor.get('Rfc', '')) xml_vat_receiver = xml.Receptor.get( 'rfc', xml.Receptor.get('Rfc', '')) xml_amount = xml.get( 'total', xml.get('Total', '')) xml_uuid = self.l10n_mx_edi_get_tfd_etree(xml) except AttributeError as ex: raise ValidationError(ex) except SyntaxError as ex: raise ValidationError( _('Error in XML structure\n%s \n %s') % str(ex), expense_line.name) validate_xml = False try: validate_xml = self._validate_xml_sat( xml_vat_emitter, xml_vat_receiver, xml_amount, xml_uuid) except ValidationError: self.message_post(_( 'Cannot be verified the SAT status for this document, please ' 'verify that the CFDI is valid before validate this record.')) if validate_xml and validate_xml.Estado == 'Cancelado': raise ValidationError( _('This XML state is CANCELED in the SAT system.\n' + 'Expense line: ' + expense_line.name + '\nTravel: ' + expense_line.travel_id.name)) if xml_uuid: xml_exists = self.search([('cfdi_uuid', '=', xml_uuid)]) if xml_exists: raise ValidationError(_( 'Can not attach this XML because other invoice already ' 'have the same UUID that this XML. \n Invoice: %s' '\n Line: %s') % ( xml_exists.number, expense_line.name)) inv_vat_receiver = self.company_id.address_parent_company_id.vat inv_vat_emitter = self.commercial_partner_id.vat inv_amount = self.amount_total or 0.0 msg = '' if not inv_vat_emitter: msg = (_("This supplier does not have VAT configured." "\n Supplier: %s \n Line: %s ") % (expense_line.partner_id.name, expense_line.name)) elif not inv_vat_receiver: msg = _("Please check that your company have a VAT configured.") elif inv_vat_receiver.upper() != xml_vat_receiver.upper(): msg = (_('The VAT receiver do not match with the Company.' '\n Company VAT: %s \n XML File: %s' '\n Expense Line: %s') % (inv_vat_receiver.upper(), xml_vat_receiver.upper(), expense_line.name)) elif inv_vat_emitter.upper() != xml_vat_emitter.upper(): msg = (_( 'The VAT emitter do not match with the supplier invoice.' '\n Supplier VAT: %s \n XML File: %s' '\n Expense Line: %s ') % ( inv_vat_emitter.upper(), xml_vat_emitter.upper(), expense_line.name)) # Use 2 as precision rounding by the decimals in XML elif float_compare( float(inv_amount), float(xml_amount), precision_rounding=6): msg = (_( "The invoice total not match with the XML file." "\n Amount Total Invoice: %s \n XML File: %s" "\n Expense Line: %s") % ( inv_amount, xml_amount, expense_line.name)) if msg: raise ValidationError(msg) self.write({ 'cfdi_uuid': xml_uuid, 'xml_signed': base64.decodestring(xml_signed)}) name = expense_line.xml_filename if not self.xml_signed: return False data_attach = { 'name': name, 'datas': base64.encodestring( self.xml_signed and self.xml_signed.lstrip(BOM_UTF8U).encode('UTF-8') or ''), 'datas_fname': name, 'description': _('XML signed from Invoice %s.' % self.number), 'res_model': self._name, 'res_id': self.id, } self.env['ir.attachment'].with_context({}).create(data_attach) return True
def getAnnotations(annfile): ''' Read XML annotations file, objectify and return ''' with open(annfile) as f: xml = f.read() return objectify.fromstring(xml)
def parse_xml(xml_file): with open(xml_file) as f: xml = f.read() root = objectify.fromstring(xml) return root
def load_metadata(metadata_file: IO[bytes], provider: Provider = None) -> EPTSMetadata: root = objectify.fromstring(metadata_file.read()) metadata = root.find("Metadata") provider = _load_provider(metadata, provider) score_path = objectify.ObjectPath( "Metadata.Sessions.Session[0].MatchParameters.Score") score_elm = score_path.find(metadata) score = Score(home=score_elm.LocalTeamScore, away=score_elm.VisitingTeamScore) _team_map = { Ground.HOME: score_elm.attrib["idLocalTeam"], Ground.AWAY: score_elm.attrib["idVisitingTeam"], } _team_name_map = { team_elm.attrib["id"]: str(team_elm.find("Name")) for team_elm in metadata.find("Teams").iterchildren(tag="Team") } teams_metadata = {} for ground, team_id in _team_map.items(): team = Team(team_id=team_id, name=_team_name_map[team_id], ground=ground) team.players = _load_players(metadata.find("Players"), team) teams_metadata.update({ground: team}) data_format_specifications = _load_data_format_specifications( root.find("DataFormatSpecifications")) device_path = objectify.ObjectPath("Metadata.Devices.Device[0].Sensors") sensors = _load_sensors(device_path.find(metadata)) _channel_map = { channel.channel_id: channel for sensor in sensors for channel in sensor.channels } _all_players = [ player for key, value in teams_metadata.items() for player in value.players ] _player_map = {player.player_id: player for player in _all_players} player_channels = [ PlayerChannel( player_channel_id=player_channel_elm.attrib["id"], player=_player_map[player_channel_elm.attrib["playerId"]], channel=_channel_map[player_channel_elm.attrib["channelId"]], ) for player_channel_elm in metadata.find("PlayerChannels").iterchildren( tag="PlayerChannel") ] frame_rate = int(metadata.find("GlobalConfig").find("FrameRate")) pitch_dimensions = _load_pitch_dimensions(metadata, sensors) periods = _load_periods(metadata.find("GlobalConfig"), frame_rate) if periods: start_attacking_direction = periods[0].attacking_direction else: start_attacking_direction = None orientation = ( (Orientation.FIXED_HOME_AWAY if start_attacking_direction == AttackingDirection.HOME_AWAY else Orientation.FIXED_AWAY_HOME) if start_attacking_direction != AttackingDirection.NOT_SET else None) metadata.orientation = orientation if provider and pitch_dimensions: from_coordinate_system = build_coordinate_system( provider, length=pitch_dimensions.length, width=pitch_dimensions.width, ) else: from_coordinate_system = None return EPTSMetadata( teams=list(teams_metadata.values()), periods=periods, pitch_dimensions=pitch_dimensions, data_format_specifications=data_format_specifications, player_channels=player_channels, frame_rate=frame_rate, sensors=sensors, score=score, orientation=None, provider=provider, flags=~(DatasetFlag.BALL_STATE | DatasetFlag.BALL_OWNING_TEAM), coordinate_system=from_coordinate_system, )
def _get_xml_autoconfig(self, url, email): try: result = {'sources': [], 'routes': []} xml_data = self._urlget(url) if xml_data: data = objectify.fromstring(xml_data) # FIXME: Massage these so they match the format of the routes and # sources more closely. Also look out and report the visiturl to # handle GMail. OAuth2 is coming up as an auth mech, we will need to # support it: https://bugzilla.mozilla.org/show_bug.cgi?id=1166625 try: for enable in data.emailProvider.enable: result['enable'] = result.get('enable', []) result['enable'].append({ 'url': enable.get('visiturl', ''), 'description': str(enable.instruction) }) except AttributeError: pass try: for docs in data.emailProvider.documentation: result['docs'] = result.get('docs', []) result['docs'].append({ 'url': docs.get('url', ''), 'description': str(docs.descr) }) except AttributeError: pass for insrv in data.emailProvider.incomingServer: result['sources'].append({ 'protocol': self._source_proto(insrv), 'username': self._username(insrv.username, email), 'auth_type': str(insrv.authentication), 'host': str(insrv.hostname), 'port': str(insrv.port), }) for outsrv in data.emailProvider.outgoingServer: result['routes'].append({ 'protocol': self._route_proto(outsrv), 'username': self._username(outsrv.username, email), 'auth_type': str(outsrv.authentication), 'host': str(outsrv.hostname), 'port': str(outsrv.port), }) result['sources'].sort(key=self._rank) result['routes'].sort(key=self._rank) return result except (IOError, ValueError, AttributeError): # don't forget to delete this # import traceback # traceback.print_exc() return None
def scan_report(qgc, current_time, args, scan_id): call = '/launch/was/wasscan' ServiceRequest_xml_header = ''' <ServiceRequest> <data> <WasScan> ''' ServiceRequest_xml_footer = ''' </WasScan> </data> </ServiceRequest> ''' web_scan_name = '<name>' + args.web_name + ' - ' + args.scan_type_name + ' WAS Scan Launch From API - ' + current_time + '</name>' + '\n' scan_type = '<type>' + args.scan_type_name + '</type>' scan_id_content = '<target><webApp><id>' + scan_id + '</id>' content = ''' </webApp> <webAppAuthRecord> <isDefault>true</isDefault> </webAppAuthRecord> <scannerAppliance> <type>INTERNAL</type> <friendlyName>CB_Scanner_Xen</friendlyName> </scannerAppliance> </target> <profile> ''' profile_id = '<id>' + args.profile_id + '</id> </profile>' parameters = ServiceRequest_xml_header + web_scan_name + scan_type + scan_id_content + content + profile_id + '\n' + ServiceRequest_xml_footer logger.debug('parameters: %s', parameters) xml_output = qgc.request(call, parameters) root = objectify.fromstring(xml_output) logger.debug('xml_output: %s', xml_output) # Need to check update results if root.responseCode != 'SUCCESS': logger.error('Error Found: %s', root.responseErrorDetails.errorMessage.text) sys.exit(1) else: SCAN_ID = root.data.WasScan.id.text logger.info('id: %s', xml_output) # get scan status call = '/status/was/wasscan' + '/' + SCAN_ID sleep_time = 180 while True: xml_output = qgc.request(call) scan_root = objectify.fromstring(xml_output) if scan_root.data.WasScan.status != 'FINISHED': time.sleep(sleep_time) logger.info('Sleeping ... %s', sleep_time) else: break #logger.debug('xml_output: %s', xml_output) # Need to check update results if scan_root.responseCode != 'SUCCESS': logger.error('Error Found: %s', scan_root.responseErrorDetails.errorMessage.text) sys.exit(1) # elif scan_root.responseCode == 'SUCCESS' and scan_root.data.WasScan.summary is not None: # print("Error Found: {}".format(scan_root.data.WasScan.summary.resultsStatus.text)) # sys.exit(1) else: logger.info('Scan finished successfully!') logger.info('Scan id: %s', scan_root.data.WasScan.id.text) return scan_root.data.WasScan.id.text
#from klaatu.components.distributor.googledrive import Distributor from klaatu.util.xmlutil.XMLWalk import XMLWalker, XPathAnalyzer from lxml import objectify, etree import numpy as np import sys #db = Distributor() #files = db.ls('infinite_result') #s = db.get(files[2]['id']) #root = objectify.fromstring(s, parser=None) #root = objectify.parse('filename.xml') root = objectify.fromstring( etree.tostring( objectify.parse('E_PTT_BeamLocation_A12345678W_20140729_225738.xml'))) print etree.tostring(root, pretty_print=True) # root.xpath('//Section/Data/Well/Multiple') # root.xpath('//Section[@Name='Label1']/Data/Well/Multiple') # root.xpath('//Section[@Name='Label1']/Data/Well/Multiple?pos=@MRW_Position') # root.xpath('//Section[@Name='Label1']/Data/Well/Multiple/@MRW_Position') wk = XMLWalker(root) data = wk.walk( XPathAnalyzer( "//Section[@Name='Label1']/Data/Well?well=@Pos/Multiple?pos=@MRW_Position&value='/text()'" ))
def read_xml_sourcefile(file_name): all_text = open(file_name).read() return objectify.fromstring(all_text)