Added feature to move File-like object inside collection.
authorFuad Muhic <fmuhic@capeannenterprises.com>
Tue, 19 Dec 2017 14:34:44 +0000 (15:34 +0100)
committerFuad Muhic <fmuhic@capeannenterprises.com>
Tue, 19 Dec 2017 14:34:44 +0000 (15:34 +0100)
Arvados-DCO-1.1-Signed-off-by: Fuad Muhic <fmuhic@capeannenterprises.com>

sdk/R/.RData [deleted file]
sdk/R/DESCRIPTION
sdk/R/NAMESPACE
sdk/R/R/ArvadosFile.R
sdk/R/R/CTest.R [deleted file]
sdk/R/R/Collection.R
sdk/R/R/CollectionTree.R
sdk/R/R/HttpRequest.R
sdk/R/R/Subcollection.R
sdk/R/man/CTest.Rd [deleted file]
sdk/R/man/Collection.Rd

diff --git a/sdk/R/.RData b/sdk/R/.RData
deleted file mode 100644 (file)
index 1344e2a..0000000
Binary files a/sdk/R/.RData and /dev/null differ
index 7eb152fdd56f5955018972ed5e590c898ee95388..0e586e91bd3bbb1c8476775428f22fbb0be6f2d9 100644 (file)
@@ -1,6 +1,7 @@
 Package: ArvadosR
 Type: Package
 Title: Arvados R SDK
+Version: 0.0.1
 Author: Fuad Muhic
 Maintainer: Ward Vandewege <wvandewege@veritasgenetics.com>
 Description: This is the Arvados R SDK
index 8b2f56757ee33c39aa9bdfaffa3330e2e28532e6..1c94e716bfd03321b8909feb8559c0cf42addee4 100644 (file)
@@ -2,7 +2,6 @@
 
 export(Arvados)
 export(ArvadosFile)
-export(CTest)
 export(Collection)
 export(CollectionTree)
 export(Subcollection)
index c8950c651cfd6ee568144d7f6f647a422bbd1843..85d11c7d66b7f28fe3d471a9648e93ae47715ed1 100644 (file)
@@ -104,6 +104,35 @@ ArvadosFile <- R6::R6Class(
 
             parsedServerResponse <- httr::content(serverResponse, "text")
             parsedServerResponse
+        },
+
+        move = function(newLocation)
+        {
+            if(endsWith(newLocation, paste0(private$name, "/")))
+            {
+                newLocation <- substr(newLocation, 0, nchar(newLocation) - nchar(paste0(private$name, "/")))
+            }
+            else if(endsWith(newLocation, private$name))
+            {
+                newLocation <- substr(newLocation, 0, nchar(newLocation) - nchar(private$name))
+            }
+            else
+            {
+                stop("Destination path is not valid.")
+            }
+
+            newParent <- private$collection$get(newLocation)
+
+            if(is.null(newParent))
+            {
+                stop("Unable to get destination subcollectin")
+            }
+
+            status <- private$collection$.__enclos_env__$private$moveOnRest(self$getRelativePath(), paste0(newParent$getRelativePath(), "/", self$getName()))
+
+            private$attachToParent(newParent)
+
+            paste("Status code :", status$status_code)
         }
     ),
 
@@ -138,6 +167,12 @@ ArvadosFile <- R6::R6Class(
                 private$parent$.__enclos_env__$private$removeChild(private$name)
                 private$parent <- NULL
             }
+        },
+
+        attachToParent = function(parent)
+        {
+            parent$.__enclos_env__$private$children <- c(parent$.__enclos_env__$private$children, self)
+            private$parent <- parent
         }
     ),
     
