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

56 statements  

« prev     ^ index     » next       coverage.py v7.10.3, created at 2025-08-12 17:44 +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 hypha_upload: str = ( 

46 "https://hypha.aicell.io/public/services/artifact-manager/create" 

47 ) 

48 """URL to the upload endpoint for bioimageio resources.""" 

49 

50 hypha_upload_token: Optional[str] = None 

51 """Hypha API token to use for uploads. 

52 

53 By setting this token you agree to our terms of service at https://bioimage.io/#/toc. 

54 

55 How to obtain a token: 

56 1. Login to https://bioimage.io 

57 2. Generate a new token at https://bioimage.io/#/api?tab=hypha-rpc 

58 """ 

59 

60 id_map: str = ( 

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

62 ) 

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

64 

65 id_map_draft: str = ( 

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

67 ) 

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

69 

70 perform_io_checks: bool = True 

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

72 e.g. downloading a remote files. 

73 

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

75 

76 resolve_draft: bool = True 

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

78 <resource id>/draft. 

79 

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

81 may not have been reviewed yet. 

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

83 and disallow loading draft versions.""" 

84 

85 log_warnings: bool = True 

86 """Log validation warnings to console.""" 

87 

88 github_username: Optional[str] = None 

89 """GitHub username for API requests""" 

90 

91 github_token: Optional[str] = None 

92 """GitHub token for API requests""" 

93 

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

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

96 

97 user_agent: Optional[str] = None 

98 """user agent for http requests""" 

99 

100 @property 

101 def github_auth(self): 

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

103 return None 

104 else: 

105 return (self.github_username, self.github_token) 

106 

107 @cached_property 

108 def disk_cache(self): 

109 cache = DiskCache[RootHttpUrl].create( 

110 url_type=RootHttpUrl, 

111 cache_dir=self.cache_path, 

112 url_hasher=UrlDigest.from_str, 

113 ) 

114 return cache 

115 

116 

117settings = Settings() 

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