R/zzz_stash_match.R

#' List files/objects that match parameters
#'
#' @param file.name Base file name for the object
#' @param from Path for where the object resides. Accepts a character
#'    string or *_stash object.
#' @param time.stamp TRUE or FALSE. When TRUE, files containing a time stamp
#'    generated by save_stash, or matching the save_stash pattern, will be
#'    matched.
#' @param uuid TRUE or FALSE. When TRUE, files containing a uuid
#'    generated by save_stash, or matching the save_stash pattern, will be
#'    matched.
#' @param extension Extension for the file to load.
#' @param compression Accepts NULL for no compression, or 'gz' for gzip.
#' @param recursive Logical. Should the match recurse into directories?
#'
#' @return List of stash objects that match parameters.
#' @export
stash_match <- function(file.name, from = '', time.stamp = FALSE, uuid = FALSE,
    extension = NULL, compression = NULL, recursive = FALSE) {

  from <- as.stash(from)
  stash_match_(file.name, from, time.stamp, uuid, extension, compression,
      recursive)
}


stash_match_ <- function(file.name, from, time.stamp, uuid, extension,
    compression, recursive) {

  UseMethod('stash_match_', from)
}

#' List files/objects that match parameters from a local stash
#'
#' @inheritParams stash_match
#'
#' @return List of stash objects that match parameters.
#' @export
stash_match_.local_stash <- function(file.name, from, time.stamp, uuid,
    extension, compression, recursive) {

  if (!is.character(from)) {
    stop('"from" directory is invalid')
  }

  from <- gsub('^$', '\\.', from)
  file.pattern <- generate_filepattern(file.name, time.stamp, uuid, extension,
      compression)
  file.matches <- list.files(from, pattern = file.pattern, full.names = TRUE,
    recursive = recursive)

  if (length(file.matches) == 0) {
    return(file.matches)
  }

  lapply(file.matches, local_stash)
}


#' List files/objects that match parameters from an AWS S3 stash
#'
#' @inheritParams stash_match
#'
#' @return List of stash objects that match parameters.
#' @export
stash_match_.s3_stash <- function(file.name, from, time.stamp, uuid,
    extension, compression, recursive) {

  if (class(from) != 's3_stash') {
    stop('"from" directory needs to be an "s3_stash"')
  }

  bucket <- attr(from, 'bucket')

  file.pattern <- generate_filepattern(file.name, time.stamp, uuid, extension,
      compression)
  file.matches <- list.files.s3(bucket, from, pattern = file.pattern,
      full.names = TRUE, recursive = recursive)

  if (length(file.matches) == 0) {
    return(file.matches)
  }

  lapply(file.matches, s3_stash)
}
jason-huling/rstash documentation built on May 18, 2019, 4:53 p.m.