Skip to content

Commit 904e827

Browse files
committed
create check_path() and use throughout the package
1 parent cf6bb1a commit 904e827

File tree

10 files changed

+91
-75
lines changed

10 files changed

+91
-75
lines changed

R/cli.R

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,3 +310,35 @@ check_bool <- function(x, alt_null = FALSE, add_msg = NULL) {
310310
))
311311
}
312312
}
313+
314+
315+
#' @title Validate a path
316+
#'
317+
#' @description Makes sure that a path passed to a cloud function is in the
318+
#' right format.
319+
#'
320+
#' @param file Path to a file relative to project folder root. Can contain only
321+
#' letters, digits, '-', '_', '.', spaces and '/' symbols.
322+
#' @param error if `TRUE` (default), throws an error if `file` is not a valid
323+
#' file path.
324+
#'
325+
#' @return Either `TRUE` or `FALSE` if `error` is `FALSE`. Either `TRUE` or
326+
#' an error if `error` is `TRUE`.
327+
#'
328+
#' @keywords internal
329+
check_path <- function(path, error = TRUE) {
330+
res <- grepl("^([A-Za-z]|[0-9]|-|_|\\.| |/)+$", path)
331+
if (error) {
332+
if (path == "") cli::cli_abort("A valid path must not be empty.")
333+
if (!res) cli_abort(c(
334+
"Path '{path}' is not valid",
335+
"A valid path must consist of:",
336+
"*" = "uppercase/lowercase letters",
337+
"*" = "digits",
338+
"*" = "'/' symbols to separate directories in the path",
339+
"*" = "'_', '-', '.' symbols or spaces"
340+
))
341+
}
342+
res
343+
}
344+

