From 44ab8ee44302102a7cd5289ef7336d7a94594558 Mon Sep 17 00:00:00 2001 From: Fuad Muhic Date: Mon, 29 Jan 2018 12:33:49 +0100 Subject: [PATCH] Implemented custom print functions for all public classes Arvados-DCO-1.1-Signed-off-by: Fuad Muhic --- sdk/R/NAMESPACE | 5 ++- sdk/R/R/Arvados.R | 8 ++++ sdk/R/R/ArvadosFile.R | 18 ++++++++ sdk/R/R/Collection.R | 22 ++++----- sdk/R/R/CollectionTree.R | 5 +-- sdk/R/R/Subcollection.R | 60 ++++++++++++++++++------- sdk/R/R/util.R | 2 +- sdk/R/README | 4 +- sdk/R/tests/testthat/test-Collection.R | 2 +- sdk/R/tests/testthat/test-HttpRequest.R | 2 +- 10 files changed, 90 insertions(+), 38 deletions(-) diff --git a/sdk/R/NAMESPACE b/sdk/R/NAMESPACE index 1c94e716bf..d4c143cdba 100644 --- a/sdk/R/NAMESPACE +++ b/sdk/R/NAMESPACE @@ -3,5 +3,8 @@ export(Arvados) export(ArvadosFile) export(Collection) -export(CollectionTree) export(Subcollection) +export(print.Arvados) +export(print.ArvadosFile) +export(print.Collection) +export(print.Subcollection) diff --git a/sdk/R/R/Arvados.R b/sdk/R/R/Arvados.R index 18b42f6df2..b21c604635 100644 --- a/sdk/R/R/Arvados.R +++ b/sdk/R/R/Arvados.R @@ -165,3 +165,11 @@ Arvados <- R6::R6Class( cloneable = FALSE ) + +#' @export print.Arvados +print.Arvados = function(arvados) +{ + cat(paste0("Type: ", "\"", "Arvados", "\""), sep = "\n") + cat(paste0("Host: ", "\"", arvados$getHostName(), "\""), sep = "\n") + cat(paste0("Token: ", "\"", arvados$getToken(), "\"") , sep = "\n") +} diff --git a/sdk/R/R/ArvadosFile.R b/sdk/R/R/ArvadosFile.R index ffb9a6b848..7ffcb29418 100644 --- a/sdk/R/R/ArvadosFile.R +++ b/sdk/R/R/ArvadosFile.R @@ -202,3 +202,21 @@ ArvadosFile <- R6::R6Class( cloneable = FALSE ) + +#' @export print.ArvadosFile +print.ArvadosFile = function(arvadosFile) +{ + collection <- NULL + relativePath <- arvadosFile$getRelativePath() + + if(!is.null(arvadosFile$getCollection())) + { + collection <- arvadosFile$getCollection()$uuid + relativePath <- paste0("/", relativePath) + } + + cat(paste0("Type: ", "\"", "ArvadosFile", "\""), sep = "\n") + cat(paste0("Name: ", "\"", arvadosFile$getName(), "\""), sep = "\n") + cat(paste0("Relative path: ", "\"", relativePath, "\"") , sep = "\n") + cat(paste0("Collection: ", "\"", collection, "\""), sep = "\n") +} diff --git a/sdk/R/R/Collection.R b/sdk/R/R/Collection.R index 8d49cd0dec..2107620435 100644 --- a/sdk/R/R/Collection.R +++ b/sdk/R/R/Collection.R @@ -50,7 +50,6 @@ Collection <- R6::R6Class( "Subcollection" %in% class(content)) { subcollection$add(content) - content } else @@ -137,7 +136,7 @@ Collection <- R6::R6Class( elementToMove <- self$get(content) if(is.null(elementToMove)) - stop("Element you want to move doesn't exist in the collection.") + stop("Content you want to move doesn't exist in the collection.") elementToMove$move(newLocation) }, @@ -161,18 +160,15 @@ Collection <- R6::R6Class( REST = NULL, tree = NULL, - fileContent = NULL, - - generateTree = function(content) - { - treeBranches <- sapply(collectionContent, function(filePath) - { - splitPath <- unlist(strsplit(filePath$name, "/", fixed = TRUE)) - - branch = private$createBranch(splitPath, filePath$fileSize) - }) - } + fileContent = NULL ), cloneable = FALSE ) + +#' @export print.Collection +print.Collection = function(collection) +{ + cat(paste0("Type: ", "\"", "Arvados Collection", "\""), sep = "\n") + cat(paste0("uuid: ", "\"", collection$uuid, "\""), sep = "\n") +} diff --git a/sdk/R/R/CollectionTree.R b/sdk/R/R/CollectionTree.R index fcc5dbece5..4194be95dd 100644 --- a/sdk/R/R/CollectionTree.R +++ b/sdk/R/R/CollectionTree.R @@ -7,7 +7,6 @@ source("./R/util.R") #' Update description #' #' @examples arv = Collection$new(api, uuid) -#' @export CollectionTree CollectionTree <- R6::R6Class( "CollectionTree", public = list( @@ -96,8 +95,8 @@ CollectionTree <- R6::R6Class( } else { - # Note: REST always returns folder name alone before other folder content - # (for some reason), so in first iteration we don't know if it's a file + # Note: REST always returns folder name alone before other folder + # content, so in first iteration we don't know if it's a file # or folder since its just a name, so we assume it's a file. # If we encounter that same name again we know # it's a folder so we need to replace ArvadosFile with Subcollection. diff --git a/sdk/R/R/Subcollection.R b/sdk/R/R/Subcollection.R index a1f83f51bf..7eb4381eda 100644 --- a/sdk/R/R/Subcollection.R +++ b/sdk/R/R/Subcollection.R @@ -13,7 +13,7 @@ Subcollection <- R6::R6Class( initialize = function(name) { - private$name <- name + private$name <- name }, getName = function() private$name, @@ -101,21 +101,7 @@ Subcollection <- R6::R6Class( getFileListing = function(fullPath = TRUE) { - content <- NULL - - if(fullPath) - { - for(child in private$children) - content <- c(content, child$getFileListing()) - - if(private$name != "") - content <- unlist(paste0(private$name, "/", content)) - } - else - { - for(child in private$children) - content <- c(content, child$getName()) - } + content <- private$getContentAsCharVector(fullPath) content[order(tolower(content))] }, @@ -250,8 +236,50 @@ Subcollection <- R6::R6Class( parent$remove(private$name) parent$setCollection(parentsCollection, setRecursively = FALSE) + }, + + getContentAsCharVector = function(fullPath = TRUE) + { + content <- NULL + + if(fullPath) + { + for(child in private$children) + content <- c(content, child$getFileListing()) + + if(private$name != "") + content <- unlist(paste0(private$name, "/", content)) + } + else + { + for(child in private$children) + content <- c(content, child$getName()) + } + + content + } ), cloneable = FALSE ) + +#' @export print.Subcollection +print.Subcollection = function(subCollection) +{ + collection <- NULL + relativePath <- subCollection$getRelativePath() + + if(!is.null(subCollection$getCollection())) + { + collection <- subCollection$getCollection()$uuid + + if(!subCollection$getName() == "") + relativePath <- paste0("/", relativePath) + } + + cat(paste0("Type: ", "\"", "Arvados Subcollection", "\""), sep = "\n") + cat(paste0("Name: ", "\"", subCollection$getName(), "\""), sep = "\n") + cat(paste0("Relative path: ", "\"", relativePath, "\"") , sep = "\n") + cat(paste0("Collection: ", "\"", collection, "\""), sep = "\n") +} diff --git a/sdk/R/R/util.R b/sdk/R/R/util.R index 65b5a4e52f..d9af8b057b 100644 --- a/sdk/R/R/util.R +++ b/sdk/R/R/util.R @@ -49,6 +49,6 @@ splitToPathAndName = function(path) nameAndPath$name <- components[length(components)] nameAndPath$path <- trimFromStart(paste0(components[-length(components)], collapse = "/"), "/") - + nameAndPath } diff --git a/sdk/R/README b/sdk/R/README index 6b8cd3f46d..8a0c31dce6 100644 --- a/sdk/R/README +++ b/sdk/R/README @@ -57,7 +57,7 @@ deletedCollection <- arv$deleteCollection("uuid") #Update a collection's metadata: -updatedCollection <- arv$updateCollection("uuid", list(name = "My new name", description = "a brand new description")) +updatedCollection <- arv$updateCollection("uuid", list(name = "New name", description = "New description")) -------------------------------------------------------------------------------------------------------------------------------- @@ -189,7 +189,7 @@ file$move("newDestination/file.cpp") subcollection <- collection$get("location/to/folder") subcollection$move("newDestination/folder") -#Make sure to include file name in new destination +#Make sure to include new file name in destination #In second example file$move("newDestination/") will not work -------------------------------------------------------------------------------------------------------------------------------- diff --git a/sdk/R/tests/testthat/test-Collection.R b/sdk/R/tests/testthat/test-Collection.R index 0c45958ba9..63a402dba4 100644 --- a/sdk/R/tests/testthat/test-Collection.R +++ b/sdk/R/tests/testthat/test-Collection.R @@ -215,7 +215,7 @@ test_that("move raises exception if new location is not valid", { collection <- Collection$new(api, "myUUID") expect_that(collection$move("fish", "object"), - throws_error("Element you want to move doesn't exist in the collection.", + throws_error("Content you want to move doesn't exist in the collection.", fixed = TRUE)) }) diff --git a/sdk/R/tests/testthat/test-HttpRequest.R b/sdk/R/tests/testthat/test-HttpRequest.R index 427ec346cd..e85037465b 100644 --- a/sdk/R/tests/testthat/test-HttpRequest.R +++ b/sdk/R/tests/testthat/test-HttpRequest.R @@ -1,4 +1,4 @@ -context("Http Parser") +context("Http Request") test_that(paste("createQuery generates and encodes query portion of http", -- 2.30.2