Coverage for python / lsst / daf / butler / remote_butler / server / _config.py: 0%

41 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-04-18 08:42 +0000

1# This file is part of daf_butler. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

5# (http://www.lsst.org). 

6# See the COPYRIGHT file at the top-level directory of this distribution 

7# for details of code ownership. 

8# 

9# This software is dual licensed under the GNU General Public License and also 

10# under a 3-clause BSD license. Recipients may choose which of these licenses 

11# to use; please see the files gpl-3.0.txt and/or bsd_license.txt, 

12# respectively. If you choose the GPL option then the following text applies 

13# (but note that there is still no warranty even if you opt for BSD instead): 

14# 

15# This program is free software: you can redistribute it and/or modify 

16# it under the terms of the GNU General Public License as published by 

17# the Free Software Foundation, either version 3 of the License, or 

18# (at your option) any later version. 

19# 

20# This program is distributed in the hope that it will be useful, 

21# but WITHOUT ANY WARRANTY; without even the implied warranty of 

22# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

23# GNU General Public License for more details. 

24# 

25# You should have received a copy of the GNU General Public License 

26# along with this program. If not, see <http://www.gnu.org/licenses/>. 

27 

28from __future__ import annotations 

29 

30from collections.abc import Iterator 

31from contextlib import contextmanager 

32from functools import cache 

33from typing import Literal 

34 

35from pydantic import AnyHttpUrl, BaseModel 

36from pydantic_settings import BaseSettings, SettingsConfigDict 

37 

38from .._config import AuthenticationMode 

39 

40 

41class RepositoryConfig(BaseModel): 

42 """Per-repository configuration for the Butler server.""" 

43 

44 config_uri: str 

45 """Path to DirectButler configuration YAML file for the repository.""" 

46 authorized_groups: list[str] 

47 """List of Gafaelfawr groups that will be allowed to access this 

48 repository. If this list contains the special group `*`, all users will be 

49 granted access. 

50 """ 

51 

52 

53class ButlerServerConfig(BaseSettings): 

54 """Butler server configuration loaded from environment variables.""" 

55 

56 model_config = SettingsConfigDict(env_prefix="DAF_BUTLER_SERVER_") 

57 

58 repositories: dict[str, RepositoryConfig] 

59 """Mapping from repository name to configuration for the repository.""" 

60 

61 gafaelfawr_url: AnyHttpUrl | Literal["DISABLED"] 

62 """URL to the top-level HTTP path where Gafaelfawr can be found (e.g. 

63 "https://data-int.lsst.cloud"). 

64 

65 This can instead be the special string "DISABLED" to turn off all features 

66 requiring Gafaelfawr integration. 

67 """ 

68 

69 authentication: AuthenticationMode 

70 

71 static_files_path: str | None = None 

72 """Absolute path to a directory of files that will be served to end-users 

73 as static files from the `configs/` HTTP route. 

74 """ 

75 

76 @property 

77 def gafaelfawr_enabled(self) -> bool: 

78 return self.gafaelfawr_url != "DISABLED" 

79 

80 

81_config: ButlerServerConfig | None = None 

82 

83 

84@cache 

85def load_config() -> ButlerServerConfig: 

86 """Read the Butler server configuration from the environment.""" 

87 global _config 

88 if _config is None: 

89 _config = ButlerServerConfig() 

90 return _config 

91 

92 

93@contextmanager 

94def mock_config(temporary_config: ButlerServerConfig | None = None) -> Iterator[ButlerServerConfig]: 

95 """Replace the global Butler server configuration with a temporary value. 

96 

97 Parameters 

98 ---------- 

99 temporary_config : `ButlerServerConfig`, optional 

100 Configuration to replace the global value with. If not provided, 

101 a default empty configuration will be used. 

102 

103 Returns 

104 ------- 

105 config : `ButlerServerConfig` 

106 The new configuration object. 

107 """ 

108 global _config 

109 orig = _config 

110 try: 

111 if temporary_config is None: 

112 temporary_config = ButlerServerConfig( 

113 repositories={}, 

114 gafaelfawr_url="http://gafaelfawr.example", 

115 authentication="rubin_science_platform", 

116 static_files_path=None, 

117 ) 

118 _config = temporary_config 

119 load_config.cache_clear() 

120 yield _config 

121 finally: 

122 _config = orig