Skip to content

Commit

Permalink
#367 support resource_type and source_type options
Browse files Browse the repository at this point in the history
  • Loading branch information
eblondel committed Oct 9, 2023
1 parent a768cce commit 21ff716
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 3 deletions.
8 changes: 7 additions & 1 deletion R/geoflow_handler_entity.R
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,13 @@ register_entity_handlers <- function(){
id = "zenodo",
def = "Handle metadata entities built from a Zenodo source",
packages = list("zen4R"),
fun = source(system.file("metadata/entity", "entity_handler_zenodo.R", package = "geoflow"))$value
fun = source(system.file("metadata/entity", "entity_handler_zenodo.R", package = "geoflow"))$value,
available_options = list(
resource_type = list(def = "Type resource. By default 'deposit', use this if you are the owner of the records you fetch.
Alternative option value is 'record' that can be used for third-party record handling (as anonymous user", default = "deposit"),
source_type = list(def = "Type of source for handling Zenodo input deposits/records. Possible values are ['query': an ElasticSearch query,
'doi': one or more DOIs]", default = 'query')
)
)
)
.geoflow$entity_handlers <- handlers
Expand Down
37 changes: 37 additions & 0 deletions inst/extdata/workflows/config_metadata_zenodo_with_options.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"profile": {
"id": "my-workflow",
"name": "My workflow",
"project": "Test geoflow project",
"organization": "My organization",
"logos": [
"https://via.placeholder.com/300x150.png/09f/fff?text=geoflow"
],
"mode": "entity"
},
"metadata": {
"entities": [
{
"handler": "zenodo",
"source": ["10.5281/zenodo.2547036"],
"options": {
"resource_type": "record",
"source_type": "doi"
}
}
],
"contacts" : []
},
"software": [
{
"id": "my-zenodo",
"type": "input",
"software_type": "zenodo",
"parameters": {
"url": "https://zenodo.org/api",
"logger": "DEBUG"
}
}
],
"actions": []
}
3 changes: 2 additions & 1 deletion inst/extdata/workflows/workflows.csv
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ config_metadata_gsheets_sdi_geoserver_geotiff_view.json,Load metadata entities/c
config_metadata_gsheets_sdi_geoserver_geotiff_directory.json,Load metadata entities/contacts from Google Spreadsheets and publish multiple GeoTIFF based layers on GeoServer for a single entity
config_metadata_ocs.json,Load metadata entities/contacts/dictionary from CSV files hosted on a cloud supporting OCS API (eg. nextcloud/owncloud)
config_metadata_dataverse.json,Load metadata entities/contacts from Dataverse records
config_metadata_zenodo.json,Load metadata entities/contacts from Zenodo deposits/records
config_metadata_zenodo.json,Load metadata entities/contacts from Zenodo deposits/records
config_metadata_zenodo_with_options.json,Load metadata entities/contacts from Zenodo deposits/records (with options declared to fetch from public records and by DOI)
53 changes: 52 additions & 1 deletion inst/metadata/entity/entity_handler_zenodo.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,56 @@ handle_entities_zenodo <- function(handler, source, config, handle = TRUE){
}
ZENODO_CONFIG <- config$software$input$zenodo_config

results <- ZENODO$getDepositions(q = source)
resource_type <- handler$getOption("resource_type") #for this handler default is "deposit"
source_type <- handler$getOption("source_type") #for this handler default is "query"

#fetch results depending on resource and source types
config$logger.info(sprintf("Zenodo entity handler: resource type '%s'", resource_type))
config$logger.info(sprintf("Zenodo entity handler: source type '%s'", source_type))
results <- switch(resource_type,
"deposit" = {
switch(source_type,
"query" = {
ZENODO$getDepositions(q = source)
},
"doi" = {
recs = lapply(source, function(doi){
rec = ZENODO$getDepositionByDOI(doi)
if(is.null(rec)) rec = ZENODO$getDepositionByConceptDOI(doi)
return(rec)
})
recs = recs[!sapply(recs, is.null)]
recs
},{
config$logger.warn(sprintf("Unknown source type '%s' in Zenodo entity handler", source_type))
list()
}
)
},
"record" = {
switch(source_type,
"query" = {
ZENODO$getRecords(q = source)
},
"doi" = {
recs = lapply(source, function(doi){
rec = ZENODO$getRecordByDOI(doi)
if(is.null(rec)) rec = ZENODO$getRecordByConceptDOI(doi)
return(rec)
})
recs = recs[!sapply(recs, is.null)]
recs
},{
config$logger.warn(sprintf("Unknown source type '%s' in Zenodo entity handler", source_type))
list()
}
)
},{
config$logger.warn(sprintf("Unknown resource type '%s' in Zenodo entity handler. Valid values ['deposit','record']", resource_type))
list()
}
)
if(length(results)==1) if(is(results, "ZenodoRecord")) results = list(results)
if(length(results)==0) return(list())

entities <- lapply(1:length(results), function(i){
Expand All @@ -27,6 +76,8 @@ handle_entities_zenodo <- function(handler, source, config, handle = TRUE){
if(length(identifiers)>0){
identifier = identifiers[[1]]$identifier
entity$setIdentifier("id", substr(identifier, 5,nchar(identifier)))
}else{
entity$setIdentifier("id", gsub("/", "_", result$doi))
}

#entity Date
Expand Down
47 changes: 47 additions & 0 deletions tests/testthat/test_config_metadata_zenodo_with_options.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# test_config_metadata_zenodo_with_options.R
# Author: Emmanuel Blondel <emmanuel.blondel1@gmail.com>
#
# Description: Integration tests for config_metadata_zenodo_with_options.json workflow
#=======================
require(geoflow, quietly = TRUE)
require(testthat)

#init
test_that("init",{
testthat::skip_on_cran()
cfg_file = system.file("extdata/workflows/config_metadata_zenodo_with_options.json", package = "geoflow")
CFG <- geoflow::initWorkflow(cfg_file)
expect_is(CFG$metadata$content, "list")
expect_equal(length(CFG$metadata$content), 1L)
expect_equal(names(CFG$metadata$content), "entities")
expect_equal(length(CFG$metadata$content$entities), 1L)
expect_equal(length(CFG$getEntities()), 1L)
expect_equal(length(CFG$actions), 0L)
expect_equal(length(CFG$software), 2L)
expect_equal(names(CFG$software), c("input", "output"))
expect_equal(length(CFG$software$input), 2L)
expect_equal(length(CFG$software$output), 0L)
expect_equal(names(CFG$software$input), c("zenodo", "zenodo_config"))
})

#debug
test_that("debug",{
testthat::skip_on_cran()
cfg_file = system.file("extdata/workflows/config_metadata_zenodo_with_options.json", package = "geoflow")
DEBUG <- geoflow::debugWorkflow(cfg_file, entityIndex = 1, dir = ".")
expect_equal(names(DEBUG), c("config", "entity"))
expect_is(DEBUG$config, "list")
expect_is(DEBUG$entity, "geoflow_entity")
expect_equal(DEBUG$entity$identifiers$doi, "10.5281/zenodo.3378733")
})

#execute
test_that("execute",{
#testthat::skip_on_cran()
#cfg_file = system.file("extdata/workflows/config_metadata_zenodo_with_options.json", package = "geoflow")
#execution
#EXEC <- geoflow::executeWorkflow(cfg_file, dir = ".")
#expect_true(dir.exists(EXEC))
#expect_true(file.exists(file.path(EXEC, "job.json")))
#expect_true(file.exists(file.path(EXEC, "job-logs.txt")))
})

0 comments on commit 21ff716

Please sign in to comment.