From ceb456bb95d1c5c4a9a93165421dae353b2c1896 Mon Sep 17 00:00:00 2001 From: AlbanSagouis Date: Wed, 7 Oct 2026 16:05:53 +0200 Subject: [PATCH] Format with air --- R/gn_debug.R | 2 +- R/gn_parse.R | 17 +++- R/gn_parse_tidy.R | 4 +- R/gn_version.R | 10 ++- R/gnparser.R | 120 +++++++++++++++++++--------- R/install_gnparser.R | 11 +-- R/zzz.R | 11 ++- inst/precompile.R | 2 +- tests/testthat/test-gn_parse.R | 35 +++++--- tests/testthat/test-gn_parse_tidy.R | 7 +- 10 files changed, 148 insertions(+), 71 deletions(-) diff --git a/R/gn_debug.R b/R/gn_debug.R index dc7154d..c381c7d 100644 --- a/R/gn_debug.R +++ b/R/gn_debug.R @@ -5,5 +5,5 @@ #' @export #' @param ... ignored gn_debug <- function(...) { - .Defunct(msg="gn_debug is defunct, functionality was removed from gnparser") + .Defunct(msg = "gn_debug is defunct, functionality was removed from gnparser") } diff --git a/R/gn_parse.R b/R/gn_parse.R index 7156644..6d0c27d 100644 --- a/R/gn_parse.R +++ b/R/gn_parse.R @@ -44,15 +44,24 @@ gn_parse <- function( cultivar = FALSE, capitalize = FALSE, diaereses = FALSE, - details = FALSE) { - + details = FALSE +) { gnparser_exists() ver_check(1) assert(x, "character") file <- tempfile(fileext = ".txt") on.exit(unlink(file)) cat(x, file = file, sep = "\n") - res <- parse_one(file, "compact", threads, batch_size, - ignore_tags, cultivar, capitalize, diaereses, details) + res <- parse_one( + file, + "compact", + threads, + batch_size, + ignore_tags, + cultivar, + capitalize, + diaereses, + details + ) lapply(strsplit(res, "\n")[[1]], jsonlite::fromJSON) } diff --git a/R/gn_parse_tidy.R b/R/gn_parse_tidy.R index ec410a9..0f03346 100644 --- a/R/gn_parse_tidy.R +++ b/R/gn_parse_tidy.R @@ -22,8 +22,8 @@ gn_parse_tidy <- function( cultivar = FALSE, capitalize = FALSE, diaereses = FALSE, - ignore_tags = FALSE) { - + ignore_tags = FALSE +) { gnparser_exists() ver_check(1) assert(x, "character") diff --git a/R/gn_version.R b/R/gn_version.R index 05dfe82..a3f7ffe 100644 --- a/R/gn_version.R +++ b/R/gn_version.R @@ -1,10 +1,10 @@ #' gn_version -#' +#' #' get gnparser version information -#' +#' #' @export #' @return named list, with `version` and `build` -#' @examples +#' @examples #' trys <- function(x) try(x, silent=TRUE) #' if (interactive()) { #' trys(gn_version()) @@ -13,7 +13,9 @@ gn_version <- function() { gnparser_exists() # z <- gnparser_cmd("-V", error = FALSE) z <- gnparser_cmd("-V", error = FALSE) - if (z$status != 0) z <- gnparser_cmd("-v", error = FALSE) + if (z$status != 0) { + z <- gnparser_cmd("-v", error = FALSE) + } err_chk(z) process_version_string(z$stdout) # txt <- rawToChar(z$stdout) diff --git a/R/gnparser.R b/R/gnparser.R index 626e495..286c41f 100644 --- a/R/gnparser.R +++ b/R/gnparser.R @@ -1,44 +1,65 @@ #' @noRd #' @param ... Arguments to be passed to `sys::exec_internal('gnparser', ...)` -gnparser_cmd = function(...) { +gnparser_cmd <- function(...) { sys::exec_internal(find_gnparser(), ...) } # find an executable from PATH, APPDATA, system.file(), ~/bin, etc -find_exec = function(cmd, dir, info = '') { +find_exec <- function(cmd, dir, info = '') { for (d in bin_paths(dir)) { - exec = if (is_windows()) paste0(cmd, ".exe") else cmd - path = file.path(d, exec) - if (utils::file_test("-x", path)) break else path = '' + exec <- if (is_windows()) paste0(cmd, ".exe") else cmd + path <- file.path(d, exec) + if (utils::file_test("-x", path)) break else path <- '' } - path2 = Sys.which(cmd) + path2 <- Sys.which(cmd) if (path == '' || same_path(path, path2)) { - if (path2 == '') stop(cmd, ' not found. ', info, call. = FALSE) - return(cmd) # do not use the full path of the command + if (path2 == '') { + stop(cmd, ' not found. ', info, call. = FALSE) + } + return(cmd) # do not use the full path of the command } else { - if (path2 != '') warning( - 'Found ', cmd, ' at "', path, '" and "', path2, '". The former will be used. ', - "If you don't need both copies, you may delete/uninstall one." - ) + if (path2 != '') { + warning( + 'Found ', + cmd, + ' at "', + path, + '" and "', + path2, + '". The former will be used. ', + "If you don't need both copies, you may delete/uninstall one." + ) + } } normalizePath(path) } gnpenv <- new.env() -find_gnparser = local({ - gnpenv$path = NULL # cache the path to gnparser +find_gnparser <- local({ + gnpenv$path <- NULL # cache the path to gnparser function() { - if (is.null(gnpenv$path)) gnpenv$path <- find_exec( - 'gnparser', 'gnparser', 'You need to install gnparser' - ) + if (is.null(gnpenv$path)) { + gnpenv$path <- find_exec( + 'gnparser', + 'gnparser', + 'You need to install gnparser' + ) + } gnpenv$path } }) -parse_one <- function(x, format = NULL, threads = NULL, - batch_size = NULL, ignore_tags = NULL, cultivar = FALSE, - capitalize = FALSE, diaereses = FALSE, details = FALSE) { - +parse_one <- function( + x, + format = NULL, + threads = NULL, + batch_size = NULL, + ignore_tags = NULL, + cultivar = FALSE, + capitalize = FALSE, + diaereses = FALSE, + details = FALSE +) { assert(format, "character") assert(threads, c("integer", "numeric")) # NULL OK assert(batch_size, c("integer", "numeric")) @@ -49,14 +70,30 @@ parse_one <- function(x, format = NULL, threads = NULL, assert(details, "logical") args <- character(0) - if (!is.null(format)) args <- c(args, "--format", format) - if (!is.null(threads)) args <- c(args, "--jobs", threads) - if (!is.null(batch_size)) args <- c(args, "--batch_size", batch_size) - if (ignore_tags) args <- c(args, "--ignore_tags") - if (cultivar) args <- c(args, "--cultivar") - if (capitalize) args <- c(args, "--capitalize") - if (diaereses) args <- c(args, "--diaereses") - if (details) args <- c(args, "--details") + if (!is.null(format)) { + args <- c(args, "--format", format) + } + if (!is.null(threads)) { + args <- c(args, "--jobs", threads) + } + if (!is.null(batch_size)) { + args <- c(args, "--batch_size", batch_size) + } + if (ignore_tags) { + args <- c(args, "--ignore_tags") + } + if (cultivar) { + args <- c(args, "--cultivar") + } + if (capitalize) { + args <- c(args, "--capitalize") + } + if (diaereses) { + args <- c(args, "--diaereses") + } + if (details) { + args <- c(args, "--details") + } z <- gnparser_cmd(c(args, x), error = FALSE) err_chk(z) return(rawToChar(z$stdout)) @@ -64,17 +101,22 @@ parse_one <- function(x, format = NULL, threads = NULL, gnparser_exists <- function() { check_gnp <- gnparser_cmd() - if (check_gnp$status != 0) stop("You need to install gnparser") + if (check_gnp$status != 0) { + stop("You need to install gnparser") + } return(TRUE) } process_version_string <- function(x) { txt <- rawToChar(x) txt <- strsplit(txt, "\n")[[1]] - unlist(lapply(txt[nzchar(txt)], function(w) { - tmp <- gsub("\\s", "", strsplit(w, ":\\s")[[1]]) - stats::setNames(list(tmp[2]), tmp[1]) - }), FALSE) + unlist( + lapply(txt[nzchar(txt)], function(w) { + tmp <- gsub("\\s", "", strsplit(w, ":\\s")[[1]]) + stats::setNames(list(tmp[2]), tmp[1]) + }), + FALSE + ) } ver_check <- function(version) { @@ -83,7 +125,9 @@ ver_check <- function(version) { # ver <- process_version_string(ver$stdout) ver <- gn_version() ver_first_num <- as.numeric(substring(gsub("v|\\.", "", ver$version), 1, 1)) - if (ver_first_num < version) stop("you need to install `gnparser` v1 or greater") + if (ver_first_num < version) { + stop("you need to install `gnparser` v1 or greater") + } return(TRUE) } @@ -93,7 +137,9 @@ same_path <- function(p1, p2, ...) { } # from xfun::normalize_path normalize_path <- function(path, winslash = "/", must_work = FALSE) { - res = normalizePath(path, winslash = winslash, mustWork = must_work) - if (is_windows()) res[is.na(path)] = NA + res <- normalizePath(path, winslash = winslash, mustWork = must_work) + if (is_windows()) { + res[is.na(path)] <- NA + } res } diff --git a/R/install_gnparser.R b/R/install_gnparser.R index 3a5212a..1ba9f70 100644 --- a/R/install_gnparser.R +++ b/R/install_gnparser.R @@ -27,9 +27,10 @@ #' installed. This may be useful when upgrading gnparser. #' @export -install_gnparser = function(version, force) { - lifecycle::deprecate_stop( - when = "0.3.0", - what = "install_gnparser()", - details = "Please see help page for deprecation reason and solution.") +install_gnparser <- function(version, force) { + lifecycle::deprecate_stop( + when = "0.3.0", + what = "install_gnparser()", + details = "Please see help page for deprecation reason and solution." + ) } diff --git a/R/zzz.R b/R/zzz.R index 1ae3a7d..94bf48e 100644 --- a/R/zzz.R +++ b/R/zzz.R @@ -1,9 +1,12 @@ - assert <- function(x, y) { if (!is.null(x)) { if (!inherits(x, y)) { - stop(deparse(substitute(x)), " must be of class ", - paste0(y, collapse = ", "), call. = FALSE) + stop( + deparse(substitute(x)), + " must be of class ", + paste0(y, collapse = ", "), + call. = FALSE + ) } } } @@ -29,7 +32,7 @@ readcsv <- function(x) { is_windows <- function() .Platform$OS.type == "windows" is_macos <- function() unname(Sys.info()["sysname"] == "Darwin") dir_exists <- function(x) utils::file_test("-d", x) -pkg_file = function(..., mustWork = TRUE) { +pkg_file <- function(..., mustWork = TRUE) { system.file(..., package = 'rgnparser', mustWork = mustWork) } diff --git a/inst/precompile.R b/inst/precompile.R index 3fdfe90..e0285a4 100644 --- a/inst/precompile.R +++ b/inst/precompile.R @@ -9,4 +9,4 @@ load_all() knit("vignettes/rgnparser.Rmd.orig", "vignettes/rgnparser.Rmd") -build_vignettes() \ No newline at end of file +build_vignettes() diff --git a/tests/testthat/test-gn_parse.R b/tests/testthat/test-gn_parse.R index b87c291..8b00479 100644 --- a/tests/testthat/test-gn_parse.R +++ b/tests/testthat/test-gn_parse.R @@ -2,14 +2,19 @@ skip_on_cran() skip_on_ci() test_that("gn_parse", { - x <- c("Quadrella steyermarkii (Standl.) Iltis & Cornejo", - "Parus major Linnaeus, 1788", "Helianthus annuus var. texanus") + x <- c( + "Quadrella steyermarkii (Standl.) Iltis & Cornejo", + "Parus major Linnaeus, 1788", + "Helianthus annuus var. texanus" + ) w <- gn_parse(x) vapply(w, "[[", "", "normalized") expect_is(w, "list") - for (i in w) expect_is(i, "list") - + for (i in w) { + expect_is(i, "list") + } + expect_is(w[[1]]$parsed, "logical") expect_is(w[[1]]$verbatim, "character") expect_is(w[[1]]$normalized, "character") @@ -22,23 +27,29 @@ test_that("cultivar arg works", { with_cult <- gn_parse("Sarracenia flava 'Maxima'", cultivar = TRUE) without_cult <- gn_parse("Sarracenia flava 'Maxima'", cultivar = FALSE) expect_equal( - with_cult[[1]]$canonical$simple, "Sarracenia flava ‘Maxima’" + with_cult[[1]]$canonical$simple, + "Sarracenia flava ‘Maxima’" ) expect_equal( - without_cult[[1]]$canonical$simple, "Sarracenia flava" + without_cult[[1]]$canonical$simple, + "Sarracenia flava" ) }) test_that("capitalize arg works", { with_capital <- gn_parse("parus major", capitalize = TRUE) without_capital <- gn_parse( - "parus major", capitalize = FALSE) + "parus major", + capitalize = FALSE + ) expect_equal( - with_capital[[1]]$canonical$simple, "Parus major" + with_capital[[1]]$canonical$simple, + "Parus major" ) # Without capitalization, name cannot be parsed expect_equal( - without_capital[[1]]$parsed, FALSE + without_capital[[1]]$parsed, + FALSE ) }) @@ -46,9 +57,11 @@ test_that("diaereses arg works", { with_dia <- gn_parse("Leptochloöpsis virgata", diaereses = TRUE) without_dia <- gn_parse("Leptochloöpsis virgata", diaereses = FALSE) expect_equal( - with_dia[[1]]$canonical$simple, "Leptochloöpsis virgata" + with_dia[[1]]$canonical$simple, + "Leptochloöpsis virgata" ) expect_equal( - without_dia[[1]]$canonical$simple, "Leptochlooepsis virgata" + without_dia[[1]]$canonical$simple, + "Leptochlooepsis virgata" ) }) diff --git a/tests/testthat/test-gn_parse_tidy.R b/tests/testthat/test-gn_parse_tidy.R index bf99fa4..29ddf00 100644 --- a/tests/testthat/test-gn_parse_tidy.R +++ b/tests/testthat/test-gn_parse_tidy.R @@ -2,8 +2,11 @@ skip_on_cran() skip_on_ci() test_that("gn_parse_tidy", { - x <- c("Quadrella steyermarkii (Standl.) Iltis & Cornejo", - "Parus major Linnaeus, 1788", "Helianthus annuus var. texanus") + x <- c( + "Quadrella steyermarkii (Standl.) Iltis & Cornejo", + "Parus major Linnaeus, 1788", + "Helianthus annuus var. texanus" + ) z <- gn_parse_tidy(x) expect_is(z, "data.frame")