diff --git a/sdk/R/R/CTest.R b/sdk/R/R/CTest.R
deleted file mode 100644 (file)
index bdef950..0000000
+++ /dev/null
@@ -1,173 +0,0 @@
-source("./R/Subcollection.R")
-source("./R/ArvadosFile.R")
-source("./R/HttpRequest.R")
-source("./R/HttpParser.R")
-
-#' Arvados Collection Object
-#'
-#' Update description
-#'
-#' @examples arv = Collection$new(api, uuid)
-#' @export Collection
-Collection <- R6::R6Class(
-
-    "Collection",
-
-    public = list(
-
-        api  = NULL,
-        uuid = NULL,
-
-        initialize = function(api, uuid)
-        {
-            self$api <- api
-            private$http <- HttpRequest$new()
-            private$httpParser <- HttpParser$new()
-
-            self$uuid <- uuid
-            collection <- self$api$getCollection(uuid)
-
-            private$fileContent <- private$getCollectionContent()
-            private$tree <- CollectionTree$new(private$fileContent, self)
-        },
-
-        add = function(content, relativePath = "")
-        {
-            if(relativePath == "" ||
-               relativePath == "." ||
-               relativePath == "./")
-            {
-                subcollection <- private$tree$.__enclos_env__$private$tree
-            }
-            else
-            {
-                if(endsWith(relativePath, "/") && nchar(relativePath) > 0)
-                    relativePath <- substr(relativePath, 1, nchar(relativePath) - 1)
-
-                subcollection <- self$get(relativePath)
-            }
-
-            if(is.null(subcollection))
-                stop(paste("Subcollection", relativePath, "doesn't exist."))
-
-            if(is.character(content))
-            {
-                sapply(content, function(fileName)
-                {
-                    subcollection$add(ArvadosFile$new(fileName))
-                })
-            }
-            else if("ArvadosFile"   %in% class(content) ||
-                    "Subcollection" %in% class(content))
-            {
-                subcollection$add(content)
-            }
-        },
-
-        remove = function(content)
-        {
-            if(is.character(content))
-            {
-                sapply(content, function(filePath)
-                {
-                    if(endsWith(filePath, "/") && nchar(filePath) > 0)
-                        filePath <- substr(filePath, 1, nchar(filePath) - 1)
-
-                    file <- self$get(filePath)
-
-                    if(is.null(file))
-                        stop(paste("File", filePath, "doesn't exist."))
-
-                    file$removeFromCollection()
-                })
-            }
-            else if("ArvadosFile"   %in% class(content) ||
-                    "Subcollection" %in% class(content))
-            {
-                if(is.null(content$.__enclos_env__$private$collection) || 
-                   content$.__enclos_env__$private$collection$uuid != self$uuid)
-                    stop("Subcollection doesn't belong to this collection.")
-
-                content$removeFromCollection()
-            }
-        },
-
-        getTree = function() private$tree,
-
-        getFileContent = function() private$getCollectionContent(),
-
-        get = function(relativePath)
-        {
-            private$tree$getElement(relativePath)
-        }
-    ),
-
-    private = list(
-
-        http       = NULL,
-        httpParser = NULL,
-        tree       = NULL,
-
-        fileContent = NULL,
-
-        getCollectionContent = function()
-        {
-            collectionURL <- URLencode(paste0(self$api$getWebDavHostName(), "c=", self$uuid))
-
-            headers = list("Authorization" = paste("OAuth2", self$api$getToken()))
-
-            response <- private$http$PROPFIND(collectionURL, headers)
-
-            parsedResponse <- private$httpParser$parseWebDAVResponse(response, collectionURL)
-            parsedResponse[-1]
-        },
-
-        createFilesOnREST = function(files)
-        {
-            sapply(files, function(filePath)
-            {
-                private$createNewFile(filePath, NULL, "text/html")
-            })
-        },
-        
-        generateTree = function(content)
-        {
-            treeBranches <- sapply(collectionContent, function(filePath)
-            {
-                splitPath <- unlist(strsplit(filePath$name, "/", fixed = TRUE))
-
-                branch = private$createBranch(splitPath, filePath$fileSize)      
-            })
-        },
-
-        createNewFile = function(relativePath, content, contentType)
-        {
-            fileURL <- paste0(self$api$getWebDavHostName(), "c=", self$uuid, "/", relativePath);
-            headers <- list(Authorization = paste("OAuth2", self$api$getToken()), 
-                            "Content-Type" = contentType)
-            body <- content
-
-            serverResponse <- private$http$PUT(fileURL, headers, body)
-
-            if(serverResponse$status_code != 201)
-                stop(paste("Server code:", serverResponse$status_code))
-
-            print(paste("File created:", relativePath))
-        },
-        
-        deleteFromREST = function(relativePath)
-        {
-            fileURL <- paste0(self$api$getWebDavHostName(), "c=", self$uuid, "/", relativePath);
-            headers <- list(Authorization = paste("OAuth2", self$api$getToken())) 
-
-            serverResponse <- private$http$DELETE(fileURL, headers)
-
-            if(serverResponse$status_code != 204)
-                stop(paste("Server code:", serverResponse$status_code))
-
-            print(paste("File deleted", relativePath))
-        }
-    ),
-
-    cloneable = FALSE
-)
index 94141d97f4437d4e5f095a193853fe10d770a283..ea6f692ce556c558c62fe71005ef384d2d2657e6 100644 (file)
@@ -8,9 +8,11 @@ source("./R/HttpParser.R")
 #' Update description
 #'
 #' @examples arv = Collection$new(api, uuid)
