-
Notifications
You must be signed in to change notification settings - Fork 8
/
config.py
92 lines (79 loc) · 2.76 KB
/
config.py
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
import os
import platform
import warnings
from pathlib import Path
from typing import TYPE_CHECKING, Generator, List, Optional
import yaml
from pydantic import ConfigDict, Field, field_validator
with warnings.catch_warnings():
# requests which is a transitive dependency has some chatty warnings during import
warnings.simplefilter("ignore", Warning)
import requests # noqa: F401
from ensureconda.api import ensureconda
from pydantic_settings import BaseSettings
if TYPE_CHECKING:
from _typeshed import StrPath
def is_windows() -> bool:
return platform.system() == "Windows"
class Config(BaseSettings):
prefix_path: Path = Path("~").expanduser() / ".condax"
link_destination: Path = Path("~").expanduser() / ".local" / "bin"
channels: List[str] = ["conda-forge", "defaults"]
conda_executable: Optional[Path] = Field(
default=ensureconda(
mamba=True, micromamba=True, conda=True, conda_exe=True, no_install=True
)
)
model_config = ConfigDict(env_prefix="CONDAX_")
@field_validator("prefix_path", "link_destination", mode="before")
@classmethod
def ensure_prefix_path(cls, v: Path) -> Path:
v = v.expanduser()
if not v.exists():
v.mkdir(parents=True, exist_ok=True)
v = v.resolve()
return v
def ensure_conda_executable(self, require_mamba: bool = True):
def candidates() -> "Generator[Optional[StrPath], None, None]":
yield ensureconda(
mamba=True,
micromamba=True,
conda=False,
conda_exe=False,
no_install=True,
)
if not require_mamba:
yield ensureconda(
mamba=False,
micromamba=False,
conda=True,
conda_exe=True,
no_install=True,
)
yield ensureconda(
mamba=True,
micromamba=True,
no_install=False,
conda=False,
conda_exe=False,
)
if not require_mamba:
yield ensureconda(
mamba=False,
micromamba=False,
no_install=False,
conda=True,
conda_exe=True,
)
for c in candidates():
if c is not None:
self.conda_executable = Path(c)
return
else:
raise RuntimeError("Could not find conda executable")
_condaxrc_path = os.path.expanduser(os.path.join("~", ".condaxrc"))
if os.path.exists(_condaxrc_path):
with open(_condaxrc_path) as fo:
CONFIG = Config(**yaml.safe_load(fo))
else:
CONFIG = Config()