Coverage for python/lsst/daf/butler/_butlerConfig.py: 23%
41 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-02 02:16 -0700
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-02 02:16 -0700
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 program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation, either version 3 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program. If not, see <http://www.gnu.org/licenses/>.
22"""
23Configuration classes specific to the Butler
24"""
25from __future__ import annotations
27__all__ = ("ButlerConfig",)
29import copy
30import os
31from typing import Optional, Sequence, Union
33from lsst.resources import ResourcePath, ResourcePathExpression
35from .core import Config, DatastoreConfig, StorageClassConfig
36from .registry import RegistryConfig
37from .transfers import RepoTransferFormatConfig
39CONFIG_COMPONENT_CLASSES = (RegistryConfig, StorageClassConfig, DatastoreConfig, RepoTransferFormatConfig)
42class ButlerConfig(Config):
43 """Contains the configuration for a `Butler`
45 The configuration is read and merged with default configurations for
46 the particular classes. The defaults are read according to the rules
47 outlined in `ConfigSubset`. Each component of the configuration associated
48 with a configuration class reads its own defaults.
50 Parameters
51 ----------
52 other : `str`, `Config`, `ButlerConfig`, optional
53 Path to butler configuration YAML file or a directory containing a
54 "butler.yaml" file. If `None` the butler will
55 be configured based entirely on defaults read from the environment
56 or from ``searchPaths``.
57 No defaults will be read if a `ButlerConfig` is supplied directly.
58 searchPaths : `list` or `tuple`, optional
59 Explicit additional paths to search for defaults. They should
60 be supplied in priority order. These paths have higher priority
61 than those read from the environment in
62 `ConfigSubset.defaultSearchPaths()`. They are only read if ``other``
63 refers to a configuration file or directory.
64 """
66 def __init__(
67 self,
68 other: Optional[Union[ResourcePathExpression, Config]] = None,
69 searchPaths: Sequence[ResourcePathExpression] | None = None,
70 ):
71 self.configDir: Optional[ResourcePath] = None
73 # If this is already a ButlerConfig we assume that defaults
74 # have already been loaded.
75 if other is not None and isinstance(other, ButlerConfig):
76 super().__init__(other)
77 # Ensure that the configuration directory propagates
78 self.configDir = copy.copy(other.configDir)
79 return
81 # Include ResourcePath here in case it refers to a directory.
82 # Creating a ResourcePath from a ResourcePath is a no-op.
83 if isinstance(other, (str, os.PathLike, ResourcePath)):
84 # This will only allow supported schemes
85 uri = ResourcePath(other)
87 # We allow the butler configuration file to be left off the
88 # URI supplied by the user. If a directory-like URI is given
89 # we add the default configuration name.
91 # It's easy to miss a trailing / for remote URIs so try to guess
92 # we have been given a directory-like URI if there is no
93 # file extension. Local URIs do not need any guess work.
94 if not uri.isLocal and not uri.getExtension():
95 uri = ResourcePath(other, forceDirectory=True)
97 if uri.isdir():
98 # Could also be butler.json (for example in the butler
99 # server) but checking for existence will slow things
100 # down given that this might involve two checks and then
101 # the config read below would still do the read.
102 other = uri.join("butler.yaml")
104 # Create an empty config for us to populate
105 super().__init__()
107 # Read the supplied config so that we can work out which other
108 # defaults to use.
109 butlerConfig = Config(other)
111 configFile = butlerConfig.configFile
112 if configFile is not None:
113 uri = ResourcePath(configFile)
114 self.configFile = uri
115 self.configDir = uri.dirname()
117 # A Butler config contains defaults defined by each of the component
118 # configuration classes. We ask each of them to apply defaults to
119 # the values we have been supplied by the user.
120 for configClass in CONFIG_COMPONENT_CLASSES:
121 # Only send the parent config if the child
122 # config component is present (otherwise it assumes that the
123 # keys from other components are part of the child)
124 localOverrides = None
125 if configClass.component in butlerConfig:
126 localOverrides = butlerConfig
127 config = configClass(localOverrides, searchPaths=searchPaths)
128 # Re-attach it using the global namespace
129 assert configClass.component is not None, "Config class component cannot be None"
130 self.update({configClass.component: config})
131 # Remove the key from the butlerConfig since we have already
132 # merged that information.
133 if configClass.component in butlerConfig:
134 del butlerConfig[configClass.component]
136 # Now that we have all the defaults we can merge the externally
137 # provided config into the defaults.
138 # Not needed if there is never information in a butler config file
139 # not present in component configurations
140 self.update(butlerConfig)