Skip to content
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

Reading user and instance settings #2

Merged
merged 1 commit into from
Oct 2, 2024
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
56 changes: 56 additions & 0 deletions R/lamindb_setup__connect_instance.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# https://github.com/laminlabs/lamindb-setup/blob/main/lamindb_setup/_connect_instance.py

get_owner_name_from_identifier <- function(
identifier
) {
if (grepl("/", identifier)) {
if (grepl("https://lamin.ai/", identifier)) {
identifier <- gsub("https://lamin.ai/", "", identifier)
}
split <- strsplit(identifier, "/")[[1]]
if (length(split) > 2) {
stop(
"The instance identifier needs to be 'owner/name', the instance name",
" (owner is current user) or the URL: https://lamin.ai/owner/name."
)
}
owner <- split[[1]]
name <- split[[2]]
} else {
stop("TODO: fetch settings from somewhere")
owner <- settings$user$handle
name <- identifier
}
return(list(owner = owner, name = name))
}


#' Connect to instance
#'
#' @param slug The instance slug `account_handle/instance_name` or URL.
#' If the instance is owned by you, it suffices to pass the instance name.
#'
#' @export
connect <- function(slug) {
owner_name <- get_owner_name_from_identifier(slug)
owner <- owner_name$owner
name <- owner_name$name

# ...
}

connect_instance <- function(
owner, name
) {
settings_file <- instance_settings_file(name, owner)
make_hub_request <- TRUE

if (file.exists(settings_file)) {
isettings <- load_instance_settings(settings_file)
make_hub_request <- FALSE
}
if (make_hub_request) {
hub_result <- load_instance_from_hub(owner = owner, name = name)
}
# ...
}
66 changes: 66 additions & 0 deletions R/lamindb_setup__core__settings_load.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# based on: https://github.com/laminlabs/lamindb-setup/blob/main/lamindb_setup/core/_settings_load.py

load_instance_settings <- function(
instance_settings_file = NULL
) {
if (is.null(instance_settings_file)) {
instance_settings_file <- current_instance_settings_file()
}
if (!file.exists(instance_settings_file)) {
stop("No instance is loaded! Call `lamin init` or `lamin load`")
}
settings_store <-
tryCatch({
parse_instance_settings(instance_settings_file)
}, error = function(e) {
content <- readLines(instance_settings_file)
stop(paste0(
"\n\n", e$message, "\n\n",
"Your instance settings file with\n\n",
paste(content, collapse = "\n"), "\n",
"is invalid (likely outdated), see validation error. ",
"Please delete ", instance_settings_file, " & ",
"reload (remote) or re-initialise (local) the instance ",
"with the same name & storage location."
))
})

setup_instance_from_store(settings_store)
}

load_or_create_user_settings <- function() {
current_user_settings_file <- current_user_settings_file()

usettings <-
if (!file.exists(current_user_settings_file)) {
warning("using anonymous user (to identify, call `lamin login`)")
stop("TODO: implement this")
} else {
load_user_settings(current_user_settings_file)
}

usettings
}

load_user_settings <- function(user_settings_file) {
settings_store <-
tryCatch({
parse_user_settings(user_settings_file)
}, error = function(e) {
stop(paste0(
"Your user settings file is invalid, please delete ",
user_settings_file, " and log in again."
))
})
setup_user_from_store(settings_store)
}

setup_instance_from_store <- function(store) {
# TODO: implement this?
return(store)
}

setup_user_from_store <- function(store) {
# TODO: implement this?
return(store)
}
153 changes: 153 additions & 0 deletions R/lamindb_setup__core__settings_store.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
# based on: https://github.com/laminlabs/lamindb-setup/blob/main/lamindb_setup/core/_settings_store.py

get_settings_dir <- function() {
settings_dir <- Sys.getenv("LAMIN_SETTINGS_DIR")

if (settings_dir != "") {
file.path(settings_dir, ".lamin")
} else {
file.path(Sys.getenv("HOME"), ".lamin")
}
}

get_settings_file_name_prefix <- function() {
lamin_env <- Sys.getenv("LAMIN_ENV")
if (lamin_env != "" && lamin_env != "prod") {
paste0(lamin_env, "--")
} else {
""
}
}

current_instance_settings_file <- function() {
settings_dir <- get_settings_dir()
settings_file_name_prefix <- get_settings_file_name_prefix()
file.path(settings_dir, paste0(settings_file_name_prefix, "current_instance.env"))
}

current_user_settings_file <- function() {
settings_dir <- get_settings_dir()
settings_file_name_prefix <- get_settings_file_name_prefix()
file.path(settings_dir, paste0(settings_file_name_prefix, "current_user.env"))
}

instance_settings_file <- function(name, owner) {
settings_dir <- get_settings_dir()
settings_file_name_prefix <- get_settings_file_name_prefix()
file.path(settings_dir, paste0(settings_file_name_prefix, "instance--", owner, "--", name, ".env"))
}

user_settings_file_email <- function(email) {
settings_dir <- get_settings_dir()
settings_file_name_prefix <- get_settings_file_name_prefix()
file.path(settings_dir, paste0(settings_file_name_prefix, "user--", email, ".env"))
}

user_settings_file_handle <- function(handle) {
settings_dir <- get_settings_dir()
settings_file_name_prefix <- get_settings_file_name_prefix()
file.path(settings_dir, paste0(settings_file_name_prefix, "user--", handle, ".env"))
}

system_storage_settings_file <- function() {
settings_dir <- get_settings_dir()
file.path(settings_dir, "storage.env")
}

settings_store__read_typed_env <- function(
env_file,
env_prefix,
field_types
) {
env <- readLines(env_file)

# remove comments
env2 <- env[!grepl("^#", env)]

# remove empty lines
env3 <- env2[env2 != ""]

parsed <- lapply(env3, function(x) {
x_split <- strsplit(x, "=")[[1]]

if (length(x_split) != 2) {
stop("Invalid line: ", x)
}

name_with_prefix <- x_split[[1]]
raw_value <- x_split[[2]]

if (!grepl(env_prefix, name_with_prefix)) {
stop("Invalid prefix: ", name_with_prefix)
}
name <- gsub(env_prefix, "", name_with_prefix)

if (!name %in% names(field_types)) {
stop("Unknown field: ", name)
}
raw_type <- field_types[[name]]

optional <- grepl("Optional\\[.*\\]", raw_type)
type <- gsub("Optional\\[(.*)\\]", "\\1", raw_type)

value <-
if (optional && raw_value == "null") {
NULL
} else if (type == "str") {
raw_value
} else if (type == "int") {
as.integer(raw_value)
} else if (type == "float") {
as.numeric(raw_value)
} else if (type == "bool") {
as.logical(raw_value)
} else {
stop("Unknown type: ", type)
}

list(name = name, value = value)
})

values <- sapply(parsed, function(x) {
x$value
})
names <- sapply(parsed, function(x) {
x$name
})

setNames(values, names)
}

parse_instance_settings <- function(env_file) {
env_prefix <- "lamindb_instance_"

field_types <- list(
owner = "str",
name = "str",
storage_root = "str",
storage_region = "str",
db = "Optional[str]",
schema_str = "Optional[str]",
id = "str",
git_repo = "Optional[str]",
keep_artifacts_local = "Optional[bool]"
)

settings_store__read_typed_env(env_file, env_prefix, field_types)
}

parse_user_settings <- function(env_file) {
env_prefix <- "lamin_user_"

field_types <- list(
email = "str",
password = "str",
access_token = "str",
uid = "str",
uuid = "str",
handle = "str",
name = "str"
)

settings_store__read_typed_env(env_file, env_prefix, field_types)
}