Skip to content

MINOR: format with air #216

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

Merged
merged 1 commit into from
Mar 11, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# tidyhydat 0.7.0.9999
- add columns added by a webservice update (#213)
- fix some partial matching in `realtime_add_local_datetime` internals
- re-format all code with the lovely air tool

# tidyhydat 0.7.0
- bump minimum R version to 4.2.0
Expand Down
1 change: 0 additions & 1 deletion R/cli_style.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and limitations under the License.


## drawing heavily from the tidyverse package

done <- function(msg) {
Expand Down
3 changes: 1 addition & 2 deletions R/data.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and limitations under the License.


#' All Canadian stations
#'
#' A shorthand to avoid having always call `hy_stations` or `realtime_stations`.
Expand Down Expand Up @@ -66,7 +65,7 @@
#' @title Parameter ID
#'
#' @description A tibble of parameter id codes and their corresponding explanation/description specific to the ECCC webservice
#'
#'
#' @format A tibble with 8 rows and 7 variables:
#' \describe{
#' \item{Parameter}{Numeric parameter code}
Expand Down
52 changes: 33 additions & 19 deletions R/download.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,14 @@ download_hydat <- function(dl_hydat_here = NULL, ask = TRUE) {
if (!dir.exists(dl_hydat_here)) {
dir.create(dl_hydat_here)
message(crayon::blue("You have downloaded hydat to", dl_hydat_here))
message(crayon::blue("See ?hy_set_default_db to change where tidyhydat looks for HYDAT"))
message(crayon::blue(
"See ?hy_set_default_db to change where tidyhydat looks for HYDAT"
))
}
}

if (!is.logical(ask)) stop("Parameter ask must be a logical")


## Create actual hydat_path
hydat_path <- file.path(dl_hydat_here, "Hydat.sqlite3")

Expand All @@ -52,7 +53,6 @@ download_hydat <- function(dl_hydat_here = NULL, ask = TRUE) {
existing_hydat <- "HYDAT not present"
}


new_hydat <- hy_remote()
# Make the download URL
url <- paste0(hy_base_url(), "Hydat_sqlite3_", new_hydat, ".zip")
Expand All @@ -62,30 +62,40 @@ download_hydat <- function(dl_hydat_here = NULL, ask = TRUE) {
req <- tidyhydat_perform(req)
httr2::resp_check_status(req)

size <- round(as.numeric(
httr2::resp_header(req, "Content-Length")
) / 1000000, 0)

size <- round(
as.numeric(
httr2::resp_header(req, "Content-Length")
) /
1000000,
0
)

## Do we need to download a new version?
if (new_hydat == existing_hydat & ask) { # DB exists and no new version
if (new_hydat == existing_hydat & ask) {
# DB exists and no new version
msg <- paste0(
"The existing local version of HYDAT, published on ",
lubridate::ymd(existing_hydat),
", is the most recent version available. \nDo you wish to overwrite it? \nDownloading HYDAT could take up to 10 minutes (",
size, " MB)."
size,
" MB)."
)
dl_overwrite <- ask(msg)
} else {
dl_overwrite <- TRUE
}

if (!dl_overwrite) {
info("HYDAT is updated on a quarterly basis, check again soon for an updated version.")
info(
"HYDAT is updated on a quarterly basis, check again soon for an updated version."
)
}
if (new_hydat != existing_hydat & ask) { # New DB available or no local DB at all
if (new_hydat != existing_hydat & ask) {
# New DB available or no local DB at all
msg <- paste0(
"This version of HYDAT is ", size, "MB in size and will take some time to download.
"This version of HYDAT is ",
size,
"MB in size and will take some time to download.
\nThis will remove any older versions of HYDAT, if applicable. \nIs that okay?"
)
ans <- ask(msg)
Expand All @@ -99,12 +109,18 @@ download_hydat <- function(dl_hydat_here = NULL, ask = TRUE) {
green_message(paste0("Downloading HYDAT to ", dl_hydat_here))
}


if (dl_overwrite) {
if (new_hydat == existing_hydat) {
info(paste0("Your local copy of HYDAT published on ", crayon::blue(lubridate::ymd(new_hydat)), " will be overwritten."))
info(paste0(
"Your local copy of HYDAT published on ",
crayon::blue(lubridate::ymd(new_hydat)),
" will be overwritten."
))
} else {
info(paste0("Downloading new version of HYDAT created on ", crayon::blue(lubridate::ymd(new_hydat))))
info(paste0(
"Downloading new version of HYDAT created on ",
crayon::blue(lubridate::ymd(new_hydat))
))
}

## temporary path to save
Expand All @@ -130,7 +146,6 @@ download_hydat <- function(dl_hydat_here = NULL, ask = TRUE) {
overwrite = TRUE
)


if (file.exists(hydat_path)) {
congrats("HYDAT successfully downloaded")
} else {
Expand Down Expand Up @@ -160,10 +175,10 @@ hy_remote <- function() {
req <- tidyhydat_perform(req)
resp <- httr2::resp_check_status(req)


raw_date <- substr(
gsub("^.*\\Hydat_sqlite3_", "", httr2::resp_body_string(req)),
1, 8
1,
8
)

raw_date
Expand All @@ -182,7 +197,6 @@ hy_check <- function(hydat_path = NULL) {
red_message(paste0(tbl_diff, "\n"))
}


invisible(lapply(have_tbls, function(x) {
tbl_rows <- dplyr::tbl(con, x) |>
utils::head(1) |>
Expand Down
29 changes: 23 additions & 6 deletions R/hy-classes.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ print.hy <- function(x, ...) {
}

summary_msg <- function(x) {
cat(paste0(" Queried from version of HYDAT released on ", as.Date(hy_version()$Date), "\n"))
cat(paste0(
" Queried from version of HYDAT released on ",
as.Date(hy_version()$Date),
"\n"
))

n_records <- format(nrow(x), big.mark = ",")
cat(paste0(" Observations: ", n_records, "\n"))
Expand All @@ -36,11 +40,19 @@ summary_msg <- function(x) {
}

if ("PROV_TERR_STATE_LOC" %in% names(x)) {
cat(paste0(" Jurisdictions: ", paste0(unique(x$PROV_TERR_STATE_LOC), collapse = ", "), "\n"))
cat(paste0(
" Jurisdictions: ",
paste0(unique(x$PROV_TERR_STATE_LOC), collapse = ", "),
"\n"
))
}

if ("Parameter" %in% names(x)) {
cat(paste0(" Parameter(s): ", paste0(unique(x$Parameter), collapse = "/"), "\n"))
cat(paste0(
" Parameter(s): ",
paste0(unique(x$Parameter), collapse = "/"),
"\n"
))
}
}

Expand All @@ -58,12 +70,17 @@ missed_station_msg <- function(x) {
cat(" Stations requested but not returned: \n")
if (length(differ) != 0) {
if (length(differ) > 50) {
cat(crayon::cyan(" More than 50 stations requested but not returned. \n"))
cat(crayon::cyan(paste0(" See object attributes for complete list of missing stations.\n")))
cat(crayon::cyan(
" More than 50 stations requested but not returned. \n"
))
cat(crayon::cyan(paste0(
" See object attributes for complete list of missing stations.\n"
)))
} else {
cat(
crayon::cyan(
paste0(" ",
paste0(
" ",
strwrap(
paste0(differ, collapse = " "),
width = 40
Expand Down
Loading
Loading