File: BasiliskEnvironment.R

package info (click to toggle)
r-bioc-basilisk 1.18.0%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 392 kB
  • sloc: python: 12; sh: 4; makefile: 2
file content (102 lines) | stat: -rw-r--r-- 3,994 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#' The BasiliskEnvironment class
#'
#' The BasiliskEnvironment class provides a simple structure 
#' containing all of the information to construct a \pkg{basilisk} environment.
#' It is used by \code{\link{basiliskStart}} to perform lazy installation.
#'
#' @author Aaron lun
#'
#' @section Constructor:
#' \code{BasiliskEnvironment(envname, pkgname, packages)} will return a BasiliskEnvironment object, given:
#' \itemize{
#' \item \code{envname}, string containing the name of the environment.
#' Environment names starting with an underscore are reserved for internal use.
#' \item \code{pkgname}, string containing the name of the package that owns the environment.
#' \item \code{packages}, character vector containing the names of the required Python packages from conda,
#' see \code{\link{setupBasiliskEnv}} for requirements.
#' \item \code{channels}, character vector specifying the Conda channels to search,
#' see \code{\link{setupBasiliskEnv}} for detials.
#' \item \code{pip}, character vector containing names of additional Python packages from PyPi,
#' see \code{\link{setupBasiliskEnv}} for requirements.
#' \item \code{paths}, character vector containing relative paths to Python packages to be installed via \code{pip}.
#' These paths are interpreted relative to the system directory of \code{pkgname},
#' i.e., they are appended to the output of \code{\link{system.file}} to obtain absolute paths for \code{\link{setupBasiliskEnv}}.
#' Thus, any Python package vendored into the R package should be stored in the \code{inst} directory of the latter's source.
#' }
#' 
#' @examples
#' BasiliskEnvironment("my_env1", "AaronPackage", 
#'     packages=c("scikit-learn=1.1.1", "pandas=1.43.1"))
#' @docType class
#' @name BasiliskEnvironment-class
#' @aliases BasiliskEnvironment-class
#' BasiliskEnvironment
NULL

#' @export
setClass("BasiliskEnvironment", slots=c(envname="character", pkgname="character", 
    packages="character", channels="character", pip="character", paths="character"))

#' @export
#' @import methods 
BasiliskEnvironment <- function(envname, pkgname, packages, channels="conda-forge", pip=character(0), paths=character(0)) {
    new("BasiliskEnvironment", envname=envname, pkgname=pkgname, packages=packages, channels=channels, pip=pip, paths=paths)
}

setValidity("BasiliskEnvironment", function(object) {
    msg <- character(0)

    if (length(val <- .getEnvName(object))!=1 || is.na(val) || !is.character(val)){ 
        msg <- c(msg, "'envname' should be a non-NA string")
    }
    if (grepl("^_", val)) {
        msg <- c(msg, "environment names starting with an underscore are reserved")
    }

    if (length(val <- .getPkgName(object))!=1 || is.na(val) || !is.character(val)){ 
        msg <- c(msg, "'pkgname' should be a non-NA string")
    }

    if (any(is.na(.getChannels(object)))) {
        msg <- c(msg, "'channels' should not contain NA strings")
    }

    if (any(is.na(.getPackages(object)))) {
        msg <- c(msg, "'packages' should not contain NA strings")
    }

    if (any(is.na(.getPipPackages(object)))) {
        msg <- c(msg, "'pip' should not contain NA strings")
    }

    if (any(is.na(.getPipPaths(object)))) {
        msg <- c(msg, "'paths' should not contain NA strings")
    }

    if (length(msg)) {
        msg
    } else {
        TRUE
    }
})

setGeneric(".getEnvName", function(x) standardGeneric(".getEnvName"))

setGeneric(".getPkgName", function(x) standardGeneric(".getPkgName"))

setMethod(".getEnvName", "BasiliskEnvironment", function(x) x@envname)

setMethod(".getPkgName", "BasiliskEnvironment", function(x) x@pkgname)

setMethod(".getEnvName", "character", identity)

setMethod(".getPkgName", "character", function(x) NULL) 

# Until all dependencies bump their version numbers to trigger a rebuild.
.getChannels <- function(x) tryCatch(x@channels, error=function(e) "conda-forge")

.getPackages <- function(x) x@packages

.getPipPackages <- function(x) x@pip

.getPipPaths <- function(x) x@paths