Skip to content

Instantly share code, notes, and snippets.

@jmbarbone
Last active July 4, 2024 14:38
Show Gist options
  • Save jmbarbone/4b988f9e90de2cc7b51cd27e20a65475 to your computer and use it in GitHub Desktop.
Save jmbarbone/4b988f9e90de2cc7b51cd27e20a65475 to your computer and use it in GitHub Desktop.
{databricks} but it's an R6 object
databricks <- local(envir = new.env(), {
.self <- environment()
.client <- NULL
.call <- function(method, args) {
if (is.null(.client)) {
stop(
"No client conigured",
"\nplease use $configure() function to set client",
call. = FALSE
)
}
args$client <- .client
fun <- get(method, envir = asNamespace("databricks"))
res <- do.call(fun, args)
if (inherits(res, "data.frame") && requireNamespace("tibble", quietly = TRUE)) {
res <- tibble::as_tibble(res)
}
class(res) <- c("databricks", class(res))
res
}
configure <- function() {}
formals(configure) <- formals(databricks::DatabricksClient)
body(configure) <- substitute({
args <- as.list(match.call())[-1L]
assign(".client", do.call(databricks::DatabricksClient, args), .self)
invisible(.self)
})
local({
databricks_exports <- getNamespaceExports("databricks")
if (utils::packageVersion("databricks") <= "0.4.0") {
# All functions are
databricks_exports <- grep("^[a-z]+[A-Z][a-z]+$", databricks_exports, value = TRUE)
} else {
# But were changes to snake_case
databricks_exports <- grep("^[a-z_]+$", databricks_exports, value = TRUE)
}
for (i in databricks_exports) {
fun <- get0(i, envir = asNamespace("databricks"), mode = "function")
if (is.null(fun)) {
next
}
args <- formals(fun)
if (!isTRUE(names(args)[1L] == "client")) {
next
}
method <- function() {}
formals(method) <- args[-1L]
body(method) <- substitute({
.call(i, as.list(match.call())[-1L])
})
assign(i, method, envir = .self)
}
})
.self
})
try(databricks$clustersList())
databricks$configure()
if (packageVersion("databricks") <= "0.4.0") {
databricks$clustersList()
} else {
databricks$list_clusters()
}
databricks_client <- R6::R6Class(
"dbClient",
public = local({
databricks_exports <- getNamespaceExports("databricks")
if (utils::packageVersion("databricks") <= "0.4.0") {
# All functions are
databricks_exports <- grep("^[a-z]+[A-Z][a-z]+$", databricks_exports, value = TRUE)
} else {
# But were changes to snake_case
databricks_exports <- grep("^[a-z_]+$", databricks_exports, value = TRUE)
}
res <- list()
res$initialize <- function() {}
formals(res$initialize) <- formals(databricks::DatabricksClient)
body(res$initialize) <- substitute({
args <- as.list(match.call())[-1L]
private$client <- do.call(databricks::DatabricksClient, args)
self
})
for (i in databricks_exports) {
fun <- get0(i, envir = asNamespace("databricks"), mode = "function")
if (is.null(fun)) {
next
}
args <- formals(fun)
if (!isTRUE(names(args)[1L] == "client")) {
next
}
method <- function() {}
formals(method) <- args[-1L]
body(method) <- substitute({
private$call(i, as.list(match.call())[-1L])
})
res[[i]] <- method
}
res
}),
private = list(
client = NULL,
call = function(method, args) {
args$client <- private$client
fun <- get(method, envir = asNamespace("databricks"))
res <- do.call(fun, args)
if (inherits(res, "data.frame") && requireNamespace("tibble", quietly = TRUE)) {
res <- tibble::as_tibble(res)
}
class(res) <- c("databricks", class(res))
res
}
)
)
print.databricks <- function(x, ...) {
if (inherits(x, "data.frame")) {
NextMethod()
}
if (inherits(x, "list")) {
if (requireNamespace("yaml", quietly = TRUE)) {
yaml::write_yaml(x, file = stdout())
} else {
utils::str(x)
}
return(invisible(x))
}
NextMethod()
}
client <- databricks_client$new()
if (packageVersion("databricks") <= "0.4.0") {
client$clustersList()
} else {
client$list_clusters()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment