Coverage for bioimageio/spec/_internal/_settings.py: 98%

52 statements  

« prev     ^ index     » next       coverage.py v7.9.1, created at 2025-06-27 09:20 +0000

1import os 

2from functools import cached_property 

3from pathlib import Path 

4from typing import Optional, Union 

5 

6import pooch # pyright: ignore [reportMissingTypeStubs] 

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 

12 

13from .root_url import RootHttpUrl 

14 

15 

16class Settings(BaseSettings, extra="ignore"): 

17 """environment variables for bioimageio.spec""" 

18 

19 model_config = SettingsConfigDict( 

20 env_prefix="BIOIMAGEIO_", env_file=".env", env_file_encoding="utf-8" 

21 ) 

22 

23 allow_pickle: bool = False 

24 """Sets the `allow_pickle` argument for `numpy.load()`""" 

25 

26 cache_path: Path = pooch.os_cache("bioimageio") 

27 """bioimageio cache location""" 

28 

29 @field_validator("cache_path", mode="after") 

30 @classmethod 

31 def _expand_user(cls, value: Path): 

32 return Path(os.path.expanduser(str(value))) 

33 

34 collection_http_pattern: str = ( 

35 "https://hypha.aicell.io/bioimage-io/artifacts/{bioimageio_id}/files/rdf.yaml" 

36 ) 

37 """A pattern to map bioimageio IDs to bioimageio.yaml URLs. 

38 Notes: 

39 - '{bioimageio_id}' is replaced with user query, 

40 e.g. "affable-shark" when calling `load_description("affable-shark")`. 

41 - This method takes precedence over resolving via **id_map**. 

42 - If this endpoints fails, we fall back to **id_map**. 

43 """ 

44 

45 id_map: str = ( 

46 "https://uk1s3.embassy.ebi.ac.uk/public-datasets/bioimage.io/id_map.json" 

47 ) 

48 """URL to bioimageio id_map.json to resolve resource IDs.""" 

49 

50 id_map_draft: str = ( 

51 "https://uk1s3.embassy.ebi.ac.uk/public-datasets/bioimage.io/id_map_draft.json" 

52 ) 

53 """URL to bioimageio id_map_draft.json to resolve draft IDs ending with '/draft'.""" 

54 

55 perform_io_checks: bool = True 

56 """Wether or not to perform validation that requires file io, 

57 e.g. downloading a remote files. 

58 

59 Existence of any local absolute file paths is still being checked.""" 

60 

61 resolve_draft: bool = True 

62 """Flag to resolve draft resource versions following the pattern 

63 <resource id>/draft. 

64 

65 Note that anyone may stage a new draft and that such a draft version 

66 may not have been reviewed yet. 

67 Set this flag to False to avoid this potential security risk 

68 and disallow loading draft versions.""" 

69 

70 log_warnings: bool = True 

71 """Log validation warnings to console.""" 

72 

73 github_username: Optional[str] = None 

74 """GitHub username for API requests""" 

75 

76 github_token: Optional[str] = None 

77 """GitHub token for API requests""" 

78 

79 CI: Annotated[Union[bool, str], Field(alias="CI")] = False 

80 """Wether or not the execution happens in a continuous integration (CI) environment.""" 

81 

82 user_agent: Optional[str] = None 

83 """user agent for http requests""" 

84 

85 @property 

86 def github_auth(self): 

87 if self.github_username is None or self.github_token is None: 

88 return None 

89 else: 

90 return (self.github_username, self.github_token) 

91 

92 @cached_property 

93 def disk_cache(self): 

94 cache = DiskCache[RootHttpUrl].create( 

95 url_type=RootHttpUrl, 

96 cache_dir=self.cache_path, 

97 url_hasher=UrlDigest.from_str, 

98 ) 

99 return cache 

100 

101 

102settings = Settings() 

103"""parsed environment variables for bioimageio.spec"""