Skip to content

Commit

Permalink
adding get_details utility
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonGreenhill committed Oct 26, 2024
1 parent 503600e commit 70401e8
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
11 changes: 4 additions & 7 deletions R/cache.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,13 @@ get_dir_size <- function(path) {
#' @export
#' @return A dataframe of the directories
list_cache_files <- function() {
df <- list.files(get_cache_dir(), full.names=TRUE)
if (length(df) == 0) { return(data.frame()) }
df <- df[sapply(df, dir.exists, USE.NAMES=FALSE)] # keep dirs only
df <- data.frame(Path=df, Name=basename(df))
df$Size <- sapply(df$Path, get_dir_size)
df
paths <- list.files(get_cache_dir(), full.names=TRUE)
if (length(paths) == 0) { return(data.frame()) }
paths <- paths[sapply(paths, dir.exists, USE.NAMES=FALSE)] # keep dirs only
do.call(rbind, lapply(paths, rcldf::get_details))
}



#' Removes all files in the cache directory
#'
#' @export
Expand Down
16 changes: 16 additions & 0 deletions R/get_details.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

#' Returns a dataframe of with details on the CLDF dataset in `path`.
#' @param path the path to resolve
#' @export
#' @return A dataframe.
get_details <- function(path) {
md <- rcldf::resolve_path(path)
data.frame(
Title=md$metadata$`dc:title`,
Path=path,
Size=get_dir_size(dirname(path)),
Citation=md$metadata$`dc:bibliographicCitation`,
ConformsTo=md$metadata$`dc:conformsTo`
)
}

9 changes: 9 additions & 0 deletions tests/testthat/test_get_details.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
test_that("test get_details", {
df <- get_details("examples/wals_1A_cldf/StructureDataset-metadata.json")
expect_is(df, 'data.frame')
expect_equal(df$Title, 'The Dataset')
expect_equal(df$Size, 13696)
expect_equal(df$Citation, 'Cite me like this!')
expect_equal(df$ConformsTo, 'http://cldf.clld.org/v1.0/terms.rdf#StructureDataset')

})

0 comments on commit 70401e8

Please sign in to comment.