def encode_atomxml(obj_d): """ encode instance of OwcContext dict into atom xml encoding, because we can't do circular imports :param obj_d: the dict from owscontext to dict :return: b'atomxml' """ # try: # xml_tree = axml_context(obj_d) # tree = etree.ElementTree(xml_tree) # return tree # except TypeError as te: # log.warning('problem encoding context to xml', te) # raise te # except AttributeError as ae: # log.warning('problem encoding context to xml', ae) # raise ae # except ValueError as ve: # log.warning('problem encoding context to xml', ve) # raise ve # except ParseError as pe: # log.warning('problem encoding context to xml', pe) # raise pe xml_tree = axml_context(obj_d) tree = etree.ElementTree(xml_tree) return element_to_string(tree, encoding='utf-8', xml_declaration=False)
def _get_etree_for_record(self, uuid): """ Set self.record_etree to etree ElementTree of record with inputted uuid. """ xml = self.records[uuid].xml root = etree.fromstring(xml) return etree.ElementTree(root)
def getrecordbyid(self, ids=[], esn=u'full', outputschema=u'gmd', **kw): ''' :param ids: (Default value = []) :param esn: (Default value = u'full') :param outputschema: (Default value = u'gmd') :param **kw: ''' from owslib.csw import namespaces csw = self._ows(**kw) kwa = { u'esn': esn, u'outputschema': namespaces[outputschema], } # Ordinary Python version's don't support the metadata argument log.info(u'Making CSW request: getrecordbyid %r %r', ids, kwa) csw.getrecordbyid(ids, **kwa) if csw.exceptionreport: err = u'Error getting record by id: %r' % \ csw.exceptionreport.exceptions # log.error(err) raise CswError(err) if not csw.records: return record = self._xmd(csw.records.values()[0]) ## strip off the enclosing results container, we only want the metadata # md = csw._exml.find("/gmd:MD_Metadata")#, namespaces=namespaces) # Ordinary Python version's don't support the metadata argument md = csw._exml.find('/{http://www.isotc211.org/2005/gmd}MD_Metadata') mdtree = etree.ElementTree(md) try: record[u'xml'] = etree.tostring(mdtree, pretty_print=True, encoding=unicode) except TypeError: # API incompatibilities between different flavours of elementtree try: record[u'xml'] = etree.tostring(mdtree, pretty_print=True, encoding=unicode) except AssertionError: record[u'xml'] = etree.tostring(md, pretty_print=True, encoding=unicode) record[u'xml'] = u'<?xml version="1.0" encoding="UTF-8"?>\n' + record[ u'xml'] record[u'tree'] = mdtree return record
def getrecordbyid(self, ids=[], esn="full", outputschema="gmd", **kw): from owslib.csw import namespaces csw = self._ows(**kw) kwa = { "esn": esn, "outputschema": namespaces[outputschema], } # Ordinary Python version's don't support the metadata argument log.info('Making CSW request: getrecordbyid %r %r', ids, kwa) csw.getrecordbyid(ids, **kwa) if csw.exceptionreport: err = 'Error getting record by id: %r' % \ csw.exceptionreport.exceptions #log.error(err) raise CswError(err) if not csw.records: return record = self._xmd(csw.records.values()[0]) ## strip off the enclosing results container, we only want the metadata #md = csw._exml.find("/gmd:MD_Metadata")#, namespaces=namespaces) # Ordinary Python version's don't support the metadata argument md = csw._exml.find("/{http://www.isotc211.org/2005/gmd}MD_Metadata") if outputschema == 'fgdc': md = csw._exml.find("/metadata") mdtree = etree.ElementTree(md) try: record["xml"] = etree.tostring(mdtree, pretty_print=True, encoding=unicode) except TypeError: # API incompatibilities between different flavours of elementtree try: record["xml"] = etree.tostring(mdtree, pretty_print=True, encoding=unicode) except AssertionError: record["xml"] = etree.tostring(md, pretty_print=True, encoding=unicode) record[ "xml"] = '<?xml version="1.0" encoding="UTF-8"?>\n' + record["xml"] record["tree"] = mdtree return record