Coverage for python / lsst / daf / butler / remote_butler / _factory.py: 0%
48 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-04-24 08:16 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-04-24 08:16 +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/>.
28from __future__ import annotations
30__all__ = ("RemoteButlerFactory",)
32import httpx
34from lsst.daf.butler.repo_relocation import replaceRoot
36from .._butler_config import ButlerConfig
37from .._butler_instance_options import ButlerInstanceOptions
38from ..registry import RegistryDefaults
39from ._config import RemoteButlerConfigModel, RemoteButlerOptionsModel
40from ._http_connection import RemoteButlerHttpConnection
41from ._remote_butler import RemoteButler, RemoteButlerCache
42from .authentication.cadc import CadcAuthenticationProvider
43from .authentication.interface import RemoteButlerAuthenticationProvider
44from .authentication.rubin import RubinAuthenticationProvider
47class RemoteButlerFactory:
48 """Factory for instantiating RemoteButler instances bound to a user's Rubin
49 Science Platform Gafaelfawr access token. All Butler instances created by
50 this factory share a common HTTP connection pool.
52 Parameters
53 ----------
54 config : `RemoteButlerOptionsModel`
55 `RemoteButler` configuration information (as read from `ButlerConfig`
56 YAML file).
57 http_client : `httpx.Client`, optional
58 The httpx connection pool that RemoteButler instances created by this
59 factory will use for making HTTP requests. If omitted, creates a new
60 connection pool.
62 Notes
63 -----
64 Most users should not directly call this constructor -- instead use
65 ``create_factory_from_config``.
66 """
68 def __init__(self, config: RemoteButlerOptionsModel, http_client: httpx.Client | None = None):
69 self._config = config
70 self.server_url = str(config.url)
71 if http_client is not None:
72 self.http_client = http_client
73 else:
74 self.http_client = httpx.Client(
75 # This timeout is fairly conservative. This value isn't the
76 # maximum amount of time the request can take -- it's the
77 # maximum amount of time to wait after receiving the last chunk
78 # of data from the server.
79 #
80 # Long-running, streamed queries send a keep-alive every 15
81 # seconds. However, unstreamed operations like
82 # queryCollections can potentially take a while if the database
83 # is under duress.
84 timeout=120 # seconds
85 )
86 self._cache = RemoteButlerCache()
88 @staticmethod
89 def create_factory_from_config(
90 config: ButlerConfig, http_client: httpx.Client | None = None
91 ) -> RemoteButlerFactory:
92 # There is a convention in Butler config files where <butlerRoot> in a
93 # configuration option refers to the directory containing the
94 # configuration file. We allow this for the remote butler's URL so
95 # that the server doesn't have to know which hostname it is being
96 # accessed from.
97 server_url_key = ("remote_butler", "url")
98 if server_url_key in config:
99 config[server_url_key] = replaceRoot(config[server_url_key], config.configDir)
100 remote_config = RemoteButlerConfigModel.model_validate(config)
101 return RemoteButlerFactory(remote_config.remote_butler, http_client=http_client)
103 @staticmethod
104 def create_factory_for_url(
105 server_url: str, http_client: httpx.Client | None = None
106 ) -> RemoteButlerFactory:
107 config = ButlerConfig(server_url)
108 return RemoteButlerFactory.create_factory_from_config(config, http_client=http_client)
110 def _create_butler(
111 self,
112 *,
113 auth: RemoteButlerAuthenticationProvider,
114 butler_options: ButlerInstanceOptions | None,
115 enable_datastore_cache: bool = False,
116 ) -> RemoteButler:
117 if butler_options is None:
118 butler_options = ButlerInstanceOptions()
119 return RemoteButler(
120 connection=RemoteButlerHttpConnection(
121 http_client=self.http_client, server_url=self.server_url, auth=auth
122 ),
123 defaults=RegistryDefaults.from_butler_instance_options(butler_options),
124 cache=self._cache,
125 use_disabled_datastore_cache=not enable_datastore_cache,
126 )
128 def create_butler_for_access_token(
129 self,
130 access_token: str,
131 *,
132 butler_options: ButlerInstanceOptions | None = None,
133 enable_datastore_cache: bool = False,
134 ) -> RemoteButler:
135 auth: RemoteButlerAuthenticationProvider
136 if self._config.authentication == "rubin_science_platform":
137 auth = RubinAuthenticationProvider(access_token)
138 elif self._config.authentication == "cadc":
139 auth = CadcAuthenticationProvider(access_token)
140 return self._create_butler(
141 auth=auth, butler_options=butler_options, enable_datastore_cache=enable_datastore_cache
142 )
144 def create_butler_with_credentials_from_environment(
145 self,
146 *,
147 butler_options: ButlerInstanceOptions | None = None,
148 enable_datastore_cache: bool = True,
149 ) -> RemoteButler:
150 auth: RemoteButlerAuthenticationProvider
151 if self._config.authentication == "rubin_science_platform":
152 auth = RubinAuthenticationProvider.create_from_environment(self.server_url)
153 elif self._config.authentication == "cadc":
154 auth = CadcAuthenticationProvider.create_from_environment(self.server_url)
156 return self._create_butler(
157 auth=auth, butler_options=butler_options, enable_datastore_cache=enable_datastore_cache
158 )