Coverage for python/lsst/daf/butler/remote_butler/_factory.py: 6%
35 statements
« prev ^ index » next coverage.py v7.4.1, created at 2024-02-13 10:56 +0000
« prev ^ index » next coverage.py v7.4.1, created at 2024-02-13 10:56 +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
33from lsst.daf.butler.repo_relocation import replaceRoot
35from .._butler_config import ButlerConfig
36from .._butler_instance_options import ButlerInstanceOptions
37from ._authentication import get_authentication_token_from_environment
38from ._config import RemoteButlerConfigModel
39from ._remote_butler import RemoteButler, RemoteButlerCache
42class RemoteButlerFactory:
43 """Factory for instantiating RemoteButler instances bound to a user's Rubin
44 Science Platform Gafaelfawr access token. All Butler instances created by
45 this factory share a common HTTP connection pool.
47 Parameters
48 ----------
49 server_url : `str`
50 The URL of the Butler server that RemoteButler instances created by
51 this factory will connect to.
52 http_client : `httpx.Client`, optional
53 The httpx connection pool that RemoteButler instances created by this
54 factory will use for making HTTP requests. If omitted, creates a new
55 connection pool.
57 Notes
58 -----
59 Most users should not directly call this constructor -- instead use
60 ``create_factory_from_config`` or ``create_factory_for_url``.
61 """
63 def __init__(self, server_url: str, http_client: httpx.Client | None = None):
64 self.server_url = server_url
65 if http_client is not None:
66 self.http_client = http_client
67 else:
68 self.http_client = httpx.Client()
69 self._cache = RemoteButlerCache()
71 @staticmethod
72 def create_factory_from_config(config: ButlerConfig) -> RemoteButlerFactory:
73 # There is a convention in Butler config files where <butlerRoot> in a
74 # configuration option refers to the directory containing the
75 # configuration file. We allow this for the remote butler's URL so
76 # that the server doesn't have to know which hostname it is being
77 # accessed from.
78 server_url_key = ("remote_butler", "url")
79 if server_url_key in config:
80 config[server_url_key] = replaceRoot(config[server_url_key], config.configDir)
81 remote_config = RemoteButlerConfigModel.model_validate(config)
82 return RemoteButlerFactory.create_factory_for_url(str(remote_config.remote_butler.url))
84 @staticmethod
85 def create_factory_for_url(server_url: str) -> RemoteButlerFactory:
86 return RemoteButlerFactory(server_url)
88 def create_butler_for_access_token(
89 self, access_token: str, *, butler_options: ButlerInstanceOptions | None = None
90 ) -> RemoteButler:
91 if butler_options is None:
92 butler_options = ButlerInstanceOptions()
93 return RemoteButler(
94 http_client=self.http_client,
95 access_token=access_token,
96 options=butler_options,
97 server_url=self.server_url,
98 cache=self._cache,
99 )
101 def create_butler_with_credentials_from_environment(
102 self, *, butler_options: ButlerInstanceOptions | None = None
103 ) -> RemoteButler:
104 token = get_authentication_token_from_environment(self.server_url)
105 if token is None:
106 raise RuntimeError(
107 "Attempting to connect to Butler server,"
108 " but no access credentials were found in the environment."
109 )
110 return self.create_butler_for_access_token(token, butler_options=butler_options)