Skip to content

settings

Functions:

Attributes:

APP_AUTHOR module-attribute

APP_AUTHOR = 'vsjet'

APP_NAME module-attribute

APP_NAME = 'vsscale'

ENV_KEYS module-attribute

ENV_KEYS = ('VSSCALE_GLOBAL', 'VSSCALE_{}_GLOBAL')

FALSY module-attribute

FALSY = frozenset({'0', 'false', 'no', 'off'})

TOML_CONFIG module-attribute

TOML_CONFIG = ('vsjet.toml', 'pyproject.toml')

TOML_KEYS module-attribute

TOML_KEYS: tuple[list[str], ...] = (['vsscale'], ['tool', 'vsscale'])

TRUTHY module-attribute

TRUTHY = frozenset({'1', 'true', 'yes', 'on'})

logger module-attribute

logger = getLogger(__name__)

get_artifact_path

get_artifact_path(filename: str, *, fallback: bool = True) -> Path
Source code in vsscale/mlrt/settings.py
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
def get_artifact_path(filename: str, *, fallback: bool = True) -> Path:
    dirname = get_artifacts_folder()
    path = dirname / filename

    if path.exists():
        logger.info("Found artifact %s at %s", filename, path)
        return path

    if fallback and is_fallback_enabled("artifact") and dirname != (g_dirname := get_artifacts_folder(global_=True)):
        g_path = g_dirname / filename
        if g_path.exists():
            logger.info("Found artifact %s in global fallback %s", filename, g_path)
            return g_path

    logger.info("Artifact %s not found. Returning default path: %s", filename, path)
    return path

get_artifacts_folder cached

get_artifacts_folder(*, global_: bool = False) -> Path

If global_=True: - Linux: ~/.cache/vsscale/artifact - macOS: ~/Library/Caches/vsscale/artifact - Windows: ...\AppData\Local\vsjet\vsscale\Cache\artifact

Else returns a local .vsjet package storage.

Source code in vsscale/mlrt/settings.py
74
75
76
77
78
79
80
81
82
83
84
@cache
def get_artifacts_folder(*, global_: bool = False) -> Path:
    r"""
    If `global_=True`:
        - Linux: ~/.cache/vsscale/artifact
        - macOS: ~/Library/Caches/vsscale/artifact
        - Windows: ...\AppData\Local\vsjet\vsscale\Cache\artifact

    Else returns a local `.vsjet` package storage.
    """
    return get_cache("artifact", global_=global_) / "artifact"

get_cache cached

get_cache(thing: str, *, global_: bool = False) -> Path
Source code in vsscale/mlrt/settings.py
50
51
52
53
54
55
56
57
58
@cache
def get_cache(thing: str, *, global_: bool = False) -> Path:
    if global_ or is_global_true(thing):
        return get_global_cache()

    if get_toml_config().get("global", False):
        return get_global_cache()

    return get_local_cache()

get_global_cache cached

get_global_cache() -> Path
Source code in vsscale/mlrt/settings.py
38
39
40
41
42
@cache
def get_global_cache() -> Path:
    import platformdirs

    return platformdirs.user_cache_path(APP_NAME, APP_AUTHOR)

get_local_cache cached

get_local_cache() -> Path
Source code in vsscale/mlrt/settings.py
45
46
47
@cache
def get_local_cache() -> Path:
    return PackageStorage(package_name=f"{__name__}").folder

get_model_folder

get_model_folder(
    provider: str, version: str | None = None, *, global_: bool = False
) -> Path
Source code in vsscale/mlrt/settings.py
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
def get_model_folder(provider: str, version: str | None = None, *, global_: bool = False) -> Path:
    folder = get_onnx_folder(global_=global_) / provider.lower()

    if version is None:
        latest = sorted(folder.glob("*"), reverse=True)

        if not latest:
            raise FileNotExistsError("The folder doesn't exist", get_model_folder)

        return latest[0]

    folder /= version

    if not folder.exists():
        raise FileNotExistsError("The folder doesn't exist", get_model_folder)

    return folder

get_model_path

get_model_path(
    provider: str, model_name: str, version: str | None = None
) -> Path
Source code in vsscale/mlrt/settings.py
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
def get_model_path(provider: str, model_name: str, version: str | None = None) -> Path:
    with suppress(FileNotExistsError):
        if (path := get_model_folder(provider, version) / f"{model_name}.onnx").exists():
            logger.info("Found model %s at %s", model_name, path)
            return path

    if is_fallback_enabled("onnx") and get_onnx_folder() != get_onnx_folder(global_=True):
        with suppress(FileNotExistsError):
            if (path := get_model_folder(provider, version, global_=True) / f"{model_name}.onnx").exists():
                logger.info("Found model %s in global fallback %s", model_name, path)
                return path

    default_path = get_model_folder(provider, version) / f"{model_name}.onnx"
    logger.info("Model %s not found. Returning default path: %s", model_name, default_path)
    return default_path

get_onnx_folder cached

get_onnx_folder(*, global_: bool = False) -> Path

If global_=True: - Linux: ~/.cache/vsscale/onnx - macOS: ~/Library/Caches/vsscale/onnx - Windows: ...\AppData\Local\vsjet\vsscale\Cache\onnx

Else returns a local .vsjet package storage.

Source code in vsscale/mlrt/settings.py
61
62
63
64
65
66
67
68
69
70
71
@cache
def get_onnx_folder(*, global_: bool = False) -> Path:
    r"""
    If `global_=True`:
        - Linux: ~/.cache/vsscale/onnx
        - macOS: ~/Library/Caches/vsscale/onnx
        - Windows: ...\AppData\Local\vsjet\vsscale\Cache\onnx

    Else returns a local `.vsjet` package storage.
    """
    return get_cache("onnx", global_=global_) / "onnx"

get_toml_config

get_toml_config() -> dict[str, Any]
Source code in vsscale/mlrt/settings.py
24
25
26
27
28
29
30
31
32
33
34
35
def get_toml_config() -> dict[str, Any]:
    for config_file, keys in zip(TOML_CONFIG, TOML_KEYS):
        file = Path(config_file).expanduser().resolve().absolute()
        if file.exists():
            with file.open("rb") as f:
                config = tomllib.load(f)

            for key in keys:
                config = config.get(key, {})
            return config

    return {}

is_fallback_enabled

is_fallback_enabled(thing: str) -> bool
Source code in vsscale/mlrt/settings.py
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
def is_fallback_enabled(thing: str) -> bool:
    keys = ["VSSCALE_FALLBACK", f"VSSCALE_{thing.upper()}_FALLBACK"]

    for k in keys:
        if val := os.getenv(k, ""):
            return val.lower() not in FALSY

    config = get_toml_config()
    if (val := config.get("fallback")) is not None:
        return bool(val)

    if isinstance(val := config.get(thing, {}), dict) and "fallback" in val:
        return bool(val["fallback"])

    return True

is_global_true

is_global_true(filetype: str) -> bool
Source code in vsscale/mlrt/settings.py
87
88
def is_global_true(filetype: str) -> bool:
    return any(env.lower() in TRUTHY for env in (os.getenv(k.format(filetype.upper()), "") for k in ENV_KEYS))