R/cloud_local.R

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -129,17 +129,7 @@ cloud_object_ls <- function(x, path, extension, prefix = "", suffix = "") {
129129
check_string(extension)
130130
check_string(prefix)
131131
check_string(suffix)
132-
133-
if (!grepl("^([A-Za-z]|[0-9]|-|_|\\.|/)+$", path)) {
134-
cli::cli_abort(c(
135-
"Directory path {.path {path}} is not valid. A valid directory path must \\
136-
consist of:",
137-
"*" = "uppercase/lowercase letters",
138-
"*" = "digits",
139-
"*" = "'/' symbols to describe its location inside project's folder",
140-
"*" = "'_', '-', '.' symbols or spaces."
141-
))
142-
}
132+
check_path(path)
143133

144134
if (!grepl("^([A-Za-z]|[0-9])+$", extension)) {
145135
cli::cli_abort(c(

R/common.R

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -49,38 +49,6 @@ proj_desc_get <- function(key, project = ".") {
4949
unname(value)
5050
}
5151

52-
#' @title Validate file path for cloud functions
53-
#'
54-
#' @description Makes sure that file path passed to a cloud function is in the
55-
#' right format.
56-
#'
57-
#' @param file Path to a file relative to project folder root. Can contain only
58-
#' letters, digits, '-', '_', '.', spaces and '/' symbols.
59-
#' @param error if `TRUE` (default), throws an error if `file` is not a valid
60-
#' file path.
61-
#'
62-
#' @return Either `TRUE` or `FALSE` if `error` is `FALSE`. Either `TRUE` or
63-
#' an error if `error` is `TRUE`.
64-
#'
65-
#' @keywords internal
66-
cloud_validate_file_path <- function(file, error = TRUE) {
67-
check_string(file)
68-
res <- grepl("^([A-Za-z]|[0-9]|-|_|\\.| |/)+$", file)
69-
if (error) {
70-
if (file == "") cli::cli_abort("A valid file name should not be empty.")
71-
if (!res) cli_abort(c(
72-
"File name '{file}' is not valid",
73-
"A valid file name must consist of:",
74-
"*" = "uppercase/lowercase letters",
75-
"*" = "digits",
76-
"*" = "spaces",
77-
"*" = "'/' symbols to describe its location inside project's folder",
78-
"*" = "'_', '-', '.' symbols"
79-
))
80-
}
81-
res
82-
}
83-
8452
#' @title Validate file names
8553
#'
8654
#' @description Given a character vector of filenames checks that all names pass

R/doc.R

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#' @title Package-wide description of `file` parameter
2+
#' @description A dummy function to be referred by `@inheritParams` for a
3+
#' parameter documentation.
4+
#'
5+
#' @param file Path to a file relative to project folder root. Can contain only
6+
#' letters, digits, '-', '_', '.', spaces and '/' symbols.
7+
#'
8+
#' @keywords internal
9+
doc_file <- function(file) {}

R/drive_transfer.R

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#' @description Uploads a local file from the project's directory to its
44
#' corresponding location within the project's Google Drive root folder.
55
#'
6-
#' @inheritParams cloud_validate_file_path
6+
#' @inheritParams doc_file
77
#' @inheritParams cloud_drive_ls
88
#'
99
#' @inherit cloud_drive_find_path details
@@ -25,7 +25,7 @@
2525
#'
2626
#' @export
2727
cloud_drive_upload <- function(file, root = NULL) {
28-
cloud_validate_file_path(file)
28+
check_path(file)
2929

3030
if (!file.exists(file)) {
3131
cli::cli_abort("File {.path {file}} does not exist.")
@@ -51,7 +51,7 @@ cloud_drive_upload <- function(file, root = NULL) {
5151
#' saves it to the local project folder, maintaining the original folder
5252
#' structure.
5353
#'
54-
#' @inheritParams cloud_validate_file_path
54+
#' @inheritParams doc_file
5555
#' @inheritParams cloud_drive_ls
5656
#'
5757
#' @inherit cloud_drive_find_path details
@@ -68,7 +68,7 @@ cloud_drive_upload <- function(file, root = NULL) {
6868
#'
6969
#' @export
7070
cloud_drive_download <- function(file, root = NULL) {
71-
cloud_validate_file_path(file)
71+
check_path(file)
7272

7373
check_string(root, alt_null = TRUE)
7474
if (is.null(root)) root <- cloud_drive_get_root()
@@ -95,7 +95,7 @@ cloud_drive_download <- function(file, root = NULL) {
9595
#' function will infer the appropriate writing method based on the file's
9696
#' extension.
9797
#'
98-
#' @inheritParams cloud_validate_file_path
98+
#' @inheritParams doc_file
9999
#' @inheritParams cloud_drive_ls
100100
#'
101101
#' @param x An R object to be written to Google Drive.
@@ -121,7 +121,7 @@ cloud_drive_download <- function(file, root = NULL) {
121121
#' @export
122122
cloud_drive_write <- function(x, file, fun = NULL, ..., local = FALSE,
123123
root = NULL) {
124-
cloud_validate_file_path(file)
124+
check_path(file)
125125
check_bool(local)
126126

127127
if (is.null(fun)) {
@@ -162,7 +162,7 @@ cloud_drive_write <- function(x, file, fun = NULL, ..., local = FALSE,
162162
#' reading function based on the file's extension. However, you can specify a
163163
#' custom reading function if necessary.
164164
#'
165-
#' @inheritParams cloud_validate_file_path
165+
#' @inheritParams doc_file
166166
#' @inheritParams cloud_drive_ls
167167
#'
168168
#' @param fun A custom reading function. If `NULL` (default), the appropriate
@@ -185,7 +185,7 @@ cloud_drive_write <- function(x, file, fun = NULL, ..., local = FALSE,
185185
#'
186186
#' @export
187187
cloud_drive_read <- function(file, fun = NULL, ..., root = NULL) {
188-
cloud_validate_file_path(file)
188+
check_path(file)
189189
if (is.null(fun)) {
190190
fun <- cloud_guess_read_fun(file)
191191
}

R/drive_utils.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ cloud_drive_download_by_id <- function(file, path, overwrite = FALSE) {
7878
#'
7979
#' @noRd
8080
cloud_drive_guess_type <- function(file) {
81-
cloud_validate_file_path(file)
81+
check_path(file)
8282
ext <- tolower(tools::file_ext(file))
8383
switch (
8484
ext,
@@ -112,7 +112,7 @@ cloud_drive_put <- function(media, path) {
112112
#' @description Finds the spreadsheet by path relative to a project root.
113113
#' Applies [googlesheets4::range_autofit()] to each sheet.
114114
#'
115-
#' @inheritParams cloud_validate_file_path
115+
#' @inheritParams doc_file
116116
#' @inheritParams cloud_drive_ls
117117
#'
118118
#' @return The file ID of the resized Google spreadsheet as an invisible result.
@@ -123,7 +123,7 @@ cloud_drive_put <- function(media, path) {
123123
#'
124124
#' @export
125125
cloud_drive_spreadsheet_autofit <- function(file, root = NULL) {
126-
cloud_validate_file_path(file)
126+
check_path(file)
127127
check_string(root, alt_null = TRUE)
128128

129129
if (is.null(root)) root <- cloud_drive_get_root()

R/read_write.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#' @description Take a look at the switch call. That's basically it. Returns an
44
#' appropriate function or throws an error if wasn't able to find one.
55
#'
6-
#' @inheritParams cloud_validate_file_path
6+
#' @inheritParams doc_file
77
#'
88
#' @section Default writing functions:
99
#'
@@ -21,7 +21,7 @@
2121
#'
2222
#' @keywords internal
2323
cloud_guess_write_fun <- function(file) {
24-
cloud_validate_file_path(file)
24+
check_path(file)
2525
ext <- tolower(tools::file_ext(file))
2626
if (ext == "") stop("Missing file extension, unable to guess writing function.")
2727
fun <- switch (
@@ -47,7 +47,7 @@ cloud_guess_write_fun <- function(file) {
4747
#' @description Take a look at the switch call. That's basically it. Returns an
4848
#' appropriate function or throws an error if wasn't able to find one.
4949
#'
50-
#' @inheritParams cloud_validate_file_path
50+
#' @inheritParams doc_file
5151
#'
5252
#' @section Default reading functions:
5353
#'
@@ -64,7 +64,7 @@ cloud_guess_write_fun <- function(file) {
6464
#'
6565
#' @keywords internal
6666
cloud_guess_read_fun <- function(file) {
67-
cloud_validate_file_path(file)
67+
check_path(file)
6868
ext <- tolower(tools::file_ext(file))
6969
if (ext == "") stop("Missing file extension, unable to guess reading function.")
7070
fun <- switch (

R/s3_transfer.R

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#' @description Uploads a local file from the project's directory to its
44
#' corresponding location within the project's S3 root folder.
55
#'
6-
#' @inheritParams cloud_validate_file_path
6+
#' @inheritParams doc_file
77
#' @inheritParams cloud_s3_ls
88
#'
99
#' @return Invisibly returns `NULL` after successfully uploading the file.
@@ -21,7 +21,7 @@
2121
#'
2222
#' @export
2323
cloud_s3_upload <- function(file, root = NULL) {
24-
cloud_validate_file_path(file)
24+
check_path(file)
2525

2626
check_string(root, alt_null = TRUE)
2727
if (is.null(root)) root <- cloud_s3_get_root()
@@ -51,7 +51,7 @@ cloud_s3_upload <- function(file, root = NULL) {
5151
#' @description Retrieves a file from the project's S3 root folder and saves it
5252
#' to the local project folder, maintaining the original folder structure.
5353
#'
54-
#' @inheritParams cloud_validate_file_path
54+
#' @inheritParams doc_file
5555
#' @inheritParams cloud_s3_ls
5656
#'
5757
#' @return Invisibly returns `NULL` after successfully downloading the file.
@@ -66,7 +66,7 @@ cloud_s3_upload <- function(file, root = NULL) {
6666
#'
6767
#' @export
6868
cloud_s3_download <- function(file, root = NULL) {
69-
cloud_validate_file_path(file)
69+
check_path(file)
7070

7171
check_string(root, alt_null = TRUE)
7272
if (is.null(root)) root <- cloud_s3_get_root()
@@ -90,7 +90,7 @@ cloud_s3_download <- function(file, root = NULL) {
9090
#' S3 storage. If no custom writing function is specified, the function will
9191
#' infer the appropriate writing method based on the file's extension.
9292
#'
93-
#' @inheritParams cloud_validate_file_path
93+
#' @inheritParams doc_file
9494
#' @inheritParams cloud_s3_ls
9595
#'
9696
#' @param x An R object to be written to S3.
@@ -115,7 +115,7 @@ cloud_s3_download <- function(file, root = NULL) {
115115
#' @export
116116
cloud_s3_write <- function(x, file, fun = NULL, ..., local = FALSE,
117117
root = NULL) {
118-
cloud_validate_file_path(file)
118+
check_path(file)
119119
check_bool(local)
120120
check_class(fun, "function", alt_null = TRUE)
121121
check_string(root, alt_null = TRUE)
@@ -158,7 +158,7 @@ cloud_s3_write <- function(x, file, fun = NULL, ..., local = FALSE,
158158
#' function based on the file's extension. However, you can specify a custom
159159
#' reading function if necessary.
160160
#'
161-
#' @inheritParams cloud_validate_file_path
161+
#' @inheritParams doc_file
162162
#' @inheritParams cloud_s3_ls
163163
#'
164164
#' @param fun A custom reading function. If `NULL` (default), the appropriate
@@ -179,7 +179,7 @@ cloud_s3_write <- function(x, file, fun = NULL, ..., local = FALSE,
179179
#'
180180
#' @export
181181
cloud_s3_read <- function(file, fun = NULL, ..., root = NULL) {
182-
cloud_validate_file_path(file)
182+
check_path(file)
183183
check_string(root, alt_null = TRUE)
184184

185185
if (is.null(fun)) {

man/cloud_validate_file_path.Rd renamed to man/check_path.Rd

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/doc_file.Rd

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)