Coverage for /opt/hostedtoolcache/Python/3.10.18/x64/lib/python3.10/site-packages/bioimageio/spec/_internal/_settings.py: 95%
58 statements
« prev ^ index » next coverage.py v7.10.7, created at 2025-10-14 08:35 +0000
« prev ^ index » next coverage.py v7.10.7, created at 2025-10-14 08:35 +0000
1import os
2from functools import cached_property
3from pathlib import Path
4from typing import Optional, Union
6import platformdirs
7from genericache import DiskCache
8from genericache.digest import UrlDigest
9from pydantic import Field, field_validator
10from pydantic_settings import BaseSettings, SettingsConfigDict
11from typing_extensions import Annotated
13from .root_url import RootHttpUrl
16class Settings(
17 BaseSettings, extra="ignore", allow_inf_nan=False, validate_assignment=True
18):
19 """environment variables for bioimageio.spec"""
21 model_config = SettingsConfigDict(
22 env_prefix="BIOIMAGEIO_", env_file=".env", env_file_encoding="utf-8"
23 )
25 allow_pickle: bool = False
26 """Sets the `allow_pickle` argument for `numpy.load()`"""
28 cache_path: Path = Path(platformdirs.user_cache_dir("bioimageio"))
29 """bioimageio cache location"""
31 @field_validator("cache_path", mode="after")
32 @classmethod
33 def _expand_user(cls, value: Path):
34 return Path(os.path.expanduser(str(value)))
36 collection_http_pattern: str = (
37 "https://hypha.aicell.io/bioimage-io/artifacts/{bioimageio_id}/files/rdf.yaml"
38 )
39 """A pattern to map bioimageio IDs to bioimageio.yaml URLs.
40 Notes:
41 - '{bioimageio_id}' is replaced with user query,
42 e.g. "affable-shark" when calling `load_description("affable-shark")`.
43 - This method takes precedence over resolving via **id_map**.
44 - If this endpoints fails, we fall back to **id_map**.
45 """
47 hypha_upload: str = (
48 "https://hypha.aicell.io/public/services/artifact-manager/create"
49 )
50 """URL to the upload endpoint for bioimageio resources."""
52 hypha_upload_token: Optional[str] = None
53 """Hypha API token to use for uploads.
55 By setting this token you agree to our terms of service at https://bioimage.io/#/toc.
57 How to obtain a token:
58 1. Login to https://bioimage.io
59 2. Generate a new token at https://bioimage.io/#/api?tab=hypha-rpc
60 """
62 http_timeout: float = 10.0
63 """Timeout in seconds for http requests."""
65 id_map: str = (
66 "https://uk1s3.embassy.ebi.ac.uk/public-datasets/bioimage.io/id_map.json"
67 )
68 """URL to bioimageio id_map.json to resolve resource IDs."""
70 id_map_draft: str = (
71 "https://uk1s3.embassy.ebi.ac.uk/public-datasets/bioimage.io/id_map_draft.json"
72 )
73 """URL to bioimageio id_map_draft.json to resolve draft IDs ending with '/draft'."""
75 perform_io_checks: bool = True
76 """Wether or not to perform validation that requires file io,
77 e.g. downloading a remote files.
79 Existence of any local absolute file paths is still being checked."""
81 resolve_draft: bool = True
82 """Flag to resolve draft resource versions following the pattern
83 <resource id>/draft.
85 Note that anyone may stage a new draft and that such a draft version
86 may not have been reviewed yet.
87 Set this flag to False to avoid this potential security risk
88 and disallow loading draft versions."""
90 log_warnings: bool = True
91 """Log validation warnings to console."""
93 github_username: Optional[str] = None
94 """GitHub username for API requests"""
96 github_token: Optional[str] = None
97 """GitHub token for API requests"""
99 CI: Annotated[Union[bool, str], Field(alias="CI")] = False
100 """Wether or not the execution happens in a continuous integration (CI) environment."""
102 user_agent: Optional[str] = None
103 """user agent for http requests"""
105 @property
106 def github_auth(self):
107 if self.github_username is None or self.github_token is None:
108 return None
109 else:
110 return (self.github_username, self.github_token)
112 @cached_property
113 def disk_cache(self):
114 cache = DiskCache[RootHttpUrl].create(
115 url_type=RootHttpUrl,
116 cache_dir=self.cache_path,
117 url_hasher=UrlDigest.from_str,
118 )
119 return cache
122settings = Settings()
123"""parsed environment variables for bioimageio.spec"""