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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
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
75
76
77
78
79
80
81
82
83
84
85
@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
51
52
53
54
55
56
57
58
59
@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
39
40
41
42
43
@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
46
47
48
@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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
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
62
63
64
65
66
67
68
69
70
71
72
@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
25
26
27
28
29
30
31
32
33
34
35
36
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
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
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
88
89
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))

write_toml_config

write_toml_config(
    file: str | PathLike[str],
    *,
    global_: bool | None = None,
    fallback: bool | None = None,
    provider: Sequence[str] | None = None,
    latest: bool | None = None,
    auto: bool | None = None,
) -> Path
Source code in vsscale/mlrt/settings.py
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
def write_toml_config(
    file: str | os.PathLike[str],
    *,
    global_: bool | None = None,
    fallback: bool | None = None,
    provider: Sequence[str] | None = None,
    latest: bool | None = None,
    auto: bool | None = None,
) -> Path:
    import tomlkit

    filepath = Path(file).expanduser().resolve().absolute()
    is_pyproject = filepath.name.lower() == "pyproject.toml"

    if filepath.exists():
        content = filepath.read_text(encoding="utf-8")
        doc = tomlkit.parse(content)
    else:
        doc = tomlkit.document()

    if is_pyproject:
        if "tool" not in doc:
            doc["tool"] = tomlkit.table()
        tool = doc["tool"]
        if "vsscale" not in tool:
            tool["vsscale"] = tomlkit.table()
        vsscale_table = tool["vsscale"]
    else:
        if "vsscale" not in doc:
            doc["vsscale"] = tomlkit.table()
        vsscale_table = doc["vsscale"]

    if global_ is not None:
        vsscale_table["global"] = global_

    if fallback is not None:
        vsscale_table["fallback"] = fallback

    if provider or latest is not None or auto is not None:
        if "onnx" not in vsscale_table:
            vsscale_table["onnx"] = tomlkit.table()
        onnx_table = vsscale_table["onnx"]

        if "download" not in onnx_table:
            onnx_table["download"] = tomlkit.table()
        download_table = onnx_table["download"]

        if provider:
            download_table["provider"] = list(provider)
        if latest is not None:
            download_table["latest"] = latest
        if auto is not None:
            download_table["auto"] = auto

    filepath.parent.mkdir(parents=True, exist_ok=True)
    filepath.write_text(tomlkit.dumps(doc), encoding="utf-8")
    return filepath