def atom2odata(self, atom_root, entity_class=None, timings=[], messages=[]): ''' Converts standard EAI Atom feed into odata collection object ''' output = ODataCollection() if atom_root: timings.append(('app.xml_parse_start', time.time())) root = et.fromstring(atom_root) timings.append(('app.odata_create_start', time.time())) # first pick off the generic messaging format if root.tag == 'response': output = self.response2odata(root, entity_class) # handle the single entry mode elif root.tag == '{http://www.w3.org/2005/Atom}entry': output = self.entry2odata(root, entity_class) # otherwise assume collection else: for node in root.xpath('a:entry', namespaces={'a': ATOM_NS}): output.items.append(self.entry2odata(node, entity_class)) # set collection data try: output.offset = int( root.xpath('o:startIndex', namespaces={'o': OPENSEARCH_NS})[0].text) output.total_count = int( root.xpath('o:totalResults', namespaces={'o': OPENSEARCH_NS})[0].text) output.count = min( output.total_count, len(root.xpath('a:entry', namespaces={'a': ATOM_NS}))) except: pass timings.append(('app.odata_create_end', time.time())) output.timings = timings output.messages.extend(messages) return output
def atom2odata(self, atom_root, entity_class=None, timings=[], messages=[]): """ Converts standard EAI Atom feed into odata collection object """ output = ODataCollection() if atom_root: timings.append(("app.xml_parse_start", time.time())) root = et.fromstring(atom_root) timings.append(("app.odata_create_start", time.time())) # first pick off the generic messaging format if root.tag == "response": output = self.response2odata(root, entity_class) # handle the single entry mode elif root.tag == "{http://www.w3.org/2005/Atom}entry": output = self.entry2odata(root, entity_class) # otherwise assume collection else: for node in root.xpath("a:entry", namespaces={"a": ATOM_NS}): output.items.append(self.entry2odata(node, entity_class)) # set collection data try: output.offset = int(root.xpath("o:startIndex", namespaces={"o": OPENSEARCH_NS})[0].text) output.total_count = int(root.xpath("o:totalResults", namespaces={"o": OPENSEARCH_NS})[0].text) output.count = min(output.total_count, len(root.xpath("a:entry", namespaces={"a": ATOM_NS}))) except: pass timings.append(("app.odata_create_end", time.time())) output.timings = timings output.messages.extend(messages) return output
def atom2odata(self, atom_root, entity_class=None, timings=[], messages=[]): ''' Converts standard EAI Atom feed into odata collection object ''' output = ODataCollection() if atom_root: timings.append(('app.xml_parse_start', time.time())) root = et.fromstring(atom_root) # service may return messages in the body; try to parse them try: msg = splunk.rest.extractMessages(root) if msg: messages.extend(msg) except: pass timings.append(('app.odata_create_start', time.time())) # handle the single entry mode if root.tag == '{http://www.w3.org/2005/Atom}entry': output = self.entry2odata(root, entity_class) # otherwise assume collection else: for node in root.xpath('a:entry', namespaces={'a': ATOM_NS}): output.items.append(self.entry2odata(node, entity_class)) # set collection data try: try: output.offset = int(root.xpath('o:startIndex', namespaces={'o': OPENSEARCH_NS})[0].text) output.total_count = int(root.xpath('o:totalResults', namespaces={'o': OPENSEARCH_NS})[0].text) except: output.total_count = None pass # We might not have a total_count field, so we have to check if it is "none" or actually # 0, since they are both false-y values if output.total_count is None: output.count = len(root.xpath('a:entry', namespaces={'a': ATOM_NS})) output.total_count = output.count else: output.count = min(output.total_count, len(root.xpath('a:entry', namespaces={'a': ATOM_NS}))) output.id = root.xpath('a:id', namespaces={'a': ATOM_NS})[0].text for link in root.xpath('a:link', namespaces={'a': ATOM_NS}): output.metadata.links.append({ 'href': link.get('href'), 'rel': link.get('rel') }) output.name = root.xpath('a:title', namespaces={'a': ATOM_NS})[0].text except: pass timings.append(('app.odata_create_end', time.time())) output.timings = timings output.messages = messages return output
def atom2odata(self, atom_root, entity_class=None, timings=[], messages=[]): ''' Converts standard EAI Atom feed into odata collection object ''' output = ODataCollection() if atom_root: timings.append(('app.xml_parse_start', time.time())) root = et.fromstring(atom_root) # service may return messages in the body; try to parse them try: msg = splunk.rest.extractMessages(root) if msg: messages.extend(msg) except: pass timings.append(('app.odata_create_start', time.time())) # handle the single entry mode if root.tag == '{http://www.w3.org/2005/Atom}entry': output = self.entry2odata(root, entity_class) # otherwise assume collection else: for node in root.xpath('a:entry', namespaces={'a': ATOM_NS}): output.items.append(self.entry2odata(node, entity_class)) # set collection data try: try: output.offset = int( root.xpath('o:startIndex', namespaces={'o': OPENSEARCH_NS})[0].text) output.total_count = int( root.xpath('o:totalResults', namespaces={'o': OPENSEARCH_NS})[0].text) except: output.total_count = None pass # We might not have a total_count field, so we have to check if it is "none" or actually # 0, since they are both false-y values if output.total_count is None: output.count = len( root.xpath('a:entry', namespaces={'a': ATOM_NS})) output.total_count = output.count else: output.count = min( output.total_count, len( root.xpath('a:entry', namespaces={'a': ATOM_NS}))) output.id = root.xpath('a:id', namespaces={'a': ATOM_NS})[0].text for link in root.xpath('a:link', namespaces={'a': ATOM_NS}): output.metadata.links.append({ 'href': link.get('href'), 'rel': link.get('rel') }) output.name = root.xpath('a:title', namespaces={'a': ATOM_NS})[0].text except: pass timings.append(('app.odata_create_end', time.time())) output.timings = timings output.messages = messages return output