Skip to content

Commit

Permalink
check more udunits xml database paths; fix r-quantities#82
Browse files Browse the repository at this point in the history
  • Loading branch information
edzer committed Jan 22, 2018
1 parent bcf3f05 commit 312eeb8
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 25 deletions.
8 changes: 2 additions & 6 deletions R/init.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,11 @@
NULL

.onLoad = function(libname, pkgname) {
udunits_init(character(0))
udunits_init(.get_ud_xml_dir())
}

.onAttach <- function(libname, pkgname) {
msg <- "udunits system database read"
p0 <- Sys.getenv("UDUNITS2_XML_PATH")
if (p0 != "") {
msg <- paste(msg, "from", p0)
}
msg <- paste("udunits system database from", .get_ud_xml_dir(TRUE))
packageStartupMessage(msg)
}

Expand Down
12 changes: 6 additions & 6 deletions R/ud_units.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
# This is setup code and all fails if we do not do it and use the units
# in the list, so there are no explicit tests for this, thus the nocov

.get_ud_xml_dir <- function() {
udunits2_dir <- dirname(Sys.getenv("UDUNITS2_XML_PATH"))
if (udunits2_dir == "")
udunits2_dir <- "/usr/share/xml/udunits"
udunits2_dir
}
#.get_ud_xml_dir <- function() {
# udunits2_dir <- dirname(Sys.getenv("UDUNITS2_XML_PATH"))
# if (udunits2_dir == "")
# udunits2_dir <- "/usr/share/xml/udunits"
# udunits2_dir
#}

.read_ud_db_symbols <- function(dir, filename) {
if (! requireNamespace("xml2", quietly = TRUE))
Expand Down
36 changes: 23 additions & 13 deletions R/valid_udunits.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,29 @@
message("udunits system database read from " , udunits2_dir)
}



.get_ud_xml_dir <- function() {
ud_is_parseable("m") # make sure udunits2 has booted
# which sets the environment varialbe UDUNITS2_XML_PATH
udunits2_dir <- dirname(Sys.getenv("UDUNITS2_XML_PATH"))
if (udunits2_dir == "")
udunits2_dir <- dirname(system.file("share/udunits2.xml", package="units"))

if (!nzchar(udunits2_dir))
warning(
"Failed to read udunits system database: udunits2 will not work properly.\nPlease set the UDUNITS2_XML_PATH environment variable before attempting to read the units database")
udunits2_dir
.get_ud_xml_dir <- function(warn = FALSE) {
paths = c(
Sys.getenv("UDUNITS2_XML_PATH"),
"/usr/local/share/udunits/udunits2.xml",
"/usr/share/udunits/udunits2.xml",
"/usr/share/xml/udunits/udunits2.xml",
"/usr/share/udunits/udunits2.xml")

fallback = system.file("share/udunits2.xml", package="units")

w = which(file.exists(paths))
if (length(w) > 1 && warn)
warning(paste("multiple udunits databases present:", paste(paths[w], collapse = " ")))
p = if (length(w) == 0)
fallback
else
paths[w[1]]

if (!file.exists(p))
stop(
"Failed to identify udunits system database: units will not work properly.\nPlease set the UDUNITS2_XML_PATH environment variable before attempting to read the units database")
else
dirname(p)
}

.read_ud_db_symbols <- function(dir, filename) {
Expand Down

0 comments on commit 312eeb8

Please sign in to comment.