-#' @export CTest
-CTest <- R6::R6Class(
-    "CTest",
+#' @export Collection
+Collection <- R6::R6Class(
+
+    "Collection",
+
     public = list(
 
         api  = NULL,
@@ -90,7 +92,18 @@ CTest <- R6::R6Class(
             }
         },
 
-        getTree = function() private$tree,
+        move = function(content, newLocation)
+        {
+            if(endsWith(content, "/"))
+                content <- substr(content, 0, nchar(content) - 1)
+
+            elementToMove <- self$get(content)
+
+            if(is.null(elementToMove))
+                stop("Element you want to move doesn't exist in the collection.")
+
+            elementToMove$move(newLocation)
+        },
 
         getFileContent = function() private$getCollectionContent(),
 
@@ -164,6 +177,23 @@ CTest <- R6::R6Class(
                 stop(paste("Server code:", serverResponse$status_code))
 
             print(paste("File deleted", relativePath))
+        },
+
+        moveOnRest = function(from, to)
+        {
+            collectionURL <- URLencode(paste0(self$api$getWebDavHostName(), "c=", self$uuid, "/"))
+            fromURL <- paste0(collectionURL, from)
+            toURL <- paste0(collectionURL, to)
+
+            headers = list("Authorization" = paste("OAuth2", self$api$getToken()),
+                           "Destination" = toURL)
+
+            serverResponse <- private$http$MOVE(fromURL, headers)
+
+            if(serverResponse$status_code < 200 || serverResponse$status_code >= 300)
+                stop(paste("Server code:", serverResponse$status_code))
+
+            serverResponse
         }
     ),
 
index cf8c6ebe773aa942424aa38d9134224480b85ec0..82c6eb8f9e16011a2b82c753e2e672ed15387d0e 100644 (file)
@@ -1,4 +1,5 @@
 source("./R/Subcollection.R")
+
 source("./R/ArvadosFile.R")
 
 #' Arvados Collection Object
@@ -36,6 +37,9 @@ CollectionTree <- R6::R6Class(
 
         getElement = function(relativePath)
         {
+            if(endsWith(relativePath, "/"))
+                relativePath <- substr(relativePath, 0, nchar(relativePath) - 1)
+
             splitPath <- unlist(strsplit(relativePath, "/", fixed = TRUE))
             returnElement = private$tree
 
@@ -62,7 +66,7 @@ CollectionTree <- R6::R6Class(
 
             for(elementIndex in lastElementIndex:1)
             {
-            if(elementIndex == lastElementIndex)
+                if(elementIndex == lastElementIndex)
                 {
                     branch = ArvadosFile$new(splitPath[[elementIndex]])
                 }
index f5c11a176fd16fd1c14e9c365d24dd6aafbc369d..7a399a4b30ff1ea4a7f75b1b2331aa102e847e20 100644 (file)
@@ -53,6 +53,15 @@ HttpRequest <- R6::R6Class(
             curl::handle_setopt(h, customrequest = "PROPFIND")
             curl::handle_setheaders(h, .list = headers)
 
+            propfindResponse <- curl::curl_fetch_memory(url, h)
+        },
+
+        MOVE = function(url, headers = NULL)
+        {
+            h <- curl::new_handle()
+            curl::handle_setopt(h, customrequest = "MOVE")
+            curl::handle_setheaders(h, .list = headers)
+
             propfindResponse <- curl::curl_fetch_memory(url, h)
         }
     ),
index b453c535ada4ebf786bb770dc6c5f11017923fab..78cc4c13bf82646f6136920b77fb4c2b00e52c5e 100644 (file)
@@ -53,7 +53,7 @@ Subcollection <- R6::R6Class(
 
             #todo rename this add to a collection
             private$addToCollection(NULL)
-            private$detachFromParent()
+            private$dettachFromParent()
 
         },
 
@@ -111,6 +111,35 @@ Subcollection <- R6::R6Class(
             paste0(relativePath, collapse = "/")
         },
 
+        move = function(newLocation)
+        {
+            if(endsWith(newLocation, paste0(private$name, "/")))
+            {
+                newLocation <- substr(newLocation, 0, nchar(newLocation) - nchar(paste0(private$name, "/")))
+            }
+            else if(endsWith(newLocation, private$name))
+            {
+                newLocation <- substr(newLocation, 0, nchar(newLocation) - nchar(private$name))
+            }
+            else
+            {
+                stop("Destination path is not valid.")
+            }
+
+            newParent <- private$collection$get(newLocation)
+
+            if(is.null(newParent))
+            {
+                stop("Unable to get destination subcollectin")
+            }
+
+            status <- private$collection$.__enclos_env__$private$moveOnRest(self$getRelativePath(), paste0(newParent$getRelativePath(), "/", self$getName()))
+
+            private$attachToParent(newParent)
+
+            paste("Status code :", status$status_code)
+        },
+
         getParent = function() private$parent
     ),
 
@@ -166,13 +195,21 @@ Subcollection <- R6::R6Class(
             private$collection = collection
         },
 
-        detachFromParent = function()
+        dettachFromParent = function()
         {
             if(!is.null(private$parent))
             {
                 private$parent$.__enclos_env__$private$removeChild(private$name)
                 private$parent <- NULL
             }
+            else
+                stop("Parent doesn't exists.")
+        },
+
+        attachToParent = function(parent)
+        {
+            parent$.__enclos_env__$private$children <- c(parent$.__enclos_env__$private$children, self)
+            private$parent <- parent
         }
     ),
     
diff --git a/sdk/R/man/CTest.Rd b/sdk/R/man/CTest.Rd
deleted file mode 100644 (file)
index 6c14529..0000000
+++ /dev/null
@@ -1,17 +0,0 @@
-% Generated by roxygen2: do not edit by hand
-% Please edit documentation in R/Collection.R
-\docType{data}
-\name{CTest}
-\alias{CTest}
-\title{Arvados Collection Object}
-\format{An object of class \code{R6ClassGenerator} of length 24.}
-\usage{
-CTest
-}
-\description{
-Update description
-}
-\examples{
-arv = Collection$new(api, uuid)
-}
-\keyword{datasets}
index 2f54bfe2eb5b08b5d83b70f1008d8a8cf2a97bb3..46c76cb40b49c7026e375cd4fa6ce0f69b56d3d3 100644 (file)
@@ -1,5 +1,5 @@
 % Generated by roxygen2: do not edit by hand
-% Please edit documentation in R/CTest.R
+% Please edit documentation in R/Collection.R
 \docType{data}
 \name{Collection}
 \alias{Collection}