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

56 statements  

« prev     ^ index     » next       coverage.py v7.10.6, created at 2025-09-11 07:34 +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( 

17 BaseSettings, extra="ignore", allow_inf_nan=False, validate_assignment=True 

18): 

19 """environment variables for bioimageio.spec""" 

20 

21 model_config = SettingsConfigDict( 

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

23 ) 

24 

25 allow_pickle: bool = False 

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

27 

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

29 """bioimageio cache location""" 

30 

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

32 @classmethod 

33 def _expand_user(cls, value: Path): 

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

35 

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 """ 

46 

47 hypha_upload: str = ( 

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

49 ) 

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

51 

52 hypha_upload_token: Optional[str] = None 

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

54 

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

56 

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 """ 

61 

62 id_map: str = ( 

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

64 ) 

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

66 

67 id_map_draft: str = ( 

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

69 ) 

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

71 

72 perform_io_checks: bool = True 

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

74 e.g. downloading a remote files. 

75 

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

77 

78 resolve_draft: bool = True 

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

80 <resource id>/draft. 

81 

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

83 may not have been reviewed yet. 

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

85 and disallow loading draft versions.""" 

86 

87 log_warnings: bool = True 

88 """Log validation warnings to console.""" 

89 

90 github_username: Optional[str] = None 

91 """GitHub username for API requests""" 

92 

93 github_token: Optional[str] = None 

94 """GitHub token for API requests""" 

95 

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

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

98 

99 user_agent: Optional[str] = None 

100 """user agent for http requests""" 

101 

102 @property 

103 def github_auth(self): 

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

105 return None 

106 else: 

107 return (self.github_username, self.github_token) 

108 

109 @cached_property 

110 def disk_cache(self): 

111 cache = DiskCache[RootHttpUrl].create( 

112 url_type=RootHttpUrl, 

113 cache_dir=self.cache_path, 

114 url_hasher=UrlDigest.from_str, 

115 ) 

116 return cache 

117 

118 

119settings = Settings() 

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