def getRecordsByTags(tags): """ Method to get records by tags from geonetwork Attention: The tags received are case sensitive! PropertyIsLike is sensitive, matchCase not possible here. See https://trac.osgeo.org/geonetwork/wiki/CSW202Improvements This method can be called by @app.route('/metadata/raw/tags/<tags>') """ log.debug('looking for tags ' + str(tags)) try: url = GEONETWORK.csw_url tpl = tplEnv.get_template('geonetwork/post_records_by_tags.xml') tags = tags.split(',') postbody = tpl.render(tags=tags).replace('\n', '') headers = {'content-type': 'application/xml; charset=utf-8'} except Exception as e: log.error('Could not set needed variable') log.error(e) return None try: gnosresp = requests.post( url, data=postbody, headers=headers, auth=auth(GEONETWORK) ) return gnosresp.content except requests.exceptions.ConnectionError: log.error('Could not connect to gnos') return None
def getRecordsByTags(tags): """ Method to get records by tags from geonetwork Attention: The tags received are case sensitive! PropertyIsLike is sensitive, matchCase not possible here. See https://trac.osgeo.org/geonetwork/wiki/CSW202Improvements This method can be called by @app.route('/metadata/raw/tags/<tags>') """ url = GEONETWORK.csw_url tpl = tplEnv.get_template('geonetwork/post_records_by_tags.xml') tags = tags.split(',') postbody = tpl.render(tags=tags).replace('\n', '') headers = {'content-type': 'application/xml; charset=utf-8'} try: gnosresp = requests.post( url, data=postbody, headers=headers, auth=auth(GEONETWORK) ) except requests.exceptions.ConnectionError: return None try: parsedresp = xmltodict.parse(gnosresp.content) records = json.dumps(parsedresp) return records except Exception: return None
def update(uuid, utcnow): """ Method to update record in geonetwork """ connection = checkConnectionWithoutResponse('geonetwork') if connection is None: log.error('Not updating metadata for uuid ' + uuid) return None try: response = getRecordByUUID(uuid) doc = parseString(response.decode('utf-8')) recordNode = doc.getElementsByTagName('gmd:MD_Metadata')[0] log.debug('Found metadata to update for ' + uuid + ', and ' + str(recordNode)) except Exception: log.error('Could not find metadata record to update for uuid ' + uuid) return None record = updateXml(response, utcnow) if record is None: return None try: url = GEONETWORK.csw_pub postbodytpl = tplEnv.get_template('geonetwork/post_update_record.xml') postbody = postbodytpl.render(metadata_record=record, uuid=uuid).replace('\n', '') headers = {'content-type': 'application/xml; charset=utf-8'} except Exception as e: log.error('Could not set needed variable') log.error(e) return None try: log.info('Updating metadata record') gnosresp = requests.post(url, data=bytes(postbody, 'utf-8'), headers=headers, auth=auth(GEONETWORK)) if '<html>' in gnosresp.content.decode('utf-8'): log.error('update error') else: log.info('update success') return gnosresp except requests.exceptions.ConnectionError: log.error('Could not connect to gnos') return None except Exception as e: log.error(e) return None
def postActiniaCore(process, preProcessChain): """ Method to start new job in actinia-core Args: process: (str): the process which is triggered preProcessChain: (dict): the enriched preProcessChain object Returns: TODO: (dict): status url of actinia-core job """ url = ACTINIACORE.url + PROCESSING_ENDPOINT headers = {'content-type': 'application/json; charset=utf-8'} if process == 'test': postbody = buildPCDummy(preProcessChain) elif process == 'sentinel1': postbody = buildPCS1Grd(preProcessChain) else: postbody = buildPCDummy(preProcessChain) try: # TODO: for now if error above, here an empty postbody is send to # actinia-core. This leads to correct status "ERROR" at the moment. # Make smarter (not call actinia-core, return correct error msg) log.info('Posting to ' + url) actiniaResp = requests.post(url, data=postbody, headers=headers, auth=auth(ACTINIACORE)) # TODO: remove GET request here. It is a workaround for a bug in # actinia-core which does not start a job if no request follows actiniaCoreId = json.loads(actiniaResp.text)['resource_id'] url = ACTINIACORE.url + "resources/actinia-gdi/" + actiniaCoreId actiniaResp2 = requests.get(url, auth=auth(ACTINIACORE)) except requests.exceptions.ConnectionError: return None return json.loads(actiniaResp2.text)
def create(filename): """ Method to get create metadata records in geonetwork """ url = GEONETWORK.csw_publication recordtpl = tplEnv.get_template('geonetwork/template_metadaten.xml') record = recordtpl.render(title=filename).replace('\n', '') recordfs = recordtpl.render(title=filename) postbodytpl = tplEnv.get_template('geonetwork/post_create_record.xml') postbody = postbodytpl.render(metadata_record=record).replace('\n', '') postbodyfs = postbodytpl.render(metadata_record=recordfs) headers = {'content-type': 'application/xml; charset=utf-8'} log.info('Creating metadata record') try: gnosresp = requests.post(url, data=postbody, headers=headers, auth=auth(GEONETWORK)) except requests.exceptions.ConnectionError: return None if not os.path.isdir(FILEUPLOAD.templates): os.makedirs(FILEUPLOAD.templates) with open(FILEUPLOAD.templates + '/' + filename + '_template.xml', 'x') as file: file.write(postbodyfs) log.info('Received binary file, saved to ' + FILEUPLOAD.templates + '/' + filename + '_template.xml') try: parsedresp = xmltodict.parse(gnosresp.content) insertRes = parsedresp['csw:TransactionResponse']['csw:InsertResult'] uuid = insertRes['csw:BriefRecord']['identifier'] log.info('GNOS response is: ' + str(parsedresp)) return uuid except Exception: return None
def getRecordByUUID(uuid): """ Method to get record by uuid from geonetwork This method can be called by @app.route('/metadata/raw/uuids/<uuid>') """ url = GEONETWORK.csw_url tpl = tplEnv.get_template('geonetwork/get_record_by_uuid_kvp.json') kvps = json.loads(tpl.render(uuid=uuid)) try: gnosresp = requests.get(url, params=kvps, auth=auth(GEONETWORK)) except requests.exceptions.ConnectionError: return None try: parsedresp = xmltodict.parse(gnosresp.content) records = json.dumps(parsedresp) return records except Exception: return None
def getRecordByUUID(uuid): """ Method to get record by uuid from geonetwork This method can be called by @app.route('/metadata/raw/uuids/<uuid>') """ try: url = GEONETWORK.csw_url tpl = tplEnv.get_template('geonetwork/get_record_by_uuid_kvp.json') kvps = json.loads(tpl.render(uuid=uuid)) except Exception as e: log.error('Could not set needed variable') log.error(e) return None try: gnosresp = requests.get(url, params=kvps, auth=auth(GEONETWORK)) return gnosresp.content except requests.exceptions.ConnectionError: log.error('Could not connect to gnos') return None
def getRecordsByCategory(category): """ Method to get records by category from geonetwork This method can be called by @app.route('/metadata/raw/categories/<category>') """ url = (GEONETWORK.csw_url + '-' + category) tpl = tplEnv.get_template('geonetwork/get_records_by_category_kvp.json') kvps = json.loads(tpl.render()) try: gnosresp = requests.get(url, params=kvps, auth=auth(GEONETWORK)) except requests.exceptions.ConnectionError: return None try: parsedresp = xmltodict.parse(gnosresp.content) records = json.dumps(parsedresp) return records except Exception: return None
def getRecordsByCategory(category): """ Method to get records by category from geonetwork This method can be called by @app.route('/metadata/raw/categories/<category>') """ try: url = (GEONETWORK.csw_url + '-' + category) tpl = tplEnv.get_template('geonetwork/get_records_by_category_kvp.json') kvps = json.loads(tpl.render()) except Exception as e: log.error('Could not set needed variable') log.error(e) return None try: gnosresp = requests.get(url, params=kvps, auth=auth(GEONETWORK)) return gnosresp.content except requests.exceptions.ConnectionError: log.error('Could not connect to gnos') return None
def cancelActiniaCore(resourceId): """ Wrapper method to cancel a job by using the jobId This method is called by HTTP POST @app.route('/processes/test/jobs/<jobid>/operations/cancel') This method is calling core method readJob Args: resourceId: (string): actinia-core job id """ if resourceId is None: return make_response("Not found", 404) print("\n Received HTTP DELETE request for job with actinia-core jobID " + resourceId) DELETING_ENDPOINT = 'resources/' url = (ACTINIACORE.url + DELETING_ENDPOINT + ACTINIACORE.user + '/' + resourceId) log.info('Requesting from ' + url) try: actiniaResp = requests.delete(url, auth=auth(ACTINIACORE)) except requests.exceptions.ConnectionError: return None try: status = parseActiniaAsyncStatusResponse(json.loads(actiniaResp.text), "status") message = parseActiniaAsyncStatusResponse(json.loads(actiniaResp.text), "message") except Exception: return None log.info('Status is "' + status + " - " + message + '"') return True