Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use fs for file path manipulations; closes #38 #40

Merged
merged 17 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ URL: https://r-transit.github.io/gtfsio/,
BugReports: https://github.com/r-transit/gtfsio/issues
Imports:
data.table,
fs,
utils,
zip,
jsonlite
Expand All @@ -51,7 +52,7 @@ VignetteBuilder:
knitr
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.1
RoxygenNote: 7.3.2
Collate:
'gtfsio_error.R'
'assert_gtfs.R'
Expand Down
13 changes: 13 additions & 0 deletions R/assert_inputs.R
polettif marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -278,3 +278,16 @@ error_x_wrong_inheritance <- function(input_name,
error_call
)
}

#' Vectorized assertion of path extensions
#'
#' @param path Vector of file paths
#' @param ext File extension to be asserted for each `path`
#'
#' @return Logicl vector of same length as `path`, with `TRUE` for each element
mpadge marked this conversation as resolved.
Show resolved Hide resolved
#' with specified extension, `FALSE` otherwise.
#'
#' @noRd
assert_extension <- function(path, ext = "zip") {
fs::path_ext(path) == ext
}
22 changes: 13 additions & 9 deletions R/export_gtfs.R
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ export_gtfs <- function(gtfs,

# input checks that depend on more than one argument

if (file.exists(path) & !overwrite) error_cannot_overwrite()
if (!as_dir & !grepl("\\.zip$", path)) error_ext_must_be_zip()
if (as_dir & grepl("\\.zip$", path)) error_path_must_be_dir()
if (fs::file_exists(path) & !overwrite) error_cannot_overwrite()
if (!as_dir & !assert_extension(path, "zip")) error_ext_must_be_zip()
if (as_dir & assert_extension(path, "zip")) error_path_must_be_dir()

extra_files <- setdiff(files, names(gtfsio::gtfs_reference))
if (standard_only & !is.null(files) & !identical(extra_files, character(0))) {
Expand Down Expand Up @@ -107,18 +107,20 @@ export_gtfs <- function(gtfs,
if (as_dir) {
tmpd <- path
} else {
tmpd <- tempfile(pattern = "gtfsio")
tmpd <- fs::file_temp(pattern = "gtfsio")
}

unlink(tmpd, recursive = TRUE)
dir.create(tmpd)
if (fs::dir_exists(tmpd)) {
fs::dir_delete(tmpd)
}
fs::dir_create(tmpd, recurse = TRUE)

# write files to 'tmpd'

if (!quiet) message("Writing text files to ", tmpd)

filenames <- append_file_ext(files)
filepaths <- file.path(tmpd, filenames)
filepaths <- fs::path(tmpd, filenames)

for (i in seq_along(files)) {

Expand All @@ -130,7 +132,7 @@ export_gtfs <- function(gtfs,

dt <- gtfs[[file]]

if(endsWith(filename, ".geojson")) {
if (assert_extension(filename, "geojson")) {
jsonlite::write_json(dt, filepath, pretty = FALSE, auto_unbox = TRUE, digits = 8)
} else {

Expand Down Expand Up @@ -165,7 +167,9 @@ export_gtfs <- function(gtfs,

if (!as_dir) {

unlink(path, recursive = TRUE)
if (fs::dir_exists(path)) {
fs::dir_delete(path)
polettif marked this conversation as resolved.
Show resolved Hide resolved
}

zip::zip(
path,
Expand Down
40 changes: 17 additions & 23 deletions R/import_gtfs.R
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ import_gtfs <- function(path,

path_is_url <- grepl("^http[s]?\\:\\/\\/\\.*", path)

if (!path_is_url && !file.exists(path)) error_non_existent_file(path)
if (!path_is_url && !fs::file_exists(path)) error_non_existent_file(path)
if (!is.null(files) && !is.null(skip)) error_files_and_skip_provided()

for (input_types in extra_spec) {
Expand All @@ -98,7 +98,7 @@ import_gtfs <- function(path,
# if 'path' is an URL, download it and save path to downloaded file to 'path'

if (path_is_url) {
tmp <- tempfile(pattern = "gtfs", fileext = ".zip")
tmp <- fs::file_temp(pattern = "gtfs", ext = ".zip")
utils::download.file(path, tmp, method = "auto", quiet = quiet)

if (!quiet) message("File downloaded to ", tmp, ".")
Expand All @@ -117,7 +117,7 @@ import_gtfs <- function(path,
)
if (inherits(filenames_in_gtfs, "error")) error_path_must_be_zip()

non_standard_file_ext <- filenames_in_gtfs[!(grepl("\\.txt$", filenames_in_gtfs) | grepl("\\.geojson$", filenames_in_gtfs))]
non_standard_file_ext <- filenames_in_gtfs[!(assert_extension(filenames_in_gtfs, "txt") | assert_extension(filenames_in_gtfs, "geojson"))]

if (!identical(non_standard_file_ext, character(0))) {
warning(
Expand Down Expand Up @@ -159,8 +159,10 @@ import_gtfs <- function(path,
# extract text files to temporary folder to later read them
# 'unlink()' makes sure that previous imports don't interfere with current one

tmpdir <- file.path(tempdir(), "gtfsio")
unlink(tmpdir, recursive = TRUE, force = TRUE)
tmpdir <- fs::path(fs::path_temp(), "gtfsio")
if (fs::dir_exists(tmpdir)) {
fs::dir_delete(tmpdir)
}

zip::unzip(
path,
Expand Down Expand Up @@ -190,12 +192,7 @@ import_gtfs <- function(path,
# assign names to 'gtfs', noting that zip_list may return full paths, which
# need to be stripped here

file_names <- vapply(
filenames_to_read,
function(i) utils::tail(strsplit(i, .Platform$file.sep)[[1]], 1),
character(1),
USE.NAMES = FALSE
)
file_names <- basename(filenames_to_read)
polettif marked this conversation as resolved.
Show resolved Hide resolved

names(gtfs) <- remove_file_ext(file_names)

Expand Down Expand Up @@ -241,19 +238,17 @@ read_files <- function(file,
encoding) {

# create object to hold the file with '.txt' extension
stopifnot(length(file) == 1L)

filename <- file
file_type <- "txt" # TODO get file ext as function
if(grepl("\\.geojson$", file)) {
file_type <- "geojson"
}
file_type <- ifelse(assert_extension(file, "txt"), "txt", "geojson")
file <- remove_file_ext(file)

if (!quiet) message("Reading ", file)

# read geojson and return
if (file_type == "geojson") {
return(read_geojson(file.path(tmpdir, filename)))
return(read_geojson(fs::path(tmpdir, filename)))
}

# get standards for reading and fields to be read from the given 'file'
Expand Down Expand Up @@ -283,7 +278,7 @@ read_files <- function(file,
withCallingHandlers(
{
sample_dt <- data.table::fread(
file.path(tmpdir, filename),
fs::path(tmpdir, filename),
nrows = 1,
colClasses = "character"
)
Expand Down Expand Up @@ -357,7 +352,7 @@ read_files <- function(file,
withCallingHandlers(
{
full_dt <- data.table::fread(
file.path(tmpdir, filename),
fs::path(tmpdir, filename),
select = fields_classes,
encoding = encoding
)
Expand All @@ -380,7 +375,7 @@ read_geojson <- function(file.geojson) {
}

remove_file_ext = function(file) {
tools::file_path_sans_ext(file)
fs::path_ext_remove(file)
}

append_file_ext = function(file) {
Expand All @@ -391,11 +386,10 @@ append_file_ext = function(file) {
# behaviour defined in test_import_gtfs.R#292
file_ext <- "txt"
}
if(endsWith(.f, paste0(".", file_ext))) {
return(.f) # file extension already present
} else {
return(paste0(.f, ".", file_ext))
if (!identical(fs::path_ext(.f), file_ext)) {
mpadge marked this conversation as resolved.
Show resolved Hide resolved
.f <- fs::path_ext_set(.f, file_ext)
}
return(.f)
}, ".txt", USE.NAMES = FALSE)
}

Expand Down
Loading