Skip to content

Commit

Permalink
more gracefull when no internet for #95
Browse files Browse the repository at this point in the history
  • Loading branch information
dblodgett-usgs committed Jun 30, 2024
1 parent 6a896a2 commit b3cb543
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 10 deletions.
8 changes: 6 additions & 2 deletions R/creation.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,21 @@ contains_array <- function(store, path=NA) {
path <- normalize_storage_path(path)
prefix <- path_to_prefix(path)
key <- paste0(prefix, ARRAY_META_KEY)
return(store$contains_item(key))
ret <- store$contains_item(key)
return(!is.null(ret) && ret)
}

#' @keywords internal
contains_group <- function(store, path=NA) {
# Reference: https://github.com/zarr-developers/zarr-python/blob/5dd4a0e6cdc04c6413e14f57f61d389972ea937c/zarr/storage.py#L99
# Return True if the store contains a group at the given logical path.

path <- normalize_storage_path(path)
prefix <- path_to_prefix(path)
key <- paste0(prefix, GROUP_META_KEY)
return(store$contains_item(key))
ret <- store$contains_item(key)

return(!is.null(ret) && ret)
}

#' @keywords internal
Expand Down
4 changes: 2 additions & 2 deletions R/meta.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ Metadata2 <- R6::R6Class("Metadata2",
},
decode_array_metadata = function(s) {
meta <- self$decode_metadata(s)
validate_v2_meta(meta)
if(!is.null(meta)) validate_v2_meta(meta)
return(meta)
},
decode_group_metadata = function(s) {
meta <- self$decode_metadata(s)
validate_v2_meta(meta)
if(!is.null(meta)) validate_v2_meta(meta)
return(meta)
},
encode_array_metadata = function(meta) {
Expand Down
9 changes: 5 additions & 4 deletions R/stores.R
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,9 @@ HttpStore <- R6::R6Class("HttpStore",
res$request()
return(unclass(res$responses())[[1]])
} else {
return(private$client$get(path = path))
ret <- NULL
try(ret <- private$client$get(path = path))
return(ret)
}
},
memoize_make_request = function() {
Expand All @@ -405,7 +407,7 @@ HttpStore <- R6::R6Class("HttpStore",
get_zmetadata = function() {
res <- private$make_request(".zmetadata")

if(res$status_code == 200) {
if(!is.null(res$status_code) && res$status_code == 200) {
out <- try_fromJSON(res$parse("UTF-8"))
} else out <- NULL

Expand Down Expand Up @@ -463,7 +465,6 @@ HttpStore <- R6::R6Class("HttpStore",
#' @param item The item key.
#' @return A boolean value.
contains_item = function(item) {

# use consolidated metadata if it exists
if(!is.null(try_from_zmeta(item_to_key(item), self))) {
return(TRUE)
Expand All @@ -472,7 +473,7 @@ HttpStore <- R6::R6Class("HttpStore",
} else {
res <- private$make_request_memoized(item)

return(res$status_code == 200)
return(!is.null(res$status_code) && res$status_code == 200)
}

},
Expand Down
3 changes: 2 additions & 1 deletion R/zarr-array.R
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ ZarrArray <- R6::R6Class("ZarrArray",

if(is.null(meta)) {
meta_bytes <- private$store$get_item(mkey)
meta <- private$store$metadata_class$decode_array_metadata(meta_bytes)
if(!is.null(meta_bytes))
meta <- private$store$metadata_class$decode_array_metadata(meta_bytes)
}

private$meta <- meta
Expand Down
8 changes: 7 additions & 1 deletion R/zarr-group.R
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@ ZarrGroup <- R6::R6Class("ZarrGroup",
}
})

private$meta <- private$store$metadata_class$decode_group_metadata(meta_bytes)
if(!is.null(meta_bytes))
private$meta <- private$store$metadata_class$decode_group_metadata(meta_bytes)

}

Expand Down Expand Up @@ -229,6 +230,11 @@ ZarrGroup <- R6::R6Class("ZarrGroup",
#' Obtain a group member.
#' @param item character item to test for
get_item = function(item) {
if(is.null(item)) {
#for case with no internet
warning("item can not be null")
return(NULL)
}
path <- private$item_path(item)
if(contains_array(private$store, path)) {
return(ZarrArray$new(
Expand Down

0 comments on commit b3cb543

Please sign in to